1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.isUsingTypes = exports.hasNullableTypeRecursively = exports.normalizeConfig = exports.normalizeInstanceOrArray = exports.normalizeOutputParam = exports.isConfiguredOutput = exports.isOutputConfigArray = void 0;
|
4 | const graphql_1 = require("graphql");
|
5 | const utils_js_1 = require("./utils.js");
|
6 | function isOutputConfigArray(type) {
|
7 | return Array.isArray(type);
|
8 | }
|
9 | exports.isOutputConfigArray = isOutputConfigArray;
|
10 | function isConfiguredOutput(type) {
|
11 | return (typeof type === 'object' && type.plugins) || type.preset;
|
12 | }
|
13 | exports.isConfiguredOutput = isConfiguredOutput;
|
14 | function normalizeOutputParam(config) {
|
15 |
|
16 | if (isOutputConfigArray(config)) {
|
17 | return {
|
18 | documents: [],
|
19 | schema: [],
|
20 | plugins: isConfiguredOutput(config) ? config.plugins : config,
|
21 | };
|
22 | }
|
23 | if (isConfiguredOutput(config)) {
|
24 | return config;
|
25 | }
|
26 | throw new Error(`Invalid "generates" config!`);
|
27 | }
|
28 | exports.normalizeOutputParam = normalizeOutputParam;
|
29 | function normalizeInstanceOrArray(type) {
|
30 | if (Array.isArray(type)) {
|
31 | return type;
|
32 | }
|
33 | if (!type) {
|
34 | return [];
|
35 | }
|
36 | return [type];
|
37 | }
|
38 | exports.normalizeInstanceOrArray = normalizeInstanceOrArray;
|
39 | function normalizeConfig(config) {
|
40 | if (typeof config === 'string') {
|
41 | return [{ [config]: {} }];
|
42 | }
|
43 | if (Array.isArray(config)) {
|
44 | return config.map(plugin => (typeof plugin === 'string' ? { [plugin]: {} } : plugin));
|
45 | }
|
46 | if (typeof config === 'object') {
|
47 | return Object.keys(config).reduce((prev, pluginName) => [...prev, { [pluginName]: config[pluginName] }], []);
|
48 | }
|
49 | return [];
|
50 | }
|
51 | exports.normalizeConfig = normalizeConfig;
|
52 | function hasNullableTypeRecursively(type) {
|
53 | if (!(0, graphql_1.isNonNullType)(type)) {
|
54 | return true;
|
55 | }
|
56 | if ((0, graphql_1.isListType)(type) || (0, graphql_1.isNonNullType)(type)) {
|
57 | return hasNullableTypeRecursively(type.ofType);
|
58 | }
|
59 | return false;
|
60 | }
|
61 | exports.hasNullableTypeRecursively = hasNullableTypeRecursively;
|
62 | function isUsingTypes(document, externalFragments, schema) {
|
63 | let foundFields = 0;
|
64 | const typesStack = [];
|
65 | (0, graphql_1.visit)(document, {
|
66 | SelectionSet: {
|
67 | enter(node, key, parent, anscestors) {
|
68 | const insideIgnoredFragment = anscestors.find((f) => f.kind && f.kind === 'FragmentDefinition' && externalFragments.includes(f.name.value));
|
69 | if (insideIgnoredFragment) {
|
70 | return;
|
71 | }
|
72 | const selections = node.selections || [];
|
73 | if (schema && selections.length > 0) {
|
74 | const nextTypeName = (() => {
|
75 | if (parent.kind === graphql_1.Kind.FRAGMENT_DEFINITION) {
|
76 | return parent.typeCondition.name.value;
|
77 | }
|
78 | if (parent.kind === graphql_1.Kind.FIELD) {
|
79 | const lastType = typesStack[typesStack.length - 1];
|
80 | if (!lastType) {
|
81 | throw new Error(`Unable to find parent type! Please make sure you operation passes validation`);
|
82 | }
|
83 | const field = lastType.getFields()[parent.name.value];
|
84 | if (!field) {
|
85 | throw new Error(`Unable to find field "${parent.name.value}" on type "${lastType}"!`);
|
86 | }
|
87 | return (0, utils_js_1.getBaseType)(field.type).name;
|
88 | }
|
89 | if (parent.kind === graphql_1.Kind.OPERATION_DEFINITION) {
|
90 | if (parent.operation === 'query') {
|
91 | return schema.getQueryType().name;
|
92 | }
|
93 | if (parent.operation === 'mutation') {
|
94 | return schema.getMutationType().name;
|
95 | }
|
96 | if (parent.operation === 'subscription') {
|
97 | return schema.getSubscriptionType().name;
|
98 | }
|
99 | }
|
100 | else if (parent.kind === graphql_1.Kind.INLINE_FRAGMENT) {
|
101 | if (parent.typeCondition) {
|
102 | return parent.typeCondition.name.value;
|
103 | }
|
104 | return typesStack[typesStack.length - 1].name;
|
105 | }
|
106 | return null;
|
107 | })();
|
108 | typesStack.push(schema.getType(nextTypeName));
|
109 | }
|
110 | },
|
111 | leave(node) {
|
112 | const selections = node.selections || [];
|
113 | if (schema && selections.length > 0) {
|
114 | typesStack.pop();
|
115 | }
|
116 | },
|
117 | },
|
118 | Field: {
|
119 | enter: (node, key, parent, path, anscestors) => {
|
120 | if (node.name.value.startsWith('__')) {
|
121 | return;
|
122 | }
|
123 | const insideIgnoredFragment = anscestors.find((f) => f.kind && f.kind === 'FragmentDefinition' && externalFragments.includes(f.name.value));
|
124 | if (insideIgnoredFragment) {
|
125 | return;
|
126 | }
|
127 | const selections = node.selectionSet ? node.selectionSet.selections || [] : [];
|
128 | const relevantFragmentSpreads = selections.filter(s => s.kind === graphql_1.Kind.FRAGMENT_SPREAD && !externalFragments.includes(s.name.value));
|
129 | if (selections.length === 0 || relevantFragmentSpreads.length > 0) {
|
130 | foundFields++;
|
131 | }
|
132 | if (schema) {
|
133 | const lastType = typesStack[typesStack.length - 1];
|
134 | if (lastType && (0, graphql_1.isObjectType)(lastType)) {
|
135 | const field = lastType.getFields()[node.name.value];
|
136 | if (!field) {
|
137 | throw new Error(`Unable to find field "${node.name.value}" on type "${lastType}"!`);
|
138 | }
|
139 | const currentType = field.type;
|
140 |
|
141 | if (hasNullableTypeRecursively(currentType)) {
|
142 | foundFields++;
|
143 | }
|
144 | }
|
145 | }
|
146 | },
|
147 | },
|
148 | VariableDefinition: {
|
149 | enter: (node, key, parent, path, anscestors) => {
|
150 | const insideIgnoredFragment = anscestors.find((f) => f.kind && f.kind === 'FragmentDefinition' && externalFragments.includes(f.name.value));
|
151 | if (insideIgnoredFragment) {
|
152 | return;
|
153 | }
|
154 | foundFields++;
|
155 | },
|
156 | },
|
157 | InputValueDefinition: {
|
158 | enter: (node, key, parent, path, anscestors) => {
|
159 | const insideIgnoredFragment = anscestors.find((f) => f.kind && f.kind === 'FragmentDefinition' && externalFragments.includes(f.name.value));
|
160 | if (insideIgnoredFragment) {
|
161 | return;
|
162 | }
|
163 | foundFields++;
|
164 | },
|
165 | },
|
166 | });
|
167 | return foundFields > 0;
|
168 | }
|
169 | exports.isUsingTypes = isUsingTypes;
|