Skip to content

Commit 3e38de3

Browse files
zubkov7zubkov7ardatan
authored
Bump GraphQL Tools for Schema Sorting and the fix for the schema definition handling (#6625)
* transformSchemaAST sort schema support * #6624 - transformSchemaAST add test * feat: sort the schema if sort option is provided * Another changeset Co-authored-by: zubkov7 <[email protected]> Co-authored-by: Arda TANRIKULU <[email protected]>
1 parent cdb9387 commit 3e38de3

File tree

8 files changed

+93
-73
lines changed

8 files changed

+93
-73
lines changed

.changeset/heavy-tables-float.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-codegen/cli': patch
3+
---
4+
5+
fix: handle convertExtensions properly with schema definitions

.changeset/quick-candles-suffer.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@graphql-codegen/cli': minor
3+
'@graphql-codegen/schema-ast': minor
4+
---
5+
6+
feat: sort the schema if "sort" option is provided

examples/programmatic-typescript/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"@graphql-codegen/typescript-operations": "*",
1515
"@graphql-codegen/typescript-resolvers": "*",
1616
"@graphql-tools/graphql-file-loader": "^7.0.1",
17-
"@graphql-tools/load": "^7.1.8",
17+
"@graphql-tools/load": "^7.3.0",
1818
"@graphql-tools/schema": "^8.1.2",
1919
"graphql": "^15.5.1",
2020
"graphql-tag": "^2.12.5",

packages/graphql-codegen-cli/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
"@graphql-tools/github-loader": "^7.0.5",
4949
"@graphql-tools/graphql-file-loader": "^7.0.5",
5050
"@graphql-tools/json-file-loader": "^7.1.2",
51-
"@graphql-tools/load": "^7.1.8",
51+
"@graphql-tools/load": "^7.3.0",
5252
"@graphql-tools/prisma-loader": "^7.0.6",
5353
"@graphql-tools/url-loader": "^7.0.11",
5454
"@graphql-tools/utils": "^8.1.1",

packages/graphql-codegen-cli/tests/codegen.spec.ts

+50
Original file line numberDiff line numberDiff line change
@@ -1011,4 +1011,54 @@ describe('Codegen Executor', () => {
10111011
expect(output.length).toBe(1);
10121012
expect(output[0].content).toContain('Hello world!');
10131013
});
1014+
1015+
it('Should sort the input schema', async () => {
1016+
const nonSortedSchema = /* GraphQL */ `
1017+
type Query {
1018+
d: String
1019+
z: String
1020+
a: String
1021+
}
1022+
1023+
type User {
1024+
aa: String
1025+
a: String
1026+
}
1027+
1028+
type A {
1029+
s: String
1030+
b: String
1031+
}
1032+
`;
1033+
const output = await executeCodegen({
1034+
schema: [nonSortedSchema],
1035+
generates: {
1036+
'out1.graphql': {
1037+
plugins: ['schema-ast'],
1038+
},
1039+
},
1040+
config: {
1041+
sort: true,
1042+
},
1043+
});
1044+
1045+
expect(output.length).toBe(1);
1046+
expect(output[0].content).toBeSimilarStringTo(/* GraphQL */ `
1047+
type A {
1048+
b: String
1049+
s: String
1050+
}
1051+
1052+
type Query {
1053+
a: String
1054+
d: String
1055+
z: String
1056+
}
1057+
1058+
type User {
1059+
a: String
1060+
aa: String
1061+
}
1062+
`);
1063+
});
10141064
});

packages/plugins/other/schema-ast/src/index.ts

+12-15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { GraphQLSchema, lexicographicSortSchema, printSchema, visit, buildASTSchema } from 'graphql';
1+
import { GraphQLSchema, printSchema, visit, buildASTSchema, print } from 'graphql';
22
import {
33
PluginFunction,
44
PluginValidateFn,
@@ -7,7 +7,6 @@ import {
77
getCachedDocumentNodeFromSchema,
88
} from '@graphql-codegen/plugin-helpers';
99
import { extname } from 'path';
10-
import { printSchemaWithDirectives } from '@graphql-tools/utils';
1110

1211
/**
1312
* @description This plugin prints the merged schema as string. If multiple schemas are provided, they will be merged and printed as one schema.
@@ -59,14 +58,13 @@ export const plugin: PluginFunction<SchemaASTConfig> = async (
5958
_documents,
6059
{ commentDescriptions = false, includeDirectives = false, sort = false, federation }
6160
): Promise<string> => {
62-
let outputSchema = federation ? removeFederation(schema) : schema;
63-
outputSchema = sort ? lexicographicSortSchema(outputSchema) : outputSchema;
61+
const transformedSchemaAndAst = transformSchemaAST(schema, { sort, federation });
6462

6563
if (includeDirectives) {
66-
return printSchemaWithDirectives(outputSchema);
64+
return print(transformedSchemaAndAst.ast);
6765
}
6866

69-
return printSchema(outputSchema, { commentDescriptions: commentDescriptions });
67+
return printSchema(transformedSchemaAndAst.schema, { commentDescriptions: commentDescriptions });
7068
};
7169

7270
export const validate: PluginValidateFn<any> = async (
@@ -84,21 +82,20 @@ export const validate: PluginValidateFn<any> = async (
8482
};
8583

8684
export function transformSchemaAST(schema: GraphQLSchema, config: { [key: string]: any }) {
87-
const astNode = getCachedDocumentNodeFromSchema(schema);
88-
89-
const transformedAST = config.disableDescriptions
90-
? visit(astNode, {
85+
schema = config.federation ? removeFederation(schema) : schema;
86+
let ast = getCachedDocumentNodeFromSchema(schema);
87+
ast = config.disableDescriptions
88+
? visit(ast, {
9189
leave: node => ({
9290
...node,
9391
description: undefined,
9492
}),
9593
})
96-
: astNode;
97-
98-
const transformedSchema = config.disableDescriptions ? buildASTSchema(transformedAST) : schema;
94+
: ast;
95+
schema = config.disableDescriptions ? buildASTSchema(ast) : schema;
9996

10097
return {
101-
schema: transformedSchema,
102-
ast: transformedAST,
98+
schema,
99+
ast,
103100
};
104101
}

packages/plugins/other/schema-ast/tests/schema-ast.spec.ts

+4-42
Original file line numberDiff line numberDiff line change
@@ -8,44 +8,6 @@ import { codegen } from '@graphql-codegen/core';
88
const SHOULD_THROW_ERROR = 'SHOULD_THROW_ERROR';
99

1010
describe('Schema AST', () => {
11-
describe('Issues', () => {
12-
it('#4919 - should support sorting the schema', async () => {
13-
const schema = buildSchema(/* GraphQL */ `
14-
type Query {
15-
d: String
16-
z: String
17-
a: String
18-
}
19-
20-
type User {
21-
aa: String
22-
a: String
23-
}
24-
25-
type A {
26-
s: String
27-
b: String
28-
}
29-
`);
30-
const content = await plugin(schema, [], { sort: true });
31-
expect(content).toBeSimilarStringTo(`
32-
type A {
33-
b: String
34-
s: String
35-
}
36-
37-
type Query {
38-
a: String
39-
d: String
40-
z: String
41-
}
42-
43-
type User {
44-
a: String
45-
aa: String
46-
}`);
47-
});
48-
});
4911
describe('Validation', () => {
5012
it('Should enforce graphql extension when its the only plugin', async () => {
5113
const fileName = 'output.ts';
@@ -164,7 +126,7 @@ describe('Schema AST', () => {
164126
const content = await plugin(schema, [], { includeDirectives: true });
165127

166128
expect(content).toBeSimilarStringTo(`
167-
directive @modify(limit: Int) on FIELD_DEFINITION
129+
directive @modify(limit: Int) on FIELD_DEFINITION
168130
`);
169131
expect(content).toBeSimilarStringTo(`
170132
type Query {
@@ -231,14 +193,14 @@ describe('Schema AST', () => {
231193
id: ID
232194
side: String
233195
}
234-
196+
235197
type Droid {
236198
id: ID
237199
model: String
238200
}
239-
201+
240202
union People = Character | Jedi | Droid
241-
203+
242204
type Query {
243205
allPeople: [People]
244206
}

yarn.lock

+14-14
Original file line numberDiff line numberDiff line change
@@ -2093,13 +2093,13 @@
20932093
unixify "1.0.0"
20942094
valid-url "1.0.9"
20952095

2096-
"@graphql-tools/load@^7.1.0", "@graphql-tools/load@^7.1.8":
2097-
version "7.1.8"
2098-
resolved "https://registry.yarnpkg.com/@graphql-tools/load/-/load-7.1.8.tgz#c6f2ce19e7497d2f56d572d4fe2aded01a87edf1"
2099-
integrity sha512-dVl2jJon9VL0qLTC98hJH4CkQ/oat6j9TouCk69ezzWHFxiPlz6tF78BzLr86Mz+bY6QCGeNIJ75Ovyn7EutCQ==
2096+
"@graphql-tools/load@^7.1.0", "@graphql-tools/load@^7.3.0":
2097+
version "7.3.0"
2098+
resolved "https://registry.yarnpkg.com/@graphql-tools/load/-/load-7.3.0.tgz#dc4177bb4b7ae537c833a2fcd97ab07b6c789c65"
2099+
integrity sha512-ZVipT7yzOpf/DJ2sLI3xGwnULVFp/icu7RFEgDo2ZX0WHiS7EjWZ0cegxEm87+WN4fMwpiysRLzWx67VIHwKGA==
21002100
dependencies:
2101-
"@graphql-tools/schema" "8.1.2"
2102-
"@graphql-tools/utils" "^8.1.1"
2101+
"@graphql-tools/schema" "8.2.0"
2102+
"@graphql-tools/utils" "^8.2.0"
21032103
p-limit "3.1.0"
21042104
tslib "~2.3.0"
21052105

@@ -3262,7 +3262,12 @@
32623262
resolved "https://registry.yarnpkg.com/@types/ms/-/ms-0.7.31.tgz#31b7ca6407128a3d2bbc27fe2d21b345397f6197"
32633263
integrity sha512-iiUgKzV9AuaEkZqkOLDIvlQiL6ltuZd9tGcW3gwpnX8JbuiuhFlEGmmFXEXkN50Cvq7Os88IY2v0dkDqXYWVgA==
32643264

3265-
"@types/node@*", "@types/[email protected]", "@types/node@^14.14.33":
3265+
"@types/node@*", "@types/node@^16.4.10":
3266+
version "16.4.13"
3267+
resolved "https://registry.yarnpkg.com/@types/node/-/node-16.4.13.tgz#7dfd9c14661edc65cccd43a29eb454174642370d"
3268+
integrity sha512-bLL69sKtd25w7p1nvg9pigE4gtKVpGTPojBFLMkGHXuUgap2sLqQt2qUnqmVCDfzGUL0DRNZP+1prIZJbMeAXg==
3269+
3270+
"@types/[email protected]", "@types/node@^14.14.33":
32663271
version "14.17.14"
32673272
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.17.14.tgz#6fda9785b41570eb628bac27be4b602769a3f938"
32683273
integrity sha512-rsAj2u8Xkqfc332iXV12SqIsjVi07H479bOP4q94NAcjzmAvapumEhuVIt53koEf7JFrpjgNKjBga5Pnn/GL8A==
@@ -3282,11 +3287,6 @@
32823287
resolved "https://registry.yarnpkg.com/@types/node/-/node-15.14.7.tgz#29fea9a5b14e2b75c19028e1c7a32edd1e89fe92"
32833288
integrity sha512-FA45p37/mLhpebgbPWWCKfOisTjxGK9lwcHlJ6XVLfu3NgfcazOJHdYUZCWPMK8QX4LhNZdmfo6iMz9FqpUbaw==
32843289

3285-
"@types/node@^16.4.10":
3286-
version "16.4.13"
3287-
resolved "https://registry.yarnpkg.com/@types/node/-/node-16.4.13.tgz#7dfd9c14661edc65cccd43a29eb454174642370d"
3288-
integrity sha512-bLL69sKtd25w7p1nvg9pigE4gtKVpGTPojBFLMkGHXuUgap2sLqQt2qUnqmVCDfzGUL0DRNZP+1prIZJbMeAXg==
3289-
32903290
"@types/normalize-package-data@^2.4.0":
32913291
version "2.4.1"
32923292
resolved "https://registry.yarnpkg.com/@types/normalize-package-data/-/normalize-package-data-2.4.1.tgz#d3357479a0fdfdd5907fe67e17e0a85c906e1301"
@@ -13561,12 +13561,12 @@ rxjs@^6.3.3, rxjs@^6.4.0, rxjs@^6.5.1, rxjs@^6.6.0, rxjs@^6.6.3, rxjs@^6.6.7:
1356113561
dependencies:
1356213562
tslib "^1.9.0"
1356313563

13564-
[email protected], safe-buffer@>=5.1.0, safe-buffer@^5.0.1, safe-buffer@^5.1.0, safe-buffer@^5.1.1, safe-buffer@^5.1.2, safe-buffer@~5.1.0, safe-buffer@~5.1.1:
13564+
[email protected], safe-buffer@~5.1.0, safe-buffer@~5.1.1:
1356513565
version "5.1.2"
1356613566
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.1.2.tgz#991ec69d296e0313747d59bdfd2b745c35f8828d"
1356713567
integrity sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==
1356813568

13569-
safe-buffer@^5.2.0, safe-buffer@~5.2.0:
13569+
safe-buffer@>=5.1.0, safe-buffer@^5.0.1, safe-buffer@^5.1.0, safe-buffer@^5.1.1, safe-buffer@^5.1.2, safe-buffer@^5.2.0, safe-buffer@~5.2.0:
1357013570
version "5.2.1"
1357113571
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.2.1.tgz#1eaf9fa9bdb1fdd4ec75f58f9cdb4e6b7827eec6"
1357213572
integrity sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==

0 commit comments

Comments
 (0)