1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.getFieldsWithDirectives = void 0;
|
4 | const graphql_1 = require("graphql");
|
5 | function parseDirectiveValue(value) {
|
6 | switch (value.kind) {
|
7 | case graphql_1.Kind.INT:
|
8 | return parseInt(value.value);
|
9 | case graphql_1.Kind.FLOAT:
|
10 | return parseFloat(value.value);
|
11 | case graphql_1.Kind.BOOLEAN:
|
12 | return Boolean(value.value);
|
13 | case graphql_1.Kind.STRING:
|
14 | case graphql_1.Kind.ENUM:
|
15 | return value.value;
|
16 | case graphql_1.Kind.LIST:
|
17 | return value.values.map(v => parseDirectiveValue(v));
|
18 | case graphql_1.Kind.OBJECT:
|
19 | return value.fields.reduce((prev, v) => ({ ...prev, [v.name.value]: parseDirectiveValue(v.value) }), {});
|
20 | case graphql_1.Kind.NULL:
|
21 | return null;
|
22 | default:
|
23 | return null;
|
24 | }
|
25 | }
|
26 | function getFieldsWithDirectives(documentNode, options = {}) {
|
27 | const result = {};
|
28 | let selected = ['ObjectTypeDefinition', 'ObjectTypeExtension'];
|
29 | if (options.includeInputTypes) {
|
30 | selected = [...selected, 'InputObjectTypeDefinition', 'InputObjectTypeExtension'];
|
31 | }
|
32 | const allTypes = documentNode.definitions.filter(obj => selected.includes(obj.kind));
|
33 | for (const type of allTypes) {
|
34 | const typeName = type.name.value;
|
35 | if (type.fields == null) {
|
36 | continue;
|
37 | }
|
38 | for (const field of type.fields) {
|
39 | if (field.directives && field.directives.length > 0) {
|
40 | const fieldName = field.name.value;
|
41 | const key = `${typeName}.${fieldName}`;
|
42 | const directives = field.directives.map(d => ({
|
43 | name: d.name.value,
|
44 | args: (d.arguments || []).reduce((prev, arg) => ({ ...prev, [arg.name.value]: parseDirectiveValue(arg.value) }), {}),
|
45 | }));
|
46 | result[key] = directives;
|
47 | }
|
48 | }
|
49 | }
|
50 | return result;
|
51 | }
|
52 | exports.getFieldsWithDirectives = getFieldsWithDirectives;
|