forked from dotansimha/graphql-code-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): ability to skip some specific validation rules with skipD…
…ocumentValidation option (dotansimha#6825) * feat(core): ability to skip some specific validation rules with skipDocumentValidation option * What if it is not boolean * ... * Update changeset * Use memoize1 from utils for memoization * Add note * More refactor * ... * ... * Fix dev-tests * Add docs * ... * ...
- Loading branch information
Showing
7 changed files
with
177 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'@graphql-codegen/core': minor | ||
'@graphql-codegen/plugin-helpers': minor | ||
--- | ||
|
||
feat(core): ability to skip some specific validation rules with skipDocumentsValidation option |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
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; | ||
} |
14 changes: 2 additions & 12 deletions
14
packages/utils/plugins-helpers/src/getCachedDocumentNodeFromSchema.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,3 @@ | ||
import { getDocumentNodeFromSchema } from '@graphql-tools/utils'; | ||
import { GraphQLSchema, DocumentNode } from 'graphql'; | ||
import { getDocumentNodeFromSchema, memoize1 } from '@graphql-tools/utils'; | ||
|
||
const schemaDocumentNodeCache = new WeakMap<GraphQLSchema, DocumentNode>(); | ||
|
||
export function getCachedDocumentNodeFromSchema(schema: GraphQLSchema) { | ||
let documentNode = schemaDocumentNodeCache.get(schema); | ||
if (!documentNode) { | ||
documentNode = getDocumentNodeFromSchema(schema); | ||
schemaDocumentNodeCache.set(schema, documentNode); | ||
} | ||
return documentNode; | ||
} | ||
export const getCachedDocumentNodeFromSchema = memoize1(getDocumentNodeFromSchema); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters