The best way to control Claude Code, Codex and more from any computer or phone

main
ts 289 lines 12.1 KB
Raw
1 import * as Validate from "@effect/graphql-generator/internal/Validate"
2 import { assert, describe, it } from "@effect/vitest"
3 import { parseOrThrow, source } from "./utils/ast.ts"
4 import { readSdl } from "./utils/model.ts"
5
6 const schema = readSdl(`
7 interface Node { id: ID! }
8 interface Orphan { id: ID! }
9 type User implements Node { id: ID! name: String! friends(first: Int!): [User!]! }
10 type Dog implements Node { id: ID! }
11 type Query { user(id: ID!): User orphan: Orphan }
12 `)
13
14 const validateWith = (schema: ReturnType<typeof readSdl>) => (files: Record<string, string>): ReadonlyArray<string> =>
15 Validate.validate(
16 schema,
17 Object.entries(files).map(([path, body]) => ({ source: source(body, path), document: parseOrThrow(body, path) }))
18 ).map(({ column, line, message, path }) => `${path}:${line}:${column} ${message}`)
19
20 const validate = validateWith(schema)
21
22 describe("Validate", () => {
23 it("fields exist on their parent type", () => {
24 assert.deepStrictEqual(validate({ "q.graphql": "query Q { user(id: \"1\") { homepage } }" }), [
25 "q.graphql:1:27 Cannot query field \"homepage\" on type \"User\"."
26 ])
27 })
28
29 it("arguments exist and required arguments are provided", () => {
30 assert.deepStrictEqual(
31 validate({ "q.graphql": "query Q { user(id: \"1\", locale: \"en\") { friends { id } } }" }),
32 [
33 "q.graphql:1:25 Unknown argument \"locale\" on field \"Query.user\".",
34 "q.graphql:1:41 Field \"friends\" argument \"first\" of type \"Int!\" is required, but it was not provided."
35 ]
36 )
37 })
38
39 it("variables are defined, used and of input types", () => {
40 assert.deepStrictEqual(
41 validate({ "q.graphql": "query Q($unused: ID, $u: User) { user(id: $u) { friends(first: $n) { id } } }" }),
42 [
43 "q.graphql:1:9 Variable \"$unused\" is never used in operation \"Q\".",
44 "q.graphql:1:26 Variable \"$u\" cannot be non-input type \"User\".",
45 "q.graphql:1:64 Variable \"$n\" is not defined by operation \"Q\"."
46 ]
47 )
48 })
49
50 it("fragments are defined and used", () => {
51 assert.deepStrictEqual(
52 validate({ "q.graphql": "query Q { user(id: \"1\") { ...Missing } } fragment Unused on User { id }" }),
53 [
54 "q.graphql:1:30 Unknown fragment \"Missing\".",
55 "q.graphql:1:42 Fragment \"Unused\" is never used."
56 ]
57 )
58 })
59
60 it("fragments are spread onto a possible type", () => {
61 assert.deepStrictEqual(validate({ "q.graphql": "query Q { user(id: \"1\") { ... on Dog { id } } }" }), [
62 "q.graphql:1:27 Fragment cannot be spread here as objects of type \"User\" can never be of type \"Dog\"."
63 ])
64 })
65
66 it("a composite type overlaps itself, even with no possible types", () => {
67 assert.deepStrictEqual(
68 validate({
69 "q.graphql": "query Q { orphan { ... on Orphan { id } ...OrphanId } } fragment OrphanId on Orphan { id }"
70 }),
71 []
72 )
73 })
74
75 it("operations are named and operation names are unique", () => {
76 assert.deepStrictEqual(
77 validate({ "q.graphql": "{ orphan { id } } query Q { orphan { id } } query Q { orphan { id } }" }),
78 [
79 "q.graphql:1:1 Anonymous operations are not supported, name this operation.",
80 "q.graphql:1:25 There can be only one operation named \"Q\".",
81 "q.graphql:1:51 There can be only one operation named \"Q\"."
82 ]
83 )
84 })
85
86 it("operations and fragments share one name namespace across files", () => {
87 assert.deepStrictEqual(
88 validate({
89 "a.graphql": "query A { user(id: \"1\") { ...Friends } } fragment Friends on User { id }",
90 "b.graphql": "query Friends { orphan { id } }"
91 }),
92 [
93 "a.graphql:1:51 There can be only one operation or fragment named \"Friends\".",
94 "b.graphql:1:7 There can be only one operation or fragment named \"Friends\"."
95 ]
96 )
97 })
98
99 it("leaf fields have no selection set and composite fields have one", () => {
100 assert.deepStrictEqual(
101 validate({ "q.graphql": "query Q { user(id: \"1\") { id { name } friends(first: 1) } }" }),
102 [
103 "q.graphql:1:30 Field \"id\" must not have a selection since type \"ID!\" has no subfields.",
104 "q.graphql:1:39 Field \"friends\" of type \"[User!]!\" must have a selection of subfields. Did you mean \"friends { ... }\"?"
105 ]
106 )
107 })
108
109 it("fields sharing a response key are the same field with the same arguments", () => {
110 assert.deepStrictEqual(
111 validate({
112 "q.graphql": "query Q { user(id: \"1\") { name: id name friends(first: 1) { id } friends(first: 2) { id } } }"
113 }),
114 [
115 "q.graphql:1:36 Fields \"name\" conflict because \"id\" and \"name\" are different fields. Use different aliases on the fields to fetch both if this was intentional.",
116 "q.graphql:1:66 Fields \"friends\" conflict because they have differing arguments. Use different aliases on the fields to fetch both if this was intentional."
117 ]
118 )
119 })
120
121 it("fragments and the variables they use resolve across files", () => {
122 assert.deepStrictEqual(
123 validate({
124 "viewer.graphql": "query Viewer($id: ID!) { ...ViewerUser }",
125 "fragments.graphql": "fragment ViewerUser on Query { user(id: $id) { name } }"
126 }),
127 []
128 )
129 })
130
131 it("a diagnostic is reported in the file holding the offending node", () => {
132 assert.deepStrictEqual(
133 validate({
134 "viewer.graphql": "query Viewer { user(id: \"1\") { ...A } ...ViewerUser } fragment B on User { ...A }",
135 "fragments.graphql": "fragment ViewerUser on Query { user(id: $id) { id } } fragment A on User { ...B }"
136 }),
137 [
138 "viewer.graphql:1:76 Cannot spread fragment \"B\" within itself via \"A\".",
139 "fragments.graphql:1:32 Fields \"user\" conflict because they have differing arguments. Use different aliases on the fields to fetch both if this was intentional.",
140 "fragments.graphql:1:41 Variable \"$id\" is not defined by operation \"Viewer\"."
141 ]
142 )
143 })
144 })
145
146 const validateValues = validateWith(readSdl(`
147 scalar Date
148 interface Node { id: ID! }
149 type User implements Node { id: ID! name: String! email: String friends(first: Int!): [User!]! }
150 type Dog implements Node { id: ID! name: String }
151 enum Role { ADMIN USER }
152 input UserInput { name: String! role: Role }
153 input By @oneOf { id: ID name: String }
154 type Query {
155 user(id: ID!): User
156 node(id: ID!): Node
157 users(first: Int, role: Role): [User!]
158 find(by: By!): User
159 at(date: Date): Int
160 }
161 type Mutation { createUser(input: UserInput!): User }
162 `))
163
164 describe("Validate: positions, merging, directives and values", () => {
165 it("a variable is used only where its type is allowed; a default lets a nullable variable fill a non-null position", () => {
166 assert.deepStrictEqual(
167 validateValues({
168 "q.graphql": [
169 `query A($id: ID) { user(id: $id) { id } }`,
170 `query B($b: Boolean) { user(id: "1") { name @include(if: $b) } }`,
171 `query C($id: ID = "1") { user(id: $id) { id } }`
172 ].join("\n")
173 }),
174 [
175 `q.graphql:1:29 Variable "$id" of type "ID" used in position expecting type "ID!".`,
176 `q.graphql:2:58 Variable "$b" of type "Boolean" used in position expecting type "Boolean!".`
177 ]
178 )
179 })
180
181 it("a subscription selects exactly one root field", () => {
182 const validateSubscriptions = validateWith(readSdl(`type Query { a: Int } type Subscription { x: Int y: Int }`))
183 assert.deepStrictEqual(validateSubscriptions({ "q.graphql": `subscription S { x y }` }), [
184 `q.graphql:1:20 Subscription "S" must select only one top level field.`
185 ])
186 })
187
188 it("a variable in a literal coerced to a list is checked against the list's item type, through nested lists", () => {
189 const validateLists = validateWith(
190 readSdl(`input I { id: ID! } type Query { many(inputs: [I!]!): Int nested(inputs: [[I!]]): Int }`)
191 )
192 assert.deepStrictEqual(
193 validateLists({
194 "q.graphql": [
195 `query Q($id: ID) { many(inputs: { id: $id }) }`,
196 `query R($id: ID) { nested(inputs: { id: $id }) }`
197 ].join("\n")
198 }),
199 [
200 `q.graphql:1:39 Variable "$id" of type "ID" used in position expecting type "ID!".`,
201 `q.graphql:2:41 Variable "$id" of type "ID" used in position expecting type "ID!".`
202 ]
203 )
204 })
205
206 it("fields sharing a response key are compared through fragments, inline fragments and subselections", () => {
207 assert.deepStrictEqual(
208 validateValues({
209 "q.graphql": [
210 `query Q { user(id: "1") { ...A ...B } }`,
211 `fragment A on User { x: name }`,
212 `fragment B on User { x: email }`,
213 `query R { user(id: "1") { friends(first: 1) { a: name } friends(first: 1) { a: email } } }`,
214 `query S { node(id: "1") { ... on User { v: name } ... on Dog { v: name } } }`,
215 `query T { node(id: "1") { ... on User { v: id } ... on Dog { v: id } } }`
216 ].join("\n")
217 }),
218 [
219 `q.graphql:3:22 Fields "x" conflict because "name" and "email" are different fields. Use different aliases on the fields to fetch both if this was intentional.`,
220 `q.graphql:4:57 Fields "friends" conflict because subfields "a" conflict because "name" and "email" are different fields. Use different aliases on the fields to fetch both if this was intentional.`,
221 `q.graphql:5:64 Fields "v" conflict because they return conflicting types "String!" and "String". Use different aliases on the fields to fetch both if this was intentional.`
222 ]
223 )
224 })
225
226 it("@skip and @include need an if: Boolean!, appear once per location and only on fields and fragments; other directives are tolerated", () => {
227 assert.deepStrictEqual(
228 validateValues({
229 "q.graphql": [
230 `query Q($s: String!) { user(id: "1") { a: name @include b: email @skip(if: $s) c: id @skip(if: false) @skip(if: true) d: name @cached } }`,
231 `query R @skip(if: false) { user(id: "1") { name } }`
232 ].join("\n")
233 }),
234 [
235 `q.graphql:1:48 Directive "@include" argument "if" of type "Boolean!" is required, but it was not provided.`,
236 `q.graphql:1:76 Variable "$s" of type "String!" used in position expecting type "Boolean!".`,
237 `q.graphql:1:103 The directive "@skip" can only be used once at this location.`,
238 `q.graphql:2:9 Directive "@skip" may not be used on QUERY.`
239 ]
240 )
241 })
242
243 it("literal arguments and variable defaults match their input types; custom scalars take any literal and ID takes an Int", () => {
244 assert.deepStrictEqual(
245 validateValues({
246 "q.graphql": [
247 `query Q($r: Role = "ADMIN") {`,
248 ` a: users(first: 1.5) { id }`,
249 ` b: users(role: SUPER) { id }`,
250 ` c: user(id: null) { id }`,
251 ` d: find(by: { id: "1", name: "x" }) { id }`,
252 ` e: user(id: 1) { id }`,
253 ` f: at(date: "2026-10-12")`,
254 ` g: users(role: $r) { id }`,
255 `}`,
256 `mutation M {`,
257 ` a: createUser(input: { role: ADMIN }) { id }`,
258 ` b: createUser(input: { name: "a", bogus: 1 }) { id }`,
259 `}`
260 ].join("\n")
261 }),
262 [
263 `q.graphql:1:20 Enum "Role" cannot represent non-enum value: "ADMIN".`,
264 `q.graphql:2:19 Int cannot represent non-integer value: 1.5`,
265 `q.graphql:3:18 Value "SUPER" does not exist in "Role" enum.`,
266 `q.graphql:4:15 Expected value of type "ID!", found null.`,
267 `q.graphql:5:15 OneOf Input Object "By" must specify exactly one key.`,
268 `q.graphql:11:24 Field "UserInput.name" of required type "String!" was not provided.`,
269 `q.graphql:12:37 Field "bogus" is not defined by type "UserInput".`
270 ]
271 )
272 })
273
274 it("variable, argument and input field names are unique", () => {
275 assert.deepStrictEqual(
276 validateValues({
277 "q.graphql": [
278 `query Q($a: ID!, $a: ID!) { user(id: $a, id: "2") { id } }`,
279 `mutation M { createUser(input: { name: "a", name: "b" }) { id } }`
280 ].join("\n")
281 }),
282 [
283 `q.graphql:1:18 There can be only one variable named "$a".`,
284 `q.graphql:1:42 There can be only one argument named "id".`,
285 `q.graphql:2:45 There can be only one input field named "name".`
286 ]
287 )
288 })
289 })