Skip to content

Commit 1944d7a

Browse files
feat: Centralize decoder
1 parent 09a0777 commit 1944d7a

2 files changed

Lines changed: 9 additions & 3 deletions

File tree

Sources/GraphQLGeneratorCore/Generator/SchemaGenerator.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ package struct SchemaGenerator {
1515
import GraphQLGeneratorRuntime
1616
1717
/// Build a GraphQL schema with the provided resolvers
18-
public func buildGraphQLSchema<Resolvers: ResolversProtocol>(resolvers: Resolvers.Type) throws -> GraphQLSchema {
18+
public func buildGraphQLSchema<Resolvers: ResolversProtocol>(
19+
resolvers: Resolvers.Type,
20+
decoder: MapDecoder = .init(),
21+
) throws -> GraphQLSchema {
1922
"""
2023

2124
// Ignore any internal types (which have prefix "__")
@@ -713,7 +716,7 @@ package struct SchemaGenerator {
713716
let safeArgName = nameGenerator.swiftMemberName(for: argName)
714717
let swiftType = try swiftTypeReference(for: arg.type, nameGenerator: nameGenerator)
715718
// Extract value from Map based on type
716-
var decodeStatement = "try MapDecoder().decode((\(swiftType)).self, from: args[\"\(argName)\"])"
719+
var decodeStatement = "try decoder.decode((\(swiftType)).self, from: args[\"\(argName)\"])"
717720
if !(arg.type is GraphQLNonNull) {
718721
// If the arg is nullable, we get errors if we try to decode an `undefined` map. This protects against that.
719722
decodeStatement = "args[\"\(argName)\"] != .undefined ? \(decodeStatement) : nil"

Tests/GraphQLGeneratorTests/SchemaGeneratorTests.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ struct SchemaGeneratorTests {
4343
import GraphQLGeneratorRuntime
4444
4545
/// Build a GraphQL schema with the provided resolvers
46-
public func buildGraphQLSchema<Resolvers: ResolversProtocol>(resolvers: Resolvers.Type) throws -> GraphQLSchema {
46+
public func buildGraphQLSchema<Resolvers: ResolversProtocol>(
47+
resolvers: Resolvers.Type,
48+
decoder: MapDecoder = .init(),
49+
) throws -> GraphQLSchema {
4750
let bar = try GraphQLObjectType(
4851
name: "Bar",
4952
description: """

0 commit comments

Comments
 (0)