11import { makeExecutableSchema } from '@graphql-tools/schema' ;
22import { IResolvers } from '@graphql-tools/utils' ;
3- import { GraphQLResolveInfo , Source , execute as graphqlExecute , parse } from 'graphql' ;
3+ import { GraphQLResolveInfo , Source , execute as graphqlExecute , parse , specifiedRules , validate } from 'graphql' ;
44import merge from 'lodash/merge' ;
55import { Context , generate , get , getResolvers } from '..' ;
6+ import { noIntrospection } from '../utils/rules' ;
67
78export const execute = async ( {
89 additionalResolvers,
910 body,
11+ introspection = false ,
1012 ...ctx
1113} : {
1214 additionalResolvers ?: IResolvers < any , any > ;
15+ introspection ?: boolean ;
1316 body : any ;
1417} & Omit < Context , 'document' > ) => {
1518 const document = generate ( ctx . models ) ;
@@ -21,14 +24,26 @@ export const execute = async ({
2124 resolvers : merge ( generatedResolvers , additionalResolvers ) ,
2225 } ) ;
2326
27+ const parsedDocument = parse ( new Source ( body . query , 'GraphQL request' ) ) ;
28+
29+ const validationErrors = validate (
30+ schema ,
31+ parsedDocument ,
32+ introspection ? specifiedRules : [ ...specifiedRules , noIntrospection ] ,
33+ ) ;
34+
35+ if ( validationErrors . length > 0 ) {
36+ return { errors : validationErrors } ;
37+ }
38+
2439 const contextValue : Context = {
2540 document,
2641 ...ctx ,
2742 } ;
2843
2944 const result = await graphqlExecute ( {
3045 schema,
31- document : parse ( new Source ( body . query , 'GraphQL request' ) ) ,
46+ document : parsedDocument ,
3247 contextValue,
3348 variableValues : body . variables ,
3449 operationName : body . operationName ,
0 commit comments