Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes default value introspection #151

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion Sources/GraphQL/Type/Introspection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,16 @@ let __InputValue = try! GraphQLObjectType(
return nil
}

return .string(defaultValue.description)
if defaultValue == .null || defaultValue == .undefined {
return defaultValue
}

guard let literal = try astFromValue(value: defaultValue, type: inputValue.type)
else {
throw GraphQLError(message: "Invalid default value")
}

return .string(print(ast: literal))
}
),
]
Expand Down
5 changes: 5 additions & 0 deletions Sources/GraphQL/Utilities/ASTFromValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ func astFromValue(
}

// Others serialize based on their corresponding scalar types.
if case let .bool(bool) = serialized {
return BooleanValue(value: bool)
}

// JavaScript numbers can be Int or Float values.
if case let .number(number) = serialized {
switch number.storageType {
case .bool:
Expand Down
274 changes: 274 additions & 0 deletions Tests/GraphQLTests/TypeTests/IntrospectionTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,274 @@
@testable import GraphQL
import NIO
import XCTest

class IntrospectionTests: XCTestCase {
private var eventLoopGroup: EventLoopGroup!

override func setUp() {
eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: System.coreCount)
}

override func tearDown() {
XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully())
}

func testDefaultValues() throws {
let numEnum = try GraphQLEnumType(
name: "Enum",
values: [
"One": .init(value: "One"),
"Two": .init(value: "Two"),
]
)
let inputObject = try GraphQLInputObjectType(
name: "InputObject",
fields: ["str": .init(type: GraphQLString)]
)
let outputObject = try GraphQLObjectType(
name: "Object",
fields: ["str": .init(type: GraphQLString)]
)

let query = try GraphQLObjectType(
name: "Query",
fields: [
"bool": .init(
type: GraphQLBoolean,
args: [
"bool": .init(
type: GraphQLBoolean,
defaultValue: true
),
]
),
"enum": .init(
type: numEnum,
args: [
"enum": .init(
type: numEnum,
defaultValue: "One"
),
]
),
"float": .init(
type: GraphQLFloat,
args: [
"float": .init(
type: GraphQLFloat,
defaultValue: 2.2
),
]
),
"id": .init(
type: GraphQLID,
args: [
"id": .init(
type: GraphQLID,
defaultValue: "5"
),
]
),
"int": .init(
type: GraphQLInt,
args: [
"int": .init(
type: GraphQLInt,
defaultValue: 5
),
]
),
"list": .init(
type: GraphQLList(GraphQLInt),
args: [
"list": .init(
type: GraphQLList(GraphQLInt),
defaultValue: [1, 2, 3]
),
]
),
"object": .init(
type: outputObject,
args: [
"input": .init(
type: inputObject,
defaultValue: ["str": "hello"]
),
]
),
"string": .init(
type: GraphQLString,
args: [
"string": .init(
type: GraphQLString,
defaultValue: "hello"
),
]
),
]
)

let schema = try GraphQLSchema(query: query, types: [inputObject, outputObject])

let introspection = try graphql(
schema: schema,
request: """
query IntrospectionTypeQuery {
__schema {
types {
fields {
args {
defaultValue
name
type {
name
}
}
name
type {
name
}
}
name
}
}
}
""",
eventLoopGroup: eventLoopGroup
).wait()

let queryType = try XCTUnwrap(
introspection.data?["__schema"]["types"].array?
.find { $0["name"] == "Query" }
)

XCTAssertEqual(
queryType,
[
"fields": [
[
"args": [
[
"defaultValue": "true",
"name": "bool",
"type": [
"name": "Boolean",
],
],
],
"name": "bool",
"type": [
"name": "Boolean",
],
],
[
"args": [
[
"defaultValue": "One",
"name": "enum",
"type": [
"name": "Enum",
],
],
],
"name": "enum",
"type": [
"name": "Enum",
],
],
[
"args": [
[
"defaultValue": "2.2",
"name": "float",
"type": [
"name": "Float",
],
],
],
"name": "float",
"type": [
"name": "Float",
],
],
[
"args": [
[
"defaultValue": "5",
"name": "id",
"type": [
"name": "ID",
],
],
],
"name": "id",
"type": [
"name": "ID",
],
],
[
"args": [
[
"defaultValue": "5",
"name": "int",
"type": [
"name": "Int",
],
],
],
"name": "int",
"type": [
"name": "Int",
],
],
[
"args": [
[
"defaultValue": "[1, 2, 3]",
"name": "list",
"type": [
"name": .null,
],
],
],
"name": "list",
"type": [
"name": .null,
],
],
[
"args": [
[
"defaultValue": "{ str: \"hello\" }",
"name": "input",
"type": [
"name": "InputObject",
],
],
],
"name": "object",
"type": [
"name": "Object",
],
],
[
"args": [
[
"defaultValue": "\"hello\"",
"name": "string",
"type": [
"name": "String",
],
],
],
"name": "string",
"type": [
"name": "String",
],
],
],
"name": "Query",
]
)
}
}
Loading