1 | import { Kind, valueFromASTUntyped, } from 'graphql';
|
2 | function isTypeWithFields(t) {
|
3 | return t.kind === Kind.OBJECT_TYPE_DEFINITION || t.kind === Kind.OBJECT_TYPE_EXTENSION;
|
4 | }
|
5 | export function getArgumentsWithDirectives(documentNode) {
|
6 | const result = {};
|
7 | const allTypes = documentNode.definitions.filter(isTypeWithFields);
|
8 | for (const type of allTypes) {
|
9 | if (type.fields == null) {
|
10 | continue;
|
11 | }
|
12 | for (const field of type.fields) {
|
13 | const argsWithDirectives = field.arguments?.filter(arg => arg.directives?.length);
|
14 | if (!argsWithDirectives?.length) {
|
15 | continue;
|
16 | }
|
17 | const typeFieldResult = (result[`${type.name.value}.${field.name.value}`] = {});
|
18 | for (const arg of argsWithDirectives) {
|
19 | const directives = arg.directives.map(d => ({
|
20 | name: d.name.value,
|
21 | args: (d.arguments || []).reduce((prev, dArg) => ({ ...prev, [dArg.name.value]: valueFromASTUntyped(dArg.value) }), {}),
|
22 | }));
|
23 | typeFieldResult[arg.name.value] = directives;
|
24 | }
|
25 | }
|
26 | }
|
27 | return result;
|
28 | }
|