|
| 1 | +@testable import GraphQL |
| 2 | +import XCTest |
| 3 | + |
| 4 | +class SchemaPrinterTests: XCTestCase { |
| 5 | + func testPrintsMinimalAST() { |
| 6 | + let ast = ScalarTypeDefinition( |
| 7 | + name: .init(value: "foo") |
| 8 | + ) |
| 9 | + XCTAssertEqual(print(ast: ast), "scalar foo") |
| 10 | + } |
| 11 | + |
| 12 | + func testPrintsKitchenSinkWithoutAlteringAST() throws { |
| 13 | + guard |
| 14 | + let url = Bundle.module.url( |
| 15 | + forResource: "schema-kitchen-sink", |
| 16 | + withExtension: "graphql" |
| 17 | + ), |
| 18 | + let kitchenSink = try? String(contentsOf: url) |
| 19 | + else { |
| 20 | + XCTFail("Could not load kitchen sink") |
| 21 | + return |
| 22 | + } |
| 23 | + |
| 24 | + let ast = try parse(source: kitchenSink, noLocation: true) |
| 25 | + |
| 26 | + let printed = print(ast: ast) |
| 27 | + let printedAST = try parse(source: printed, noLocation: true) |
| 28 | + |
| 29 | + XCTAssertEqual(printed, print(ast: printedAST)) |
| 30 | + XCTAssertEqual(printed, #""" |
| 31 | + """This is a description of the schema as a whole.""" |
| 32 | + schema { |
| 33 | + query: QueryType |
| 34 | + mutation: MutationType |
| 35 | + } |
| 36 | +
|
| 37 | + """ |
| 38 | + This is a description |
| 39 | + of the `Foo` type. |
| 40 | + """ |
| 41 | + type Foo implements Bar & Baz & Two { |
| 42 | + "Description of the `one` field." |
| 43 | + one: Type |
| 44 | + """This is a description of the `two` field.""" |
| 45 | + two( |
| 46 | + """This is a description of the `argument` argument.""" |
| 47 | + argument: InputType! |
| 48 | + ): Type |
| 49 | + """This is a description of the `three` field.""" |
| 50 | + three(argument: InputType, other: String): Int |
| 51 | + four(argument: String = "string"): String |
| 52 | + five(argument: [String] = ["string", "string"]): String |
| 53 | + six(argument: InputType = { key: "value" }): Type |
| 54 | + seven(argument: Int = null): Type |
| 55 | + eight(argument: OneOfInputType): Type |
| 56 | + } |
| 57 | +
|
| 58 | + type AnnotatedObject @onObject(arg: "value") { |
| 59 | + annotatedField(arg: Type = "default" @onArgumentDefinition): Type @onField |
| 60 | + } |
| 61 | +
|
| 62 | + type UndefinedType |
| 63 | +
|
| 64 | + extend type Foo { |
| 65 | + seven(argument: [String]): Type |
| 66 | + } |
| 67 | +
|
| 68 | + extend type Foo @onType |
| 69 | +
|
| 70 | + interface Bar { |
| 71 | + one: Type |
| 72 | + four(argument: String = "string"): String |
| 73 | + } |
| 74 | +
|
| 75 | + interface AnnotatedInterface @onInterface { |
| 76 | + annotatedField(arg: Type @onArgumentDefinition): Type @onField |
| 77 | + } |
| 78 | +
|
| 79 | + interface UndefinedInterface |
| 80 | +
|
| 81 | + extend interface Bar implements Two { |
| 82 | + two(argument: InputType!): Type |
| 83 | + } |
| 84 | +
|
| 85 | + extend interface Bar @onInterface |
| 86 | +
|
| 87 | + interface Baz implements Bar & Two { |
| 88 | + one: Type |
| 89 | + two(argument: InputType!): Type |
| 90 | + four(argument: String = "string"): String |
| 91 | + } |
| 92 | +
|
| 93 | + union Feed = Story | Article | Advert |
| 94 | +
|
| 95 | + union AnnotatedUnion @onUnion = A | B |
| 96 | +
|
| 97 | + union AnnotatedUnionTwo @onUnion = A | B |
| 98 | +
|
| 99 | + union UndefinedUnion |
| 100 | +
|
| 101 | + extend union Feed = Photo | Video |
| 102 | +
|
| 103 | + extend union Feed @onUnion |
| 104 | +
|
| 105 | + scalar CustomScalar |
| 106 | +
|
| 107 | + scalar AnnotatedScalar @onScalar |
| 108 | +
|
| 109 | + extend scalar CustomScalar @onScalar |
| 110 | +
|
| 111 | + enum Site { |
| 112 | + """This is a description of the `DESKTOP` value""" |
| 113 | + DESKTOP |
| 114 | + """This is a description of the `MOBILE` value""" |
| 115 | + MOBILE |
| 116 | + "This is a description of the `WEB` value" |
| 117 | + WEB |
| 118 | + } |
| 119 | +
|
| 120 | + enum AnnotatedEnum @onEnum { |
| 121 | + ANNOTATED_VALUE @onEnumValue |
| 122 | + OTHER_VALUE |
| 123 | + } |
| 124 | +
|
| 125 | + enum UndefinedEnum |
| 126 | +
|
| 127 | + extend enum Site { |
| 128 | + VR |
| 129 | + } |
| 130 | +
|
| 131 | + extend enum Site @onEnum |
| 132 | +
|
| 133 | + input InputType { |
| 134 | + key: String! |
| 135 | + answer: Int = 42 |
| 136 | + } |
| 137 | +
|
| 138 | + input OneOfInputType @oneOf { |
| 139 | + string: String |
| 140 | + int: Int |
| 141 | + } |
| 142 | +
|
| 143 | + input AnnotatedInput @onInputObject { |
| 144 | + annotatedField: Type @onInputFieldDefinition |
| 145 | + } |
| 146 | +
|
| 147 | + input UndefinedInput |
| 148 | +
|
| 149 | + extend input InputType { |
| 150 | + other: Float = 1.23e4 @onInputFieldDefinition |
| 151 | + } |
| 152 | +
|
| 153 | + extend input InputType @onInputObject |
| 154 | +
|
| 155 | + """This is a description of the `@skip` directive""" |
| 156 | + directive @skip( |
| 157 | + """This is a description of the `if` argument""" |
| 158 | + if: Boolean! @onArgumentDefinition |
| 159 | + ) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT |
| 160 | +
|
| 161 | + directive @include(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT |
| 162 | +
|
| 163 | + directive @include2(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT |
| 164 | +
|
| 165 | + directive @myRepeatableDir(name: String!) repeatable on OBJECT | INTERFACE |
| 166 | +
|
| 167 | + extend schema @onSchema |
| 168 | +
|
| 169 | + extend schema @onSchema { |
| 170 | + subscription: SubscriptionType |
| 171 | + } |
| 172 | + """#) |
| 173 | + } |
| 174 | +} |
0 commit comments