-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
utils.ts
85 lines (73 loc) · 2.91 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { Types } from '@graphql-codegen/plugin-helpers';
import { isDocumentNode } from '@graphql-tools/utils';
import { DocumentNode, GraphQLSchema, isSchema, Kind } from 'graphql';
export function isObjectMap(obj: any): obj is Types.PluginConfig<any> {
return obj && typeof obj === 'object' && !Array.isArray(obj);
}
export function prioritize<T>(...values: T[]): T {
const picked = values.find(val => typeof val === 'boolean');
if (typeof picked !== 'boolean') {
return values[values.length - 1];
}
return picked;
}
export function pickFlag<TConfig, TKey extends keyof TConfig>(flag: TKey, config: TConfig): TConfig[TKey] | undefined {
return isObjectMap(config) ? config[flag] : undefined;
}
export function shouldValidateDuplicateDocuments(
skipDocumentsValidationOption: Types.GenerateOptions['skipDocumentsValidation']
) {
// If the value is true, skip all
if (skipDocumentsValidationOption === true) {
return false;
}
// If the value is object with the specific flag, only skip this one
if (typeof skipDocumentsValidationOption === 'object' && skipDocumentsValidationOption.skipDuplicateValidation) {
return false;
}
// If the value is falsy or the specific flag is not set, validate
return true;
}
export function shouldValidateDocumentsAgainstSchema(
skipDocumentsValidationOption: Types.GenerateOptions['skipDocumentsValidation']
) {
// If the value is true, skip all
if (skipDocumentsValidationOption === true) {
return false;
}
// If the value is object with the specific flag, only skip this one
if (typeof skipDocumentsValidationOption === 'object' && skipDocumentsValidationOption.skipValidationAgainstSchema) {
return false;
}
// If the value is falsy or the specific flag is not set, validate
return true;
}
export function getSkipDocumentsValidationOption(options: Types.GenerateOptions): Types.SkipDocumentsValidationOptions {
// If the value is set on the root level
if (options.skipDocumentsValidation) {
return options.skipDocumentsValidation;
}
// If the value is set under `config` property
const flagFromConfig: Types.SkipDocumentsValidationOptions = pickFlag('skipDocumentsValidation', options.config);
if (flagFromConfig) {
return flagFromConfig;
}
return false;
}
const federationDirectives = ['key', 'requires', 'provides', 'external'];
export function hasFederationSpec(schemaOrAST: GraphQLSchema | DocumentNode) {
if (isSchema(schemaOrAST)) {
return federationDirectives.some(directive => schemaOrAST.getDirective(directive));
} else if (isDocumentNode(schemaOrAST)) {
return schemaOrAST.definitions.some(
def => def.kind === Kind.DIRECTIVE_DEFINITION && federationDirectives.includes(def.name.value)
);
}
return false;
}
export function extractHashFromSchema(schema: GraphQLSchema): string | null {
if (!schema.extensions) {
schema.extensions = {};
}
return (schema.extensions['hash'] as string) ?? null;
}