node_modules.apollo-codegen.src.validation.ts Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of apollo-client-maven-plugin Show documentation
Show all versions of apollo-client-maven-plugin Show documentation
Maven plugin for generating graphql clients
import {
validate,
specifiedRules,
NoUnusedFragmentsRule,
KnownDirectivesRule,
GraphQLError,
FieldNode,
ValidationContext,
GraphQLSchema,
DocumentNode,
OperationDefinitionNode
} from 'graphql';
import { ToolError, logError } from 'apollo-codegen-core/lib/errors';
export function validateQueryDocument(schema: GraphQLSchema, document: DocumentNode) {
const specifiedRulesToBeRemoved = [NoUnusedFragmentsRule, KnownDirectivesRule];
const rules = [
NoAnonymousQueries,
NoTypenameAlias,
...specifiedRules.filter(rule => !specifiedRulesToBeRemoved.includes(rule))
];
const validationErrors = validate(schema, document, rules);
if (validationErrors && validationErrors.length > 0) {
for (const error of validationErrors) {
logError(error);
}
throw new ToolError('Validation of GraphQL query document failed');
}
}
export function NoAnonymousQueries(context: ValidationContext) {
return {
OperationDefinition(node: OperationDefinitionNode) {
if (!node.name) {
context.reportError(new GraphQLError('Apollo does not support anonymous operations', [node]));
}
return false;
}
};
}
export function NoTypenameAlias(context: ValidationContext) {
return {
Field(node: FieldNode) {
const aliasName = node.alias && node.alias.value;
if (aliasName == '__typename') {
context.reportError(
new GraphQLError(
'Apollo needs to be able to insert __typename when needed, please do not use it as an alias',
[node]
)
);
}
}
};
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy