|
| 1 | +@testable import GraphQL |
| 2 | +import XCTest |
| 3 | + |
| 4 | +class GraphQLSchemaTests: XCTestCase { |
| 5 | + |
| 6 | + func testAssertObjectImplementsInterfacePassesWhenObjectFieldHasRequiredArgumentsFromInterface() throws { |
| 7 | + let interface = try GraphQLInterfaceType( |
| 8 | + name: "Interface", |
| 9 | + fields: [ |
| 10 | + "fieldWithNoArg": GraphQLField( |
| 11 | + type: GraphQLInt, |
| 12 | + args: [:] |
| 13 | + ), |
| 14 | + "fieldWithOneArg": GraphQLField( |
| 15 | + type: GraphQLInt, |
| 16 | + args: ["requiredArg": GraphQLArgument(type: GraphQLString)] |
| 17 | + ), |
| 18 | + "fieldWithMultipleArgs": GraphQLField( |
| 19 | + type: GraphQLInt, |
| 20 | + args: [ |
| 21 | + "arg1": GraphQLArgument(type: GraphQLString), |
| 22 | + "arg2": GraphQLArgument(type: GraphQLNonNull(GraphQLInt)), |
| 23 | + "arg3": GraphQLArgument(type: GraphQLNonNull(GraphQLBoolean)) |
| 24 | + ] |
| 25 | + ), |
| 26 | + ] |
| 27 | + ) |
| 28 | + |
| 29 | + let object = try GraphQLObjectType( |
| 30 | + name: "Object", |
| 31 | + fields: [ |
| 32 | + "fieldWithNoArg": GraphQLField( |
| 33 | + type: GraphQLInt, |
| 34 | + args: [:] |
| 35 | + ), |
| 36 | + "fieldWithOneArg": GraphQLField( |
| 37 | + type: GraphQLInt, |
| 38 | + args: ["requiredArg": GraphQLArgument(type: GraphQLString)] |
| 39 | + ), |
| 40 | + "fieldWithMultipleArgs": GraphQLField( |
| 41 | + type: GraphQLInt, |
| 42 | + args: [ |
| 43 | + "arg1": GraphQLArgument(type: GraphQLString), |
| 44 | + "arg2": GraphQLArgument(type: GraphQLNonNull(GraphQLInt)), |
| 45 | + "arg3": GraphQLArgument(type: GraphQLNonNull(GraphQLBoolean)) |
| 46 | + ] |
| 47 | + ), |
| 48 | + ], |
| 49 | + interfaces: [interface], |
| 50 | + isTypeOf: { (_, _, _) -> Bool in |
| 51 | + preconditionFailure("Should not be called") |
| 52 | + } |
| 53 | + ) |
| 54 | + |
| 55 | + _ = try GraphQLSchema(query: object, types: [interface, object]) |
| 56 | + } |
| 57 | + |
| 58 | + func testAssertObjectImplementsInterfacePassesWhenObjectFieldHasRequiredArgumentMissingInInterfaceButHasDefaultValue() throws { |
| 59 | + let interface = try GraphQLInterfaceType( |
| 60 | + name: "Interface", |
| 61 | + fields: [ |
| 62 | + "fieldWithOneArg": GraphQLField( |
| 63 | + type: GraphQLInt, |
| 64 | + args: [:] |
| 65 | + ), |
| 66 | + ] |
| 67 | + ) |
| 68 | + |
| 69 | + let object = try GraphQLObjectType( |
| 70 | + name: "Object", |
| 71 | + fields: [ |
| 72 | + "fieldWithOneArg": GraphQLField( |
| 73 | + type: GraphQLInt, |
| 74 | + args: [ |
| 75 | + "addedRequiredArgWithDefaultValue": GraphQLArgument( |
| 76 | + type: GraphQLNonNull(GraphQLInt), |
| 77 | + defaultValue: .int(5) |
| 78 | + ) |
| 79 | + ] |
| 80 | + ), |
| 81 | + ], |
| 82 | + interfaces: [interface], |
| 83 | + isTypeOf: { (_, _, _) -> Bool in |
| 84 | + preconditionFailure("Should not be called") |
| 85 | + } |
| 86 | + ) |
| 87 | + |
| 88 | + _ = try GraphQLSchema(query: object, types: [interface, object]) |
| 89 | + } |
| 90 | + |
| 91 | + func testAssertObjectImplementsInterfacePassesWhenObjectFieldHasNullableArgumentMissingInInterface() throws { |
| 92 | + let interface = try GraphQLInterfaceType( |
| 93 | + name: "Interface", |
| 94 | + fields: [ |
| 95 | + "fieldWithOneArg": GraphQLField( |
| 96 | + type: GraphQLInt, |
| 97 | + args: [:] |
| 98 | + ), |
| 99 | + ] |
| 100 | + ) |
| 101 | + |
| 102 | + let object = try GraphQLObjectType( |
| 103 | + name: "Object", |
| 104 | + fields: [ |
| 105 | + "fieldWithOneArg": GraphQLField( |
| 106 | + type: GraphQLInt, |
| 107 | + args: ["addedNullableArg": GraphQLArgument(type: GraphQLInt)] |
| 108 | + ), |
| 109 | + ], |
| 110 | + interfaces: [interface], |
| 111 | + isTypeOf: { (_, _, _) -> Bool in |
| 112 | + preconditionFailure("Should not be called") |
| 113 | + } |
| 114 | + ) |
| 115 | + |
| 116 | + _ = try GraphQLSchema(query: object, types: [interface, object]) |
| 117 | + } |
| 118 | + |
| 119 | + func testAssertObjectImplementsInterfaceFailsWhenObjectFieldHasRequiredArgumentMissingInInterface() throws { |
| 120 | + let interface = try GraphQLInterfaceType( |
| 121 | + name: "Interface", |
| 122 | + fields: [ |
| 123 | + "fieldWithoutArg": GraphQLField( |
| 124 | + type: GraphQLInt, |
| 125 | + args: [:] |
| 126 | + ), |
| 127 | + ] |
| 128 | + ) |
| 129 | + |
| 130 | + let object = try GraphQLObjectType( |
| 131 | + name: "Object", |
| 132 | + fields: [ |
| 133 | + "fieldWithoutArg": GraphQLField( |
| 134 | + type: GraphQLInt, |
| 135 | + args: [ |
| 136 | + "addedRequiredArg": GraphQLArgument(type: GraphQLNonNull(GraphQLInt)) |
| 137 | + ] |
| 138 | + ), |
| 139 | + ], |
| 140 | + interfaces: [interface], |
| 141 | + isTypeOf: { (_, _, _) -> Bool in |
| 142 | + preconditionFailure("Should not be called") |
| 143 | + } |
| 144 | + ) |
| 145 | + |
| 146 | + do { |
| 147 | + _ = try GraphQLSchema(query: object, types: [interface, object]) |
| 148 | + XCTFail("Expected errors when creating schema") |
| 149 | + } catch { |
| 150 | + let graphQLError = try XCTUnwrap(error as? GraphQLError) |
| 151 | + XCTAssertEqual(graphQLError.message, "Object.fieldWithoutArg includes required argument (addedRequiredArg:) that is missing from the Interface field Interface.fieldWithoutArg.") |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments