1 | import { isDocumentNode } from '@graphql-tools/utils';
|
2 | import { isSchema, Kind } from 'graphql';
|
3 | export function isObjectMap(obj) {
|
4 | return obj && typeof obj === 'object' && !Array.isArray(obj);
|
5 | }
|
6 | export 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 | }
|
13 | export function pickFlag(flag, config) {
|
14 | return isObjectMap(config) ? config[flag] : undefined;
|
15 | }
|
16 | export function shouldValidateDuplicateDocuments(skipDocumentsValidationOption) {
|
17 |
|
18 | if (skipDocumentsValidationOption === true) {
|
19 | return false;
|
20 | }
|
21 |
|
22 | if (typeof skipDocumentsValidationOption === 'object' && skipDocumentsValidationOption.skipDuplicateValidation) {
|
23 | return false;
|
24 | }
|
25 |
|
26 | return true;
|
27 | }
|
28 | export function shouldValidateDocumentsAgainstSchema(skipDocumentsValidationOption) {
|
29 |
|
30 | if (skipDocumentsValidationOption === true) {
|
31 | return false;
|
32 | }
|
33 |
|
34 | if (typeof skipDocumentsValidationOption === 'object' && skipDocumentsValidationOption.skipValidationAgainstSchema) {
|
35 | return false;
|
36 | }
|
37 |
|
38 | return true;
|
39 | }
|
40 | export function getSkipDocumentsValidationOption(options) {
|
41 |
|
42 | if (options.skipDocumentsValidation) {
|
43 | return options.skipDocumentsValidation;
|
44 | }
|
45 |
|
46 | const flagFromConfig = pickFlag('skipDocumentsValidation', options.config);
|
47 | if (flagFromConfig) {
|
48 | return flagFromConfig;
|
49 | }
|
50 | return false;
|
51 | }
|
52 | const federationDirectives = ['key', 'requires', 'provides', 'external'];
|
53 | export 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 | }
|
62 | export function extractHashFromSchema(schema) {
|
63 | schema.extensions ||= {};
|
64 | return schema.extensions['hash'] ?? null;
|
65 | }
|