import { validate , specifiedRules , NoUnusedFragmentsRule , GraphQLError , FieldNode , ValidationContext , GraphQLSchema , DocumentNode , OperationDefinitionNode , } from 'graphql' ; import { ToolError , logError } from './errors' ; export function validateQueryDocument ( schema , document ) { const specifiedRulesToBeRemoved = [ NoUnusedFragmentsRule ] ; 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 ) { return { OperationDefinition ( node ) { if ( ! node . name ) { context . reportError ( new GraphQLError ( 'Apollo does not support anonymous operations' , [ node ] ) ) ; } return false ; } , } ; } export function NoTypenameAlias ( context ) { return { Field ( node ) { 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 ] ) ) ; } } , } ; }