UNPKG

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