UNPKG

3.36 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.extractHashFromSchema = exports.hasFederationSpec = exports.getSkipDocumentsValidationOption = exports.shouldValidateDocumentsAgainstSchema = exports.shouldValidateDuplicateDocuments = exports.pickFlag = exports.prioritize = exports.isObjectMap = void 0;
4const utils_1 = require("@graphql-tools/utils");
5const graphql_1 = require("graphql");
6function isObjectMap(obj) {
7 return obj && typeof obj === 'object' && !Array.isArray(obj);
8}
9exports.isObjectMap = isObjectMap;
10function prioritize(...values) {
11 const picked = values.find(val => typeof val === 'boolean');
12 if (typeof picked !== 'boolean') {
13 return values[values.length - 1];
14 }
15 return picked;
16}
17exports.prioritize = prioritize;
18function pickFlag(flag, config) {
19 return isObjectMap(config) ? config[flag] : undefined;
20}
21exports.pickFlag = pickFlag;
22function shouldValidateDuplicateDocuments(skipDocumentsValidationOption) {
23 // If the value is true, skip all
24 if (skipDocumentsValidationOption === true) {
25 return false;
26 }
27 // If the value is object with the specific flag, only skip this one
28 if (typeof skipDocumentsValidationOption === 'object' && skipDocumentsValidationOption.skipDuplicateValidation) {
29 return false;
30 }
31 // If the value is falsy or the specific flag is not set, validate
32 return true;
33}
34exports.shouldValidateDuplicateDocuments = shouldValidateDuplicateDocuments;
35function shouldValidateDocumentsAgainstSchema(skipDocumentsValidationOption) {
36 // If the value is true, skip all
37 if (skipDocumentsValidationOption === true) {
38 return false;
39 }
40 // If the value is object with the specific flag, only skip this one
41 if (typeof skipDocumentsValidationOption === 'object' && skipDocumentsValidationOption.skipValidationAgainstSchema) {
42 return false;
43 }
44 // If the value is falsy or the specific flag is not set, validate
45 return true;
46}
47exports.shouldValidateDocumentsAgainstSchema = shouldValidateDocumentsAgainstSchema;
48function getSkipDocumentsValidationOption(options) {
49 // If the value is set on the root level
50 if (options.skipDocumentsValidation) {
51 return options.skipDocumentsValidation;
52 }
53 // If the value is set under `config` property
54 const flagFromConfig = pickFlag('skipDocumentsValidation', options.config);
55 if (flagFromConfig) {
56 return flagFromConfig;
57 }
58 return false;
59}
60exports.getSkipDocumentsValidationOption = getSkipDocumentsValidationOption;
61const federationDirectives = ['key', 'requires', 'provides', 'external'];
62function hasFederationSpec(schemaOrAST) {
63 if ((0, graphql_1.isSchema)(schemaOrAST)) {
64 return federationDirectives.some(directive => schemaOrAST.getDirective(directive));
65 }
66 if ((0, utils_1.isDocumentNode)(schemaOrAST)) {
67 return schemaOrAST.definitions.some(def => def.kind === graphql_1.Kind.DIRECTIVE_DEFINITION && federationDirectives.includes(def.name.value));
68 }
69 return false;
70}
71exports.hasFederationSpec = hasFederationSpec;
72function extractHashFromSchema(schema) {
73 var _a;
74 schema.extensions || (schema.extensions = {});
75 return (_a = schema.extensions['hash']) !== null && _a !== void 0 ? _a : null;
76}
77exports.extractHashFromSchema = extractHashFromSchema;