Skip to content

Commit

Permalink
test: Adds missing schema parser tests
Browse files Browse the repository at this point in the history
  • Loading branch information
NeedleInAJayStack committed Dec 25, 2023
1 parent 06d0266 commit 83b2c70
Showing 1 changed file with 197 additions and 57 deletions.
254 changes: 197 additions & 57 deletions Tests/GraphQLTests/LanguageTests/SchemaParserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -311,67 +311,59 @@ class SchemaParserTests: XCTestCase {
)
}

func testSchemeExtension() throws {
// Based on Apollo Federation example schema: https://github.com/apollographql/apollo-federation-subgraph-compatibility/blob/main/COMPATIBILITY.md#products-schema-to-be-implemented-by-library-maintainers
let source =
"""
extend schema
@link(
url: "https://specs.apollo.dev/federation/v2.0",
import: [
"@extends",
"@external",
"@key",
"@inaccessible",
"@override",
"@provides",
"@requires",
"@shareable",
"@tag"
func testSchemaExtension() throws {
XCTAssertEqual(
try parse(source: """
extend schema {
mutation: Mutation
}
"""),
Document(
definitions: [
SchemaExtensionDefinition(
definition: SchemaDefinition(
directives: [],
operationTypes: [
OperationTypeDefinition(
operation: .mutation,
type: .init(name: .init(value: "Mutation"))
),
]
)
),
]
)
"""
)
)
}

let expected = Document(
definitions: [
SchemaExtensionDefinition(
definition: SchemaDefinition(
directives: [
Directive(
name: nameNode("link"),
arguments: [
Argument(
name: nameNode("url"),
value: StringValue(
value: "https://specs.apollo.dev/federation/v2.0",
block: false
)
),
Argument(
name: nameNode("import"),
value: ListValue(values: [
StringValue(value: "@extends", block: false),
StringValue(value: "@external", block: false),
StringValue(value: "@key", block: false),
StringValue(value: "@inaccessible", block: false),
StringValue(value: "@override", block: false),
StringValue(value: "@provides", block: false),
StringValue(value: "@requires", block: false),
StringValue(value: "@shareable", block: false),
StringValue(value: "@tag", block: false),
])
),
]
),
],
operationTypes: []
)
),
]
func testSchemaExtensionWithOnlyDirectives() throws {
XCTAssertEqual(
try parse(source: "extend schema @directive"),
Document(
definitions: [
SchemaExtensionDefinition(
definition: SchemaDefinition(
directives: [
Directive(name: .init(value: "directive")),
],
operationTypes: []
)
),
]
)
)
}

let result = try parse(source: source)
XCTAssert(result == expected)
func testSchemaExtensionWithoutAnythingThrows() throws {
XCTAssertThrowsError(
try parse(source: "extend schema")
)
}

func testSchemaExtensionWithInvalidOperationTypeThrows() throws {
XCTAssertThrowsError(
try parse(source: "extend schema { unknown: SomeType }")
)
}

func testSimpleNonNullType() throws {
Expand All @@ -397,6 +389,26 @@ class SchemaParserTests: XCTestCase {
XCTAssert(result == expected)
}

func testSimpleInterfaceInheritingInterface() throws {
XCTAssertEqual(
try parse(source: "interface Hello implements World { field: String }"),
Document(
definitions: [
InterfaceTypeDefinition(
name: nameNode("Hello"),
interfaces: [typeNode("World")],
fields: [
FieldDefinition(
name: .init(value: "field"),
type: NamedType(name: .init(value: "String"))
),
]
),
]
)
)
}

func testSimpleTypeInheritingInterface() throws {
let source = "type Hello implements World { }"

Expand Down Expand Up @@ -432,6 +444,71 @@ class SchemaParserTests: XCTestCase {
XCTAssert(result == expected)
}

func testSimpleInterfaceInheritingMultipleInterfaces() throws {
XCTAssertEqual(
try parse(source: "interface Hello implements Wo & rld { field: String }"),
Document(
definitions: [
InterfaceTypeDefinition(
name: nameNode("Hello"),
interfaces: [
typeNode("Wo"),
typeNode("rld"),
],
fields: [
FieldDefinition(
name: .init(value: "field"),
type: NamedType(name: .init(value: "String"))
),
]
),
]
)
)
}

func testSimpleTypeInheritingMultipleInterfacesWithLeadingAmbersand() throws {
let source = "type Hello implements & Wo & rld { }"

let expected = Document(
definitions: [
ObjectTypeDefinition(
name: nameNode("Hello"),
interfaces: [
typeNode("Wo"),
typeNode("rld"),
]
),
]
)

let result = try parse(source: source)
XCTAssert(result == expected)
}

func testSimpleInterfaceInheritingMultipleInterfacesWithLeadingAmbersand() throws {
XCTAssertEqual(
try parse(source: "interface Hello implements & Wo & rld { field: String }"),
Document(
definitions: [
InterfaceTypeDefinition(
name: nameNode("Hello"),
interfaces: [
typeNode("Wo"),
typeNode("rld"),
],
fields: [
FieldDefinition(
name: .init(value: "field"),
type: NamedType(name: .init(value: "String"))
),
]
),
]
)
)
}

func testSingleValueEnum() throws {
let source = "enum Hello { WORLD }"

Expand Down Expand Up @@ -1310,4 +1387,67 @@ class SchemaParserTests: XCTestCase {

_ = try parse(source: kitchenSink)
}

func testSchemeExtension() throws {
// Based on Apollo Federation example schema: https://github.com/apollographql/apollo-federation-subgraph-compatibility/blob/main/COMPATIBILITY.md#products-schema-to-be-implemented-by-library-maintainers
let source =
"""
extend schema
@link(
url: "https://specs.apollo.dev/federation/v2.0",
import: [
"@extends",
"@external",
"@key",
"@inaccessible",
"@override",
"@provides",
"@requires",
"@shareable",
"@tag"
]
)
"""

let expected = Document(
definitions: [
SchemaExtensionDefinition(
definition: SchemaDefinition(
directives: [
Directive(
name: nameNode("link"),
arguments: [
Argument(
name: nameNode("url"),
value: StringValue(
value: "https://specs.apollo.dev/federation/v2.0",
block: false
)
),
Argument(
name: nameNode("import"),
value: ListValue(values: [
StringValue(value: "@extends", block: false),
StringValue(value: "@external", block: false),
StringValue(value: "@key", block: false),
StringValue(value: "@inaccessible", block: false),
StringValue(value: "@override", block: false),
StringValue(value: "@provides", block: false),
StringValue(value: "@requires", block: false),
StringValue(value: "@shareable", block: false),
StringValue(value: "@tag", block: false),
])
),
]
),
],
operationTypes: []
)
),
]
)

let result = try parse(source: source)
XCTAssert(result == expected)
}
}

0 comments on commit 83b2c70

Please sign in to comment.