Skip to content

Commit 1764f8e

Browse files
committed
Add 'sortIntrospectionQuery' utility function
1 parent fd4a69c commit 1764f8e

File tree

4 files changed

+449
-0
lines changed

4 files changed

+449
-0
lines changed

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,8 @@ export {
307307
getIntrospectionQuery,
308308
// Deprecated: use getIntrospectionQuery
309309
introspectionQuery,
310+
// Sort the result of Introspection Query.
311+
sortIntrospectionQuery,
310312
// Gets the target Operation from a Document
311313
getOperationAST,
312314
// Build a GraphQLSchema from an introspection result.
Lines changed: 321 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,321 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import { describe, it } from 'mocha';
9+
import { expect } from 'chai';
10+
import dedent from '../../jsutils/dedent';
11+
import { graphqlSync } from '../../graphql';
12+
import { printSchema } from '../schemaPrinter';
13+
import { buildSchema } from '../buildASTSchema';
14+
import { buildClientSchema } from '../buildClientSchema';
15+
import { getIntrospectionQuery } from '../introspectionQuery';
16+
import { sortIntrospectionQuery } from '../sortIntrospection';
17+
18+
function sortSDL(sdl) {
19+
const schema = buildSchema(sdl);
20+
const result = graphqlSync(schema, getIntrospectionQuery());
21+
expect(result.errors).to.equal(undefined);
22+
const introspection = sortIntrospectionQuery(result.data);
23+
const sortedSchema = buildClientSchema(introspection);
24+
return printSchema(sortedSchema);
25+
}
26+
27+
describe('sortIntrospectionSchema', () => {
28+
it('sort fields', () => {
29+
const sorted = sortSDL(dedent`
30+
input Bar {
31+
barB: String
32+
barA: String
33+
barC: String
34+
}
35+
36+
interface FooInterface {
37+
fooB: String
38+
fooA: String
39+
fooC: String
40+
}
41+
42+
type FooType implements FooInterface {
43+
fooC: String
44+
fooA: String
45+
fooB: String
46+
}
47+
48+
type Query {
49+
dummy(arg: Bar): FooType
50+
}
51+
`);
52+
53+
expect(sorted).to.equal(dedent`
54+
input Bar {
55+
barA: String
56+
barB: String
57+
barC: String
58+
}
59+
60+
interface FooInterface {
61+
fooA: String
62+
fooB: String
63+
fooC: String
64+
}
65+
66+
type FooType implements FooInterface {
67+
fooA: String
68+
fooB: String
69+
fooC: String
70+
}
71+
72+
type Query {
73+
dummy(arg: Bar): FooType
74+
}
75+
`);
76+
});
77+
78+
it('sort implemented interfaces', () => {
79+
const sorted = sortSDL(dedent`
80+
interface FooA {
81+
dummy: String
82+
}
83+
84+
interface FooB {
85+
dummy: String
86+
}
87+
88+
interface FooC {
89+
dummy: String
90+
}
91+
92+
type Query implements FooB & FooA & FooC {
93+
dummy: String
94+
}
95+
`);
96+
97+
expect(sorted).to.equal(dedent`
98+
interface FooA {
99+
dummy: String
100+
}
101+
102+
interface FooB {
103+
dummy: String
104+
}
105+
106+
interface FooC {
107+
dummy: String
108+
}
109+
110+
type Query implements FooA, FooB, FooC {
111+
dummy: String
112+
}
113+
`);
114+
});
115+
116+
it('sort types in union', () => {
117+
const sorted = sortSDL(dedent`
118+
type FooA {
119+
dummy: String
120+
}
121+
122+
type FooB {
123+
dummy: String
124+
}
125+
126+
type FooC {
127+
dummy: String
128+
}
129+
130+
union FooUnion = FooB | FooA | FooC
131+
132+
type Query {
133+
dummy: FooUnion
134+
}
135+
`);
136+
137+
expect(sorted).to.equal(dedent`
138+
type FooA {
139+
dummy: String
140+
}
141+
142+
type FooB {
143+
dummy: String
144+
}
145+
146+
type FooC {
147+
dummy: String
148+
}
149+
150+
union FooUnion = FooA | FooB | FooC
151+
152+
type Query {
153+
dummy: FooUnion
154+
}
155+
`);
156+
});
157+
158+
it('sort enum values', () => {
159+
const sorted = sortSDL(dedent`
160+
enum Foo {
161+
B
162+
C
163+
A
164+
}
165+
166+
type Query {
167+
dummy: Foo
168+
}
169+
`);
170+
171+
expect(sorted).to.equal(dedent`
172+
enum Foo {
173+
A
174+
B
175+
C
176+
}
177+
178+
type Query {
179+
dummy: Foo
180+
}
181+
`);
182+
});
183+
184+
it('sort field arguments', () => {
185+
const sorted = sortSDL(dedent`
186+
type Query {
187+
dummy(argB: Int, argA: String, argC: Float): ID
188+
}
189+
`);
190+
191+
expect(sorted).to.equal(dedent`
192+
type Query {
193+
dummy(argA: String, argB: Int, argC: Float): ID
194+
}
195+
`);
196+
});
197+
198+
it('sort types', () => {
199+
const sorted = sortSDL(dedent`
200+
type Query {
201+
dummy(arg1: FooF, arg2: FooA, arg3: FooG): FooD
202+
}
203+
204+
type FooC implements FooE {
205+
dummy: String
206+
}
207+
208+
enum FooG {
209+
enumValue
210+
}
211+
212+
scalar FooA
213+
214+
input FooF {
215+
dummy: String
216+
}
217+
218+
union FooD = FooC | FooB
219+
220+
interface FooE {
221+
dummy: String
222+
}
223+
224+
type FooB {
225+
dummy: String
226+
}
227+
`);
228+
229+
expect(sorted).to.equal(dedent`
230+
scalar FooA
231+
232+
type FooB {
233+
dummy: String
234+
}
235+
236+
type FooC implements FooE {
237+
dummy: String
238+
}
239+
240+
union FooD = FooB | FooC
241+
242+
interface FooE {
243+
dummy: String
244+
}
245+
246+
input FooF {
247+
dummy: String
248+
}
249+
250+
enum FooG {
251+
enumValue
252+
}
253+
254+
type Query {
255+
dummy(arg1: FooF, arg2: FooA, arg3: FooG): FooD
256+
}
257+
`);
258+
});
259+
260+
it('sort directive arguments', () => {
261+
const sorted = sortSDL(dedent`
262+
directive @test(argC: Float, argA: String, argB: Int) on FIELD
263+
264+
type Query {
265+
dummy: String
266+
}
267+
`);
268+
269+
expect(sorted).to.equal(dedent`
270+
directive @test(argA: String, argB: Int, argC: Float) on FIELD
271+
272+
type Query {
273+
dummy: String
274+
}
275+
`);
276+
});
277+
278+
it('sort directive locations', () => {
279+
const sorted = sortSDL(dedent`
280+
directive @test(argC: Float, argA: String, argB: Int) on UNION | FIELD | ENUM
281+
282+
type Query {
283+
dummy: String
284+
}
285+
`);
286+
287+
expect(sorted).to.equal(dedent`
288+
directive @test(argA: String, argB: Int, argC: Float) on ENUM | FIELD | UNION
289+
290+
type Query {
291+
dummy: String
292+
}
293+
`);
294+
});
295+
296+
it('sort directives', () => {
297+
const sorted = sortSDL(dedent`
298+
directive @fooC on FIELD
299+
300+
directive @fooB on UNION
301+
302+
directive @fooA on ENUM
303+
304+
type Query {
305+
dummy: String
306+
}
307+
`);
308+
309+
expect(sorted).to.equal(dedent`
310+
directive @fooA on ENUM
311+
312+
directive @fooB on UNION
313+
314+
directive @fooC on FIELD
315+
316+
type Query {
317+
dummy: String
318+
}
319+
`);
320+
});
321+
});

src/utilities/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ export type {
3838
IntrospectionDirective,
3939
} from './introspectionQuery';
4040

41+
// Sort the result of Introspection Query.
42+
export { sortIntrospectionQuery } from './sortIntrospection';
43+
4144
// Gets the target Operation from a Document
4245
export { getOperationAST } from './getOperationAST';
4346

0 commit comments

Comments
 (0)