@graphcommerce/graphql-codegen-relay-optimizer-plugin 5.2.0-canary.8 → 6.0.0-canary.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/__tests__/index.spec.js +136 -0
- package/dist/index.js +88 -0
- package/package.json +13 -14
- package/dist/main/index.d.ts +0 -4
- package/dist/main/index.js +0 -91
- package/dist/module/index.d.ts +0 -4
- package/dist/module/index.js +0 -81
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const graphql_1 = require("graphql");
|
|
4
|
+
const __1 = require("..");
|
|
5
|
+
const testSchema = (0, graphql_1.buildSchema)(/* GraphQL */ `
|
|
6
|
+
type Avatar {
|
|
7
|
+
id: ID!
|
|
8
|
+
url: String!
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
type User {
|
|
12
|
+
id: ID!
|
|
13
|
+
login: String!
|
|
14
|
+
avatar(height: Int!, width: Int!): Avatar
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
type Query {
|
|
18
|
+
user: User!
|
|
19
|
+
users: [User!]!
|
|
20
|
+
}
|
|
21
|
+
`);
|
|
22
|
+
it('can be called', async () => {
|
|
23
|
+
const testDocument = (0, graphql_1.parse)(/* GraphQL */ `
|
|
24
|
+
query user {
|
|
25
|
+
user {
|
|
26
|
+
id
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
`);
|
|
30
|
+
await (0, __1.plugin)(testSchema, [{ document: testDocument }], {});
|
|
31
|
+
});
|
|
32
|
+
it('can be called with queries that include connection fragments', async () => {
|
|
33
|
+
const testDocument = (0, graphql_1.parse)(/* GraphQL */ `
|
|
34
|
+
query user {
|
|
35
|
+
users @connection(key: "foo") {
|
|
36
|
+
id
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
`);
|
|
40
|
+
await (0, __1.plugin)(testSchema, [{ document: testDocument }], {});
|
|
41
|
+
});
|
|
42
|
+
it('can inline @argumentDefinitions/@arguments annotated fragments', async () => {
|
|
43
|
+
const fragmentDocument = (0, graphql_1.parse)(/* GraphQL */ `
|
|
44
|
+
fragment UserLogin on User
|
|
45
|
+
@argumentDefinitions(
|
|
46
|
+
height: { type: "Int", defaultValue: 10 }
|
|
47
|
+
width: { type: "Int", defaultValue: 10 }
|
|
48
|
+
) {
|
|
49
|
+
id
|
|
50
|
+
login
|
|
51
|
+
avatar(width: $width, height: $height) {
|
|
52
|
+
id
|
|
53
|
+
url
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
`);
|
|
57
|
+
const queryDocument = (0, graphql_1.parse)(/* GraphQL */ `
|
|
58
|
+
query user {
|
|
59
|
+
users {
|
|
60
|
+
...UserLogin @arguments(height: 30, width: 30)
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
`);
|
|
64
|
+
const input = [{ document: fragmentDocument }, { document: queryDocument }];
|
|
65
|
+
await (0, __1.plugin)(testSchema, input, {});
|
|
66
|
+
const queryDoc = input.find((doc) => doc.document?.definitions[0].kind === 'OperationDefinition');
|
|
67
|
+
expect(queryDoc).toBeDefined();
|
|
68
|
+
expect((0, graphql_1.print)(queryDoc?.document)).toBeSimilarStringTo(/* GraphQL */ `
|
|
69
|
+
query user {
|
|
70
|
+
users {
|
|
71
|
+
id
|
|
72
|
+
login
|
|
73
|
+
avatar(width: 30, height: 30) {
|
|
74
|
+
id
|
|
75
|
+
url
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
`);
|
|
80
|
+
});
|
|
81
|
+
it('handles unions with interfaces the correct way', async () => {
|
|
82
|
+
const schema = (0, graphql_1.buildSchema)(/* GraphQL */ `
|
|
83
|
+
type User {
|
|
84
|
+
id: ID!
|
|
85
|
+
login: String!
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
interface Error {
|
|
89
|
+
message: String!
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
type UserNotFoundError implements Error {
|
|
93
|
+
message: String!
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
type UserBlockedError implements Error {
|
|
97
|
+
message: String!
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
union UserResult = User | UserNotFoundError | UserBlockedError
|
|
101
|
+
|
|
102
|
+
type Query {
|
|
103
|
+
user: UserResult!
|
|
104
|
+
}
|
|
105
|
+
`);
|
|
106
|
+
const queryDocument = (0, graphql_1.parse)(/* GraphQL */ `
|
|
107
|
+
query user {
|
|
108
|
+
user {
|
|
109
|
+
... on User {
|
|
110
|
+
id
|
|
111
|
+
login
|
|
112
|
+
}
|
|
113
|
+
... on Error {
|
|
114
|
+
message
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
`);
|
|
119
|
+
const input = [{ document: queryDocument }];
|
|
120
|
+
await (0, __1.plugin)(schema, input, {});
|
|
121
|
+
const queryDoc = input.find((doc) => doc.document?.definitions[0].kind === 'OperationDefinition');
|
|
122
|
+
expect(queryDoc).toBeDefined();
|
|
123
|
+
expect((0, graphql_1.print)(queryDoc?.document)).toBeSimilarStringTo(/* GraphQL */ `
|
|
124
|
+
query user {
|
|
125
|
+
user {
|
|
126
|
+
... on User {
|
|
127
|
+
id
|
|
128
|
+
login
|
|
129
|
+
}
|
|
130
|
+
... on Error {
|
|
131
|
+
message
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
`);
|
|
136
|
+
});
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/* eslint-disable @typescript-eslint/no-non-null-assertion */
|
|
3
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
4
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
5
|
+
};
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.plugin = void 0;
|
|
8
|
+
const CompilerContext_1 = __importDefault(require("@ardatan/relay-compiler/lib/core/CompilerContext"));
|
|
9
|
+
const IRPrinter_1 = require("@ardatan/relay-compiler/lib/core/IRPrinter");
|
|
10
|
+
const RelayParser_1 = require("@ardatan/relay-compiler/lib/core/RelayParser");
|
|
11
|
+
const Schema_1 = require("@ardatan/relay-compiler/lib/core/Schema");
|
|
12
|
+
const ApplyFragmentArgumentTransform_1 = require("@ardatan/relay-compiler/lib/transforms/ApplyFragmentArgumentTransform");
|
|
13
|
+
const FlattenTransform_1 = require("@ardatan/relay-compiler/lib/transforms/FlattenTransform");
|
|
14
|
+
const InlineFragmentsTransform_1 = require("@ardatan/relay-compiler/lib/transforms/InlineFragmentsTransform");
|
|
15
|
+
const SkipRedundantNodesTransform_1 = require("@ardatan/relay-compiler/lib/transforms/SkipRedundantNodesTransform");
|
|
16
|
+
const graphql_1 = require("graphql");
|
|
17
|
+
function isFragment(documentFile) {
|
|
18
|
+
let name = false;
|
|
19
|
+
if (!documentFile.document)
|
|
20
|
+
return false;
|
|
21
|
+
(0, graphql_1.visit)(documentFile.document, {
|
|
22
|
+
FragmentDefinition: () => {
|
|
23
|
+
name = true;
|
|
24
|
+
},
|
|
25
|
+
});
|
|
26
|
+
return name;
|
|
27
|
+
}
|
|
28
|
+
const plugin = (schema, documents,
|
|
29
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
30
|
+
_config) => {
|
|
31
|
+
const isFrag = documents.every((d) => isFragment(d));
|
|
32
|
+
if (isFrag)
|
|
33
|
+
return { content: '' };
|
|
34
|
+
// @TODO way for users to define directives they use, otherwise relay will throw an unknown directive error
|
|
35
|
+
// Maybe we can scan the queries and add them dynamically without users having to do some extra stuff
|
|
36
|
+
// transformASTSchema creates a new schema instance instead of mutating the old one
|
|
37
|
+
const adjustedSchema = (0, Schema_1.create)((0, graphql_1.printSchema)(schema)).extend([
|
|
38
|
+
/* GraphQL */ `
|
|
39
|
+
directive @connection(key: String!, filter: [String!]) on FIELD
|
|
40
|
+
directive @client on FIELD
|
|
41
|
+
`,
|
|
42
|
+
]);
|
|
43
|
+
const documentAsts = documents.reduce((prev, v) => [...prev, ...(v.document?.definitions ?? [])], []);
|
|
44
|
+
const relayDocuments = (0, RelayParser_1.transform)(adjustedSchema, documentAsts);
|
|
45
|
+
const fragmentCompilerContext = new CompilerContext_1.default(adjustedSchema).addAll(relayDocuments);
|
|
46
|
+
const fragmentDocuments = fragmentCompilerContext
|
|
47
|
+
.applyTransforms([
|
|
48
|
+
ApplyFragmentArgumentTransform_1.transform,
|
|
49
|
+
(0, FlattenTransform_1.transformWithOptions)({ flattenAbstractTypes: false }),
|
|
50
|
+
SkipRedundantNodesTransform_1.transform,
|
|
51
|
+
])
|
|
52
|
+
.documents()
|
|
53
|
+
.filter((doc) => doc.kind === 'Fragment');
|
|
54
|
+
const queryCompilerContext = new CompilerContext_1.default(adjustedSchema)
|
|
55
|
+
.addAll(relayDocuments)
|
|
56
|
+
.applyTransforms([
|
|
57
|
+
ApplyFragmentArgumentTransform_1.transform,
|
|
58
|
+
InlineFragmentsTransform_1.transform,
|
|
59
|
+
(0, FlattenTransform_1.transformWithOptions)({ flattenAbstractTypes: false }),
|
|
60
|
+
SkipRedundantNodesTransform_1.transform,
|
|
61
|
+
]);
|
|
62
|
+
const newQueryDocuments = queryCompilerContext.documents().map((doc) => ({
|
|
63
|
+
location: 'optimized by relay',
|
|
64
|
+
document: (0, graphql_1.parse)((0, IRPrinter_1.print)(adjustedSchema, doc)),
|
|
65
|
+
}));
|
|
66
|
+
let newDocuments = [];
|
|
67
|
+
if (newQueryDocuments.length === 0) {
|
|
68
|
+
return { content: '' };
|
|
69
|
+
}
|
|
70
|
+
if (newQueryDocuments.length === 1) {
|
|
71
|
+
newDocuments = newQueryDocuments;
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
newDocuments = [
|
|
75
|
+
...fragmentDocuments.map((doc) => ({
|
|
76
|
+
location: 'optimized by relay',
|
|
77
|
+
document: (0, graphql_1.parse)((0, IRPrinter_1.print)(adjustedSchema, doc)),
|
|
78
|
+
})),
|
|
79
|
+
...newQueryDocuments,
|
|
80
|
+
];
|
|
81
|
+
}
|
|
82
|
+
documents.splice(0, documents.length);
|
|
83
|
+
documents.push(...newDocuments);
|
|
84
|
+
return {
|
|
85
|
+
content: '',
|
|
86
|
+
};
|
|
87
|
+
};
|
|
88
|
+
exports.plugin = plugin;
|
package/package.json
CHANGED
|
@@ -2,33 +2,32 @@
|
|
|
2
2
|
"name": "@graphcommerce/graphql-codegen-relay-optimizer-plugin",
|
|
3
3
|
"homepage": "https://www.graphcommerce.org/",
|
|
4
4
|
"repository": "github:graphcommerce-org/graphcommerce",
|
|
5
|
-
"version": "
|
|
5
|
+
"version": "6.0.0-canary.20",
|
|
6
6
|
"description": "GraphQL Code Generator plugin for optimizing your GraphQL queries relay style.",
|
|
7
7
|
"license": "MIT",
|
|
8
|
-
"
|
|
9
|
-
"
|
|
8
|
+
"type": "commonjs",
|
|
9
|
+
"main": "dist/index.js",
|
|
10
|
+
"types": "src/index.ts",
|
|
11
|
+
"scripts": {
|
|
12
|
+
"test": "jest",
|
|
13
|
+
"dev": "tsc -W --preserveWatchOutput",
|
|
14
|
+
"build": "tsc",
|
|
15
|
+
"prepack": "tsc"
|
|
16
|
+
},
|
|
10
17
|
"dependencies": {
|
|
11
18
|
"@ardatan/relay-compiler": "12.0.0",
|
|
12
|
-
"@graphql-codegen/plugin-helpers": "
|
|
19
|
+
"@graphql-codegen/plugin-helpers": "4.0.0",
|
|
13
20
|
"graphql": "16.6.0"
|
|
14
21
|
},
|
|
15
22
|
"devDependencies": {
|
|
16
|
-
"@graphcommerce/eslint-config-pwa": "
|
|
17
|
-
"@graphcommerce/prettier-config-pwa": "
|
|
23
|
+
"@graphcommerce/eslint-config-pwa": "6.0.0-canary.20",
|
|
24
|
+
"@graphcommerce/prettier-config-pwa": "6.0.0-canary.20",
|
|
18
25
|
"@types/jest": "29.2.4",
|
|
19
26
|
"@types/relay-compiler": "8.0.3",
|
|
20
27
|
"jest": "29.3.1",
|
|
21
28
|
"ts-jest": "29.0.3",
|
|
22
29
|
"typescript": "4.9.4"
|
|
23
30
|
},
|
|
24
|
-
"scripts": {
|
|
25
|
-
"test": "jest",
|
|
26
|
-
"dev": "tsc --preserveWatchOutput --watch --sourceMap --outDir dist/main",
|
|
27
|
-
"build:module": "tsc --target es2017 --outDir dist/module",
|
|
28
|
-
"build:main": "tsc --target es5 --outDir dist/main",
|
|
29
|
-
"build": "yarn build:module && yarn build:main",
|
|
30
|
-
"prepack": "yarn build"
|
|
31
|
-
},
|
|
32
31
|
"prettier": "@graphcommerce/prettier-config-pwa",
|
|
33
32
|
"eslint": {
|
|
34
33
|
"extends": "@graphcommerce/eslint-config-pwa"
|
package/dist/main/index.d.ts
DELETED
package/dist/main/index.js
DELETED
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/* eslint-disable @typescript-eslint/no-non-null-assertion */
|
|
3
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
4
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
5
|
-
if (ar || !(i in from)) {
|
|
6
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
7
|
-
ar[i] = from[i];
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
11
|
-
};
|
|
12
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
13
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
14
|
-
};
|
|
15
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
-
exports.plugin = void 0;
|
|
17
|
-
var CompilerContext_1 = __importDefault(require("@ardatan/relay-compiler/lib/core/CompilerContext"));
|
|
18
|
-
var IRPrinter_1 = require("@ardatan/relay-compiler/lib/core/IRPrinter");
|
|
19
|
-
var RelayParser_1 = require("@ardatan/relay-compiler/lib/core/RelayParser");
|
|
20
|
-
var Schema_1 = require("@ardatan/relay-compiler/lib/core/Schema");
|
|
21
|
-
var ApplyFragmentArgumentTransform_1 = require("@ardatan/relay-compiler/lib/transforms/ApplyFragmentArgumentTransform");
|
|
22
|
-
var FlattenTransform_1 = require("@ardatan/relay-compiler/lib/transforms/FlattenTransform");
|
|
23
|
-
var InlineFragmentsTransform_1 = require("@ardatan/relay-compiler/lib/transforms/InlineFragmentsTransform");
|
|
24
|
-
var SkipRedundantNodesTransform_1 = require("@ardatan/relay-compiler/lib/transforms/SkipRedundantNodesTransform");
|
|
25
|
-
var graphql_1 = require("graphql");
|
|
26
|
-
function isFragment(documentFile) {
|
|
27
|
-
var name = false;
|
|
28
|
-
if (!documentFile.document)
|
|
29
|
-
return false;
|
|
30
|
-
(0, graphql_1.visit)(documentFile.document, {
|
|
31
|
-
FragmentDefinition: function () {
|
|
32
|
-
name = true;
|
|
33
|
-
},
|
|
34
|
-
});
|
|
35
|
-
return name;
|
|
36
|
-
}
|
|
37
|
-
var plugin = function (schema, documents,
|
|
38
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
39
|
-
_config) {
|
|
40
|
-
var isFrag = documents.every(function (d) { return isFragment(d); });
|
|
41
|
-
if (isFrag)
|
|
42
|
-
return { content: '' };
|
|
43
|
-
// @TODO way for users to define directives they use, otherwise relay will throw an unknown directive error
|
|
44
|
-
// Maybe we can scan the queries and add them dynamically without users having to do some extra stuff
|
|
45
|
-
// transformASTSchema creates a new schema instance instead of mutating the old one
|
|
46
|
-
var adjustedSchema = (0, Schema_1.create)((0, graphql_1.printSchema)(schema)).extend([
|
|
47
|
-
/* GraphQL */ "\n directive @connection(key: String!, filter: [String!]) on FIELD\n directive @client on FIELD\n ",
|
|
48
|
-
]);
|
|
49
|
-
var documentAsts = documents.reduce(function (prev, v) { var _a, _b; return __spreadArray(__spreadArray([], prev, true), ((_b = (_a = v.document) === null || _a === void 0 ? void 0 : _a.definitions) !== null && _b !== void 0 ? _b : []), true); }, []);
|
|
50
|
-
var relayDocuments = (0, RelayParser_1.transform)(adjustedSchema, documentAsts);
|
|
51
|
-
var fragmentCompilerContext = new CompilerContext_1.default(adjustedSchema).addAll(relayDocuments);
|
|
52
|
-
var fragmentDocuments = fragmentCompilerContext
|
|
53
|
-
.applyTransforms([
|
|
54
|
-
ApplyFragmentArgumentTransform_1.transform,
|
|
55
|
-
(0, FlattenTransform_1.transformWithOptions)({ flattenAbstractTypes: false }),
|
|
56
|
-
SkipRedundantNodesTransform_1.transform,
|
|
57
|
-
])
|
|
58
|
-
.documents()
|
|
59
|
-
.filter(function (doc) { return doc.kind === 'Fragment'; });
|
|
60
|
-
var queryCompilerContext = new CompilerContext_1.default(adjustedSchema)
|
|
61
|
-
.addAll(relayDocuments)
|
|
62
|
-
.applyTransforms([
|
|
63
|
-
ApplyFragmentArgumentTransform_1.transform,
|
|
64
|
-
InlineFragmentsTransform_1.transform,
|
|
65
|
-
(0, FlattenTransform_1.transformWithOptions)({ flattenAbstractTypes: false }),
|
|
66
|
-
SkipRedundantNodesTransform_1.transform,
|
|
67
|
-
]);
|
|
68
|
-
var newQueryDocuments = queryCompilerContext.documents().map(function (doc) { return ({
|
|
69
|
-
location: 'optimized by relay',
|
|
70
|
-
document: (0, graphql_1.parse)((0, IRPrinter_1.print)(adjustedSchema, doc)),
|
|
71
|
-
}); });
|
|
72
|
-
var newDocuments = [];
|
|
73
|
-
if (newQueryDocuments.length === 0) {
|
|
74
|
-
return { content: '' };
|
|
75
|
-
}
|
|
76
|
-
if (newQueryDocuments.length === 1) {
|
|
77
|
-
newDocuments = newQueryDocuments;
|
|
78
|
-
}
|
|
79
|
-
else {
|
|
80
|
-
newDocuments = __spreadArray(__spreadArray([], fragmentDocuments.map(function (doc) { return ({
|
|
81
|
-
location: 'optimized by relay',
|
|
82
|
-
document: (0, graphql_1.parse)((0, IRPrinter_1.print)(adjustedSchema, doc)),
|
|
83
|
-
}); }), true), newQueryDocuments, true);
|
|
84
|
-
}
|
|
85
|
-
documents.splice(0, documents.length);
|
|
86
|
-
documents.push.apply(documents, newDocuments);
|
|
87
|
-
return {
|
|
88
|
-
content: '',
|
|
89
|
-
};
|
|
90
|
-
};
|
|
91
|
-
exports.plugin = plugin;
|
package/dist/module/index.d.ts
DELETED
package/dist/module/index.js
DELETED
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-non-null-assertion */
|
|
2
|
-
import CompilerContext from '@ardatan/relay-compiler/lib/core/CompilerContext';
|
|
3
|
-
import { print as relayPrint } from '@ardatan/relay-compiler/lib/core/IRPrinter';
|
|
4
|
-
import { transform as relayParserTransform } from '@ardatan/relay-compiler/lib/core/RelayParser';
|
|
5
|
-
import { create as relayCreate } from '@ardatan/relay-compiler/lib/core/Schema';
|
|
6
|
-
import { transform as applyFragmentArgumentTransform } from '@ardatan/relay-compiler/lib/transforms/ApplyFragmentArgumentTransform';
|
|
7
|
-
import { transformWithOptions as flattenTransformWithOptions } from '@ardatan/relay-compiler/lib/transforms/FlattenTransform';
|
|
8
|
-
import { transform as inlineFragmentsTransform } from '@ardatan/relay-compiler/lib/transforms/InlineFragmentsTransform';
|
|
9
|
-
import { transform as skipRedundantNodesTransform } from '@ardatan/relay-compiler/lib/transforms/SkipRedundantNodesTransform';
|
|
10
|
-
import { parse, printSchema, visit } from 'graphql';
|
|
11
|
-
function isFragment(documentFile) {
|
|
12
|
-
let name = false;
|
|
13
|
-
if (!documentFile.document)
|
|
14
|
-
return false;
|
|
15
|
-
visit(documentFile.document, {
|
|
16
|
-
FragmentDefinition: () => {
|
|
17
|
-
name = true;
|
|
18
|
-
},
|
|
19
|
-
});
|
|
20
|
-
return name;
|
|
21
|
-
}
|
|
22
|
-
export const plugin = (schema, documents,
|
|
23
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
24
|
-
_config) => {
|
|
25
|
-
const isFrag = documents.every((d) => isFragment(d));
|
|
26
|
-
if (isFrag)
|
|
27
|
-
return { content: '' };
|
|
28
|
-
// @TODO way for users to define directives they use, otherwise relay will throw an unknown directive error
|
|
29
|
-
// Maybe we can scan the queries and add them dynamically without users having to do some extra stuff
|
|
30
|
-
// transformASTSchema creates a new schema instance instead of mutating the old one
|
|
31
|
-
const adjustedSchema = relayCreate(printSchema(schema)).extend([
|
|
32
|
-
/* GraphQL */ `
|
|
33
|
-
directive @connection(key: String!, filter: [String!]) on FIELD
|
|
34
|
-
directive @client on FIELD
|
|
35
|
-
`,
|
|
36
|
-
]);
|
|
37
|
-
const documentAsts = documents.reduce((prev, v) => { var _a, _b; return [...prev, ...((_b = (_a = v.document) === null || _a === void 0 ? void 0 : _a.definitions) !== null && _b !== void 0 ? _b : [])]; }, []);
|
|
38
|
-
const relayDocuments = relayParserTransform(adjustedSchema, documentAsts);
|
|
39
|
-
const fragmentCompilerContext = new CompilerContext(adjustedSchema).addAll(relayDocuments);
|
|
40
|
-
const fragmentDocuments = fragmentCompilerContext
|
|
41
|
-
.applyTransforms([
|
|
42
|
-
applyFragmentArgumentTransform,
|
|
43
|
-
flattenTransformWithOptions({ flattenAbstractTypes: false }),
|
|
44
|
-
skipRedundantNodesTransform,
|
|
45
|
-
])
|
|
46
|
-
.documents()
|
|
47
|
-
.filter((doc) => doc.kind === 'Fragment');
|
|
48
|
-
const queryCompilerContext = new CompilerContext(adjustedSchema)
|
|
49
|
-
.addAll(relayDocuments)
|
|
50
|
-
.applyTransforms([
|
|
51
|
-
applyFragmentArgumentTransform,
|
|
52
|
-
inlineFragmentsTransform,
|
|
53
|
-
flattenTransformWithOptions({ flattenAbstractTypes: false }),
|
|
54
|
-
skipRedundantNodesTransform,
|
|
55
|
-
]);
|
|
56
|
-
const newQueryDocuments = queryCompilerContext.documents().map((doc) => ({
|
|
57
|
-
location: 'optimized by relay',
|
|
58
|
-
document: parse(relayPrint(adjustedSchema, doc)),
|
|
59
|
-
}));
|
|
60
|
-
let newDocuments = [];
|
|
61
|
-
if (newQueryDocuments.length === 0) {
|
|
62
|
-
return { content: '' };
|
|
63
|
-
}
|
|
64
|
-
if (newQueryDocuments.length === 1) {
|
|
65
|
-
newDocuments = newQueryDocuments;
|
|
66
|
-
}
|
|
67
|
-
else {
|
|
68
|
-
newDocuments = [
|
|
69
|
-
...fragmentDocuments.map((doc) => ({
|
|
70
|
-
location: 'optimized by relay',
|
|
71
|
-
document: parse(relayPrint(adjustedSchema, doc)),
|
|
72
|
-
})),
|
|
73
|
-
...newQueryDocuments,
|
|
74
|
-
];
|
|
75
|
-
}
|
|
76
|
-
documents.splice(0, documents.length);
|
|
77
|
-
documents.push(...newDocuments);
|
|
78
|
-
return {
|
|
79
|
-
content: '',
|
|
80
|
-
};
|
|
81
|
-
};
|