@envelop/fragment-arguments 4.0.0-alpha-d0d0776.0 → 4.0.0-alpha-5b6bfa8.0
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/README.md +5 -5
- package/cjs/extended-parser.js +80 -0
- package/cjs/index.js +23 -0
- package/cjs/package.json +1 -0
- package/cjs/utils.js +48 -0
- package/{index.mjs → esm/extended-parser.js} +4 -69
- package/esm/index.js +18 -0
- package/esm/utils.js +44 -0
- package/package.json +26 -16
- package/{extended-parser.d.ts → typings/extended-parser.d.ts} +2 -3
- package/{index.d.ts → typings/index.d.ts} +1 -1
- package/{utils.d.ts → typings/utils.d.ts} +1 -1
- package/index.js +0 -146
package/README.md
CHANGED
|
@@ -15,13 +15,13 @@ yarn add @envelop/fragment-arguments
|
|
|
15
15
|
## Usage Example
|
|
16
16
|
|
|
17
17
|
```ts
|
|
18
|
-
import { envelop } from '@envelop/core'
|
|
19
|
-
import { useFragmentArguments } from '@envelop/fragment-arguments'
|
|
18
|
+
import { envelop } from '@envelop/core'
|
|
19
|
+
import { useFragmentArguments } from '@envelop/fragment-arguments'
|
|
20
20
|
|
|
21
21
|
const getEnveloped = envelop({
|
|
22
22
|
plugins: [
|
|
23
23
|
// ... other plugins ...
|
|
24
|
-
useFragmentArguments()
|
|
25
|
-
]
|
|
26
|
-
})
|
|
24
|
+
useFragmentArguments()
|
|
25
|
+
]
|
|
26
|
+
})
|
|
27
27
|
```
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FragmentArgumentCompatibleParser = void 0;
|
|
4
|
+
const graphql_1 = require("@graphql-tools/graphql");
|
|
5
|
+
class FragmentArgumentCompatibleParser extends graphql_1.Parser {
|
|
6
|
+
// see https://github.com/graphql/graphql-js/pull/3248
|
|
7
|
+
getLexer() {
|
|
8
|
+
return this._lexer;
|
|
9
|
+
}
|
|
10
|
+
// see https://github.com/graphql/graphql-js/pull/3248
|
|
11
|
+
getOptions() {
|
|
12
|
+
return this._options;
|
|
13
|
+
}
|
|
14
|
+
// for backwards-compat with v15, this api was removed in v16 in favor of the this.node API.
|
|
15
|
+
loc(startToken) {
|
|
16
|
+
var _a;
|
|
17
|
+
if (((_a = this.getOptions()) === null || _a === void 0 ? void 0 : _a.noLocation) !== true) {
|
|
18
|
+
const lexer = this.getLexer();
|
|
19
|
+
return new graphql_1.Location(startToken, lexer.lastToken, lexer.source);
|
|
20
|
+
}
|
|
21
|
+
return undefined;
|
|
22
|
+
}
|
|
23
|
+
parseFragment() {
|
|
24
|
+
const start = this.getLexer().token;
|
|
25
|
+
this.expectToken(graphql_1.TokenKind.SPREAD);
|
|
26
|
+
const hasTypeCondition = this.expectOptionalKeyword('on');
|
|
27
|
+
if (!hasTypeCondition && this.peek(graphql_1.TokenKind.NAME)) {
|
|
28
|
+
const name = this.parseFragmentName();
|
|
29
|
+
if (this.peek(graphql_1.TokenKind.PAREN_L)) {
|
|
30
|
+
return {
|
|
31
|
+
kind: graphql_1.Kind.FRAGMENT_SPREAD,
|
|
32
|
+
name,
|
|
33
|
+
arguments: this.parseArguments(),
|
|
34
|
+
directives: this.parseDirectives(),
|
|
35
|
+
loc: this.loc(start),
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
return {
|
|
39
|
+
kind: graphql_1.Kind.FRAGMENT_SPREAD,
|
|
40
|
+
name: this.parseFragmentName(),
|
|
41
|
+
directives: this.parseDirectives(),
|
|
42
|
+
loc: this.loc(start),
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
return {
|
|
46
|
+
kind: graphql_1.Kind.INLINE_FRAGMENT,
|
|
47
|
+
typeCondition: hasTypeCondition ? this.parseNamedType() : undefined,
|
|
48
|
+
directives: this.parseDirectives(),
|
|
49
|
+
selectionSet: this.parseSelectionSet(),
|
|
50
|
+
loc: this.loc(start),
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
parseFragmentDefinition() {
|
|
54
|
+
const start = this.getLexer().token;
|
|
55
|
+
this.expectKeyword('fragment');
|
|
56
|
+
const name = this.parseFragmentName();
|
|
57
|
+
if (this.peek(graphql_1.TokenKind.PAREN_L)) {
|
|
58
|
+
const fragmentDefinition = {
|
|
59
|
+
kind: graphql_1.Kind.FRAGMENT_DEFINITION,
|
|
60
|
+
name,
|
|
61
|
+
variableDefinitions: this.parseVariableDefinitions(),
|
|
62
|
+
typeCondition: (this.expectKeyword('on'), this.parseNamedType()),
|
|
63
|
+
directives: this.parseDirectives(),
|
|
64
|
+
selectionSet: this.parseSelectionSet(),
|
|
65
|
+
loc: this.loc(start),
|
|
66
|
+
};
|
|
67
|
+
return fragmentDefinition;
|
|
68
|
+
}
|
|
69
|
+
const fragmentDefinition = {
|
|
70
|
+
kind: graphql_1.Kind.FRAGMENT_DEFINITION,
|
|
71
|
+
name,
|
|
72
|
+
typeCondition: (this.expectKeyword('on'), this.parseNamedType()),
|
|
73
|
+
directives: this.parseDirectives(),
|
|
74
|
+
selectionSet: this.parseSelectionSet(),
|
|
75
|
+
loc: this.loc(start),
|
|
76
|
+
};
|
|
77
|
+
return fragmentDefinition;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
exports.FragmentArgumentCompatibleParser = FragmentArgumentCompatibleParser;
|
package/cjs/index.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useFragmentArguments = exports.parseWithFragmentArguments = void 0;
|
|
4
|
+
const extended_parser_js_1 = require("./extended-parser.js");
|
|
5
|
+
const utils_js_1 = require("./utils.js");
|
|
6
|
+
function parseWithFragmentArguments(source, options) {
|
|
7
|
+
const parser = new extended_parser_js_1.FragmentArgumentCompatibleParser(source, options);
|
|
8
|
+
return parser.parseDocument();
|
|
9
|
+
}
|
|
10
|
+
exports.parseWithFragmentArguments = parseWithFragmentArguments;
|
|
11
|
+
const useFragmentArguments = () => {
|
|
12
|
+
return {
|
|
13
|
+
onParse({ setParseFn }) {
|
|
14
|
+
setParseFn(parseWithFragmentArguments);
|
|
15
|
+
return ({ result, replaceParseResult }) => {
|
|
16
|
+
if (result && 'kind' in result) {
|
|
17
|
+
replaceParseResult((0, utils_js_1.applySelectionSetFragmentArguments)(result));
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
exports.useFragmentArguments = useFragmentArguments;
|
package/cjs/package.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"commonjs"}
|
package/cjs/utils.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.applySelectionSetFragmentArguments = void 0;
|
|
4
|
+
const graphql_1 = require("@graphql-tools/graphql");
|
|
5
|
+
function applySelectionSetFragmentArguments(document) {
|
|
6
|
+
const fragmentList = new Map();
|
|
7
|
+
for (const def of document.definitions) {
|
|
8
|
+
if (def.kind !== 'FragmentDefinition') {
|
|
9
|
+
continue;
|
|
10
|
+
}
|
|
11
|
+
fragmentList.set(def.name.value, def);
|
|
12
|
+
}
|
|
13
|
+
return (0, graphql_1.visit)(document, {
|
|
14
|
+
FragmentSpread(fragmentNode) {
|
|
15
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
16
|
+
// @ts-ignore
|
|
17
|
+
if (fragmentNode.arguments != null && fragmentNode.arguments.length) {
|
|
18
|
+
const fragmentDef = fragmentList.get(fragmentNode.name.value);
|
|
19
|
+
if (!fragmentDef) {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
const fragmentArguments = new Map();
|
|
23
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
24
|
+
// @ts-ignore
|
|
25
|
+
for (const arg of fragmentNode.arguments) {
|
|
26
|
+
fragmentArguments.set(arg.name.value, arg);
|
|
27
|
+
}
|
|
28
|
+
const selectionSet = (0, graphql_1.visit)(fragmentDef.selectionSet, {
|
|
29
|
+
Variable(variableNode) {
|
|
30
|
+
const fragArg = fragmentArguments.get(variableNode.name.value);
|
|
31
|
+
if (fragArg) {
|
|
32
|
+
return fragArg.value;
|
|
33
|
+
}
|
|
34
|
+
return variableNode;
|
|
35
|
+
},
|
|
36
|
+
});
|
|
37
|
+
const inlineFragment = {
|
|
38
|
+
kind: graphql_1.Kind.INLINE_FRAGMENT,
|
|
39
|
+
typeCondition: fragmentDef.typeCondition,
|
|
40
|
+
selectionSet,
|
|
41
|
+
};
|
|
42
|
+
return inlineFragment;
|
|
43
|
+
}
|
|
44
|
+
return fragmentNode;
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
exports.applySelectionSetFragmentArguments = applySelectionSetFragmentArguments;
|
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
import { Parser } from 'graphql/
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
|
5
|
-
class FragmentArgumentCompatibleParser extends Parser {
|
|
1
|
+
import { TokenKind, Kind, Location, Parser, } from '@graphql-tools/graphql';
|
|
2
|
+
export class FragmentArgumentCompatibleParser extends Parser {
|
|
6
3
|
// see https://github.com/graphql/graphql-js/pull/3248
|
|
7
4
|
getLexer() {
|
|
8
5
|
return this._lexer;
|
|
@@ -13,7 +10,8 @@ class FragmentArgumentCompatibleParser extends Parser {
|
|
|
13
10
|
}
|
|
14
11
|
// for backwards-compat with v15, this api was removed in v16 in favor of the this.node API.
|
|
15
12
|
loc(startToken) {
|
|
16
|
-
|
|
13
|
+
var _a;
|
|
14
|
+
if (((_a = this.getOptions()) === null || _a === void 0 ? void 0 : _a.noLocation) !== true) {
|
|
17
15
|
const lexer = this.getLexer();
|
|
18
16
|
return new Location(startToken, lexer.lastToken, lexer.source);
|
|
19
17
|
}
|
|
@@ -76,66 +74,3 @@ class FragmentArgumentCompatibleParser extends Parser {
|
|
|
76
74
|
return fragmentDefinition;
|
|
77
75
|
}
|
|
78
76
|
}
|
|
79
|
-
|
|
80
|
-
function applySelectionSetFragmentArguments(document) {
|
|
81
|
-
const fragmentList = new Map();
|
|
82
|
-
for (const def of document.definitions) {
|
|
83
|
-
if (def.kind !== 'FragmentDefinition') {
|
|
84
|
-
continue;
|
|
85
|
-
}
|
|
86
|
-
fragmentList.set(def.name.value, def);
|
|
87
|
-
}
|
|
88
|
-
return visit(document, {
|
|
89
|
-
FragmentSpread(fragmentNode) {
|
|
90
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
91
|
-
// @ts-ignore
|
|
92
|
-
if (fragmentNode.arguments != null && fragmentNode.arguments.length) {
|
|
93
|
-
const fragmentDef = fragmentList.get(fragmentNode.name.value);
|
|
94
|
-
if (!fragmentDef) {
|
|
95
|
-
return;
|
|
96
|
-
}
|
|
97
|
-
const fragmentArguments = new Map();
|
|
98
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
99
|
-
// @ts-ignore
|
|
100
|
-
for (const arg of fragmentNode.arguments) {
|
|
101
|
-
fragmentArguments.set(arg.name.value, arg);
|
|
102
|
-
}
|
|
103
|
-
const selectionSet = visit(fragmentDef.selectionSet, {
|
|
104
|
-
Variable(variableNode) {
|
|
105
|
-
const fragArg = fragmentArguments.get(variableNode.name.value);
|
|
106
|
-
if (fragArg) {
|
|
107
|
-
return fragArg.value;
|
|
108
|
-
}
|
|
109
|
-
return variableNode;
|
|
110
|
-
},
|
|
111
|
-
});
|
|
112
|
-
const inlineFragment = {
|
|
113
|
-
kind: Kind.INLINE_FRAGMENT,
|
|
114
|
-
typeCondition: fragmentDef.typeCondition,
|
|
115
|
-
selectionSet,
|
|
116
|
-
};
|
|
117
|
-
return inlineFragment;
|
|
118
|
-
}
|
|
119
|
-
return fragmentNode;
|
|
120
|
-
},
|
|
121
|
-
});
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
function parseWithFragmentArguments(source, options) {
|
|
125
|
-
const parser = new FragmentArgumentCompatibleParser(source, options);
|
|
126
|
-
return parser.parseDocument();
|
|
127
|
-
}
|
|
128
|
-
const useFragmentArguments = () => {
|
|
129
|
-
return {
|
|
130
|
-
onParse({ setParseFn }) {
|
|
131
|
-
setParseFn(parseWithFragmentArguments);
|
|
132
|
-
return ({ result, replaceParseResult }) => {
|
|
133
|
-
if (result && 'kind' in result) {
|
|
134
|
-
replaceParseResult(applySelectionSetFragmentArguments(result));
|
|
135
|
-
}
|
|
136
|
-
};
|
|
137
|
-
},
|
|
138
|
-
};
|
|
139
|
-
};
|
|
140
|
-
|
|
141
|
-
export { parseWithFragmentArguments, useFragmentArguments };
|
package/esm/index.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { FragmentArgumentCompatibleParser } from './extended-parser.js';
|
|
2
|
+
import { applySelectionSetFragmentArguments } from './utils.js';
|
|
3
|
+
export function parseWithFragmentArguments(source, options) {
|
|
4
|
+
const parser = new FragmentArgumentCompatibleParser(source, options);
|
|
5
|
+
return parser.parseDocument();
|
|
6
|
+
}
|
|
7
|
+
export const useFragmentArguments = () => {
|
|
8
|
+
return {
|
|
9
|
+
onParse({ setParseFn }) {
|
|
10
|
+
setParseFn(parseWithFragmentArguments);
|
|
11
|
+
return ({ result, replaceParseResult }) => {
|
|
12
|
+
if (result && 'kind' in result) {
|
|
13
|
+
replaceParseResult(applySelectionSetFragmentArguments(result));
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
};
|
package/esm/utils.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { visit, Kind, } from '@graphql-tools/graphql';
|
|
2
|
+
export function applySelectionSetFragmentArguments(document) {
|
|
3
|
+
const fragmentList = new Map();
|
|
4
|
+
for (const def of document.definitions) {
|
|
5
|
+
if (def.kind !== 'FragmentDefinition') {
|
|
6
|
+
continue;
|
|
7
|
+
}
|
|
8
|
+
fragmentList.set(def.name.value, def);
|
|
9
|
+
}
|
|
10
|
+
return visit(document, {
|
|
11
|
+
FragmentSpread(fragmentNode) {
|
|
12
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
13
|
+
// @ts-ignore
|
|
14
|
+
if (fragmentNode.arguments != null && fragmentNode.arguments.length) {
|
|
15
|
+
const fragmentDef = fragmentList.get(fragmentNode.name.value);
|
|
16
|
+
if (!fragmentDef) {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
const fragmentArguments = new Map();
|
|
20
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
21
|
+
// @ts-ignore
|
|
22
|
+
for (const arg of fragmentNode.arguments) {
|
|
23
|
+
fragmentArguments.set(arg.name.value, arg);
|
|
24
|
+
}
|
|
25
|
+
const selectionSet = visit(fragmentDef.selectionSet, {
|
|
26
|
+
Variable(variableNode) {
|
|
27
|
+
const fragArg = fragmentArguments.get(variableNode.name.value);
|
|
28
|
+
if (fragArg) {
|
|
29
|
+
return fragArg.value;
|
|
30
|
+
}
|
|
31
|
+
return variableNode;
|
|
32
|
+
},
|
|
33
|
+
});
|
|
34
|
+
const inlineFragment = {
|
|
35
|
+
kind: Kind.INLINE_FRAGMENT,
|
|
36
|
+
typeCondition: fragmentDef.typeCondition,
|
|
37
|
+
selectionSet,
|
|
38
|
+
};
|
|
39
|
+
return inlineFragment;
|
|
40
|
+
}
|
|
41
|
+
return fragmentNode;
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
}
|
package/package.json
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@envelop/fragment-arguments",
|
|
3
|
-
"version": "4.0.0-alpha-
|
|
3
|
+
"version": "4.0.0-alpha-5b6bfa8.0",
|
|
4
4
|
"peerDependencies": {
|
|
5
|
-
"@envelop/core": "3.0.0-alpha-
|
|
6
|
-
"graphql": "
|
|
5
|
+
"@envelop/core": "3.0.0-alpha-5b6bfa8.0",
|
|
6
|
+
"@graphql-tools/graphql": "0.1.0-alpha-33fb2950.0"
|
|
7
7
|
},
|
|
8
|
+
"dependencies": {},
|
|
8
9
|
"repository": {
|
|
9
10
|
"type": "git",
|
|
10
11
|
"url": "https://github.com/n1ru4l/envelop.git",
|
|
@@ -12,33 +13,42 @@
|
|
|
12
13
|
},
|
|
13
14
|
"author": "Dotan Simha <dotansimha@gmail.com>",
|
|
14
15
|
"license": "MIT",
|
|
15
|
-
"main": "index.js",
|
|
16
|
-
"module": "index.
|
|
17
|
-
"typings": "index.d.ts",
|
|
16
|
+
"main": "cjs/index.js",
|
|
17
|
+
"module": "esm/index.js",
|
|
18
|
+
"typings": "typings/index.d.ts",
|
|
18
19
|
"typescript": {
|
|
19
|
-
"definition": "index.d.ts"
|
|
20
|
+
"definition": "typings/index.d.ts"
|
|
20
21
|
},
|
|
22
|
+
"type": "module",
|
|
21
23
|
"exports": {
|
|
22
24
|
".": {
|
|
23
25
|
"require": {
|
|
24
|
-
"
|
|
25
|
-
"
|
|
26
|
+
"types": "./typings/index.d.ts",
|
|
27
|
+
"default": "./cjs/index.js"
|
|
26
28
|
},
|
|
27
29
|
"import": {
|
|
28
|
-
"
|
|
29
|
-
"
|
|
30
|
+
"types": "./typings/index.d.ts",
|
|
31
|
+
"default": "./esm/index.js"
|
|
32
|
+
},
|
|
33
|
+
"default": {
|
|
34
|
+
"types": "./typings/index.d.ts",
|
|
35
|
+
"default": "./esm/index.js"
|
|
30
36
|
}
|
|
31
37
|
},
|
|
32
38
|
"./*": {
|
|
33
39
|
"require": {
|
|
34
|
-
"
|
|
35
|
-
"
|
|
40
|
+
"types": "./typings/*.d.ts",
|
|
41
|
+
"default": "./cjs/*.js"
|
|
36
42
|
},
|
|
37
43
|
"import": {
|
|
38
|
-
"
|
|
39
|
-
"
|
|
44
|
+
"types": "./typings/*.d.ts",
|
|
45
|
+
"default": "./esm/*.js"
|
|
46
|
+
},
|
|
47
|
+
"default": {
|
|
48
|
+
"types": "./typings/*.d.ts",
|
|
49
|
+
"default": "./esm/*.js"
|
|
40
50
|
}
|
|
41
51
|
},
|
|
42
52
|
"./package.json": "./package.json"
|
|
43
53
|
}
|
|
44
|
-
}
|
|
54
|
+
}
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
import { Token, Location, FragmentDefinitionNode, FragmentSpreadNode, InlineFragmentNode } from 'graphql';
|
|
1
|
+
import type { Lexer, ParseOptions } from '@graphql-tools/graphql';
|
|
2
|
+
import { Token, Location, FragmentDefinitionNode, FragmentSpreadNode, InlineFragmentNode, Parser } from '@graphql-tools/graphql';
|
|
4
3
|
export declare class FragmentArgumentCompatibleParser extends Parser {
|
|
5
4
|
getLexer(): Lexer;
|
|
6
5
|
getOptions(): ParseOptions;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Plugin } from '@envelop/core';
|
|
2
2
|
import type { ParseOptions } from 'graphql/language/parser';
|
|
3
|
-
import type { Source, DocumentNode } from 'graphql';
|
|
3
|
+
import type { Source, DocumentNode } from '@graphql-tools/graphql';
|
|
4
4
|
export declare function parseWithFragmentArguments(source: string | Source, options?: ParseOptions): DocumentNode;
|
|
5
5
|
export declare const useFragmentArguments: () => Plugin;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { DocumentNode } from 'graphql';
|
|
1
|
+
import { DocumentNode } from '@graphql-tools/graphql';
|
|
2
2
|
export declare function applySelectionSetFragmentArguments(document: DocumentNode): DocumentNode | Error;
|
package/index.js
DELETED
|
@@ -1,146 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
const parser_js = require('graphql/language/parser.js');
|
|
6
|
-
const graphql = require('graphql');
|
|
7
|
-
|
|
8
|
-
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
|
9
|
-
class FragmentArgumentCompatibleParser extends parser_js.Parser {
|
|
10
|
-
// see https://github.com/graphql/graphql-js/pull/3248
|
|
11
|
-
getLexer() {
|
|
12
|
-
return this._lexer;
|
|
13
|
-
}
|
|
14
|
-
// see https://github.com/graphql/graphql-js/pull/3248
|
|
15
|
-
getOptions() {
|
|
16
|
-
return this._options;
|
|
17
|
-
}
|
|
18
|
-
// for backwards-compat with v15, this api was removed in v16 in favor of the this.node API.
|
|
19
|
-
loc(startToken) {
|
|
20
|
-
if (this.getOptions()?.noLocation !== true) {
|
|
21
|
-
const lexer = this.getLexer();
|
|
22
|
-
return new graphql.Location(startToken, lexer.lastToken, lexer.source);
|
|
23
|
-
}
|
|
24
|
-
return undefined;
|
|
25
|
-
}
|
|
26
|
-
parseFragment() {
|
|
27
|
-
const start = this.getLexer().token;
|
|
28
|
-
this.expectToken(graphql.TokenKind.SPREAD);
|
|
29
|
-
const hasTypeCondition = this.expectOptionalKeyword('on');
|
|
30
|
-
if (!hasTypeCondition && this.peek(graphql.TokenKind.NAME)) {
|
|
31
|
-
const name = this.parseFragmentName();
|
|
32
|
-
if (this.peek(graphql.TokenKind.PAREN_L)) {
|
|
33
|
-
return {
|
|
34
|
-
kind: graphql.Kind.FRAGMENT_SPREAD,
|
|
35
|
-
name,
|
|
36
|
-
arguments: this.parseArguments(),
|
|
37
|
-
directives: this.parseDirectives(),
|
|
38
|
-
loc: this.loc(start),
|
|
39
|
-
};
|
|
40
|
-
}
|
|
41
|
-
return {
|
|
42
|
-
kind: graphql.Kind.FRAGMENT_SPREAD,
|
|
43
|
-
name: this.parseFragmentName(),
|
|
44
|
-
directives: this.parseDirectives(),
|
|
45
|
-
loc: this.loc(start),
|
|
46
|
-
};
|
|
47
|
-
}
|
|
48
|
-
return {
|
|
49
|
-
kind: graphql.Kind.INLINE_FRAGMENT,
|
|
50
|
-
typeCondition: hasTypeCondition ? this.parseNamedType() : undefined,
|
|
51
|
-
directives: this.parseDirectives(),
|
|
52
|
-
selectionSet: this.parseSelectionSet(),
|
|
53
|
-
loc: this.loc(start),
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
|
-
parseFragmentDefinition() {
|
|
57
|
-
const start = this.getLexer().token;
|
|
58
|
-
this.expectKeyword('fragment');
|
|
59
|
-
const name = this.parseFragmentName();
|
|
60
|
-
if (this.peek(graphql.TokenKind.PAREN_L)) {
|
|
61
|
-
const fragmentDefinition = {
|
|
62
|
-
kind: graphql.Kind.FRAGMENT_DEFINITION,
|
|
63
|
-
name,
|
|
64
|
-
variableDefinitions: this.parseVariableDefinitions(),
|
|
65
|
-
typeCondition: (this.expectKeyword('on'), this.parseNamedType()),
|
|
66
|
-
directives: this.parseDirectives(),
|
|
67
|
-
selectionSet: this.parseSelectionSet(),
|
|
68
|
-
loc: this.loc(start),
|
|
69
|
-
};
|
|
70
|
-
return fragmentDefinition;
|
|
71
|
-
}
|
|
72
|
-
const fragmentDefinition = {
|
|
73
|
-
kind: graphql.Kind.FRAGMENT_DEFINITION,
|
|
74
|
-
name,
|
|
75
|
-
typeCondition: (this.expectKeyword('on'), this.parseNamedType()),
|
|
76
|
-
directives: this.parseDirectives(),
|
|
77
|
-
selectionSet: this.parseSelectionSet(),
|
|
78
|
-
loc: this.loc(start),
|
|
79
|
-
};
|
|
80
|
-
return fragmentDefinition;
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
function applySelectionSetFragmentArguments(document) {
|
|
85
|
-
const fragmentList = new Map();
|
|
86
|
-
for (const def of document.definitions) {
|
|
87
|
-
if (def.kind !== 'FragmentDefinition') {
|
|
88
|
-
continue;
|
|
89
|
-
}
|
|
90
|
-
fragmentList.set(def.name.value, def);
|
|
91
|
-
}
|
|
92
|
-
return graphql.visit(document, {
|
|
93
|
-
FragmentSpread(fragmentNode) {
|
|
94
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
95
|
-
// @ts-ignore
|
|
96
|
-
if (fragmentNode.arguments != null && fragmentNode.arguments.length) {
|
|
97
|
-
const fragmentDef = fragmentList.get(fragmentNode.name.value);
|
|
98
|
-
if (!fragmentDef) {
|
|
99
|
-
return;
|
|
100
|
-
}
|
|
101
|
-
const fragmentArguments = new Map();
|
|
102
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
103
|
-
// @ts-ignore
|
|
104
|
-
for (const arg of fragmentNode.arguments) {
|
|
105
|
-
fragmentArguments.set(arg.name.value, arg);
|
|
106
|
-
}
|
|
107
|
-
const selectionSet = graphql.visit(fragmentDef.selectionSet, {
|
|
108
|
-
Variable(variableNode) {
|
|
109
|
-
const fragArg = fragmentArguments.get(variableNode.name.value);
|
|
110
|
-
if (fragArg) {
|
|
111
|
-
return fragArg.value;
|
|
112
|
-
}
|
|
113
|
-
return variableNode;
|
|
114
|
-
},
|
|
115
|
-
});
|
|
116
|
-
const inlineFragment = {
|
|
117
|
-
kind: graphql.Kind.INLINE_FRAGMENT,
|
|
118
|
-
typeCondition: fragmentDef.typeCondition,
|
|
119
|
-
selectionSet,
|
|
120
|
-
};
|
|
121
|
-
return inlineFragment;
|
|
122
|
-
}
|
|
123
|
-
return fragmentNode;
|
|
124
|
-
},
|
|
125
|
-
});
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
function parseWithFragmentArguments(source, options) {
|
|
129
|
-
const parser = new FragmentArgumentCompatibleParser(source, options);
|
|
130
|
-
return parser.parseDocument();
|
|
131
|
-
}
|
|
132
|
-
const useFragmentArguments = () => {
|
|
133
|
-
return {
|
|
134
|
-
onParse({ setParseFn }) {
|
|
135
|
-
setParseFn(parseWithFragmentArguments);
|
|
136
|
-
return ({ result, replaceParseResult }) => {
|
|
137
|
-
if (result && 'kind' in result) {
|
|
138
|
-
replaceParseResult(applySelectionSetFragmentArguments(result));
|
|
139
|
-
}
|
|
140
|
-
};
|
|
141
|
-
},
|
|
142
|
-
};
|
|
143
|
-
};
|
|
144
|
-
|
|
145
|
-
exports.parseWithFragmentArguments = parseWithFragmentArguments;
|
|
146
|
-
exports.useFragmentArguments = useFragmentArguments;
|