@@ -8,6 +8,7 @@ import { Kind, OperationDefinitionNode } from 'graphql/language'
88import { applyMiddleware } from 'graphql-middleware'
99
1010import appConfig from '@/config/app'
11+ import { BadUserInputError } from '@/errors/graphql-errors'
1112import { typeDefs } from '@/graphql/__generated__/typeDefs.generated'
1213import resolvers from '@/graphql/resolvers'
1314import authentication , { setCurrentUserContext } from '@/helpers/authentication'
@@ -43,7 +44,7 @@ function ApolloServerPluginUserTracer(): ApolloServerPlugin<AuthenticatedContext
4344 }
4445}
4546
46- function preventBatching ( ) : ApolloServerPlugin {
47+ function PreventBatching ( ) : ApolloServerPlugin {
4748 return {
4849 async requestDidStart ( ) {
4950 return {
@@ -54,13 +55,11 @@ function preventBatching(): ApolloServerPlugin {
5455 ( definition ) => definition . kind === Kind . OPERATION_DEFINITION ,
5556 ) as OperationDefinitionNode | undefined
5657
57- if ( queryDefinition ) {
58- // Check if there are multiple selections (root fields) in the query
59- if ( queryDefinition . selectionSet . selections . length > 1 ) {
60- throw new Error (
61- 'Multiple root fields in a single operation are not allowed.' ,
62- )
63- }
58+ // Check if there are multiple selections (root fields) in the query
59+ if ( queryDefinition ?. selectionSet . selections . length > 1 ) {
60+ throw new BadUserInputError (
61+ 'Multiple root fields in a single operation are not allowed.' ,
62+ )
6463 }
6564 } ,
6665 }
@@ -75,16 +74,18 @@ const schemaWithMiddleware = applyMiddleware(
7574 authentication . generate ( schema ) ,
7675)
7776
78- const server = new ApolloServer < UnauthenticatedContext > ( {
77+ export const server = new ApolloServer < UnauthenticatedContext > ( {
7978 schema : schemaWithMiddleware ,
8079 introspection : appConfig . isDev ,
8180 plugins : [
8281 appConfig . isDev
8382 ? ApolloServerPluginLandingPageLocalDefault ( )
8483 : ApolloServerPluginLandingPageDisabled ( ) ,
8584 ApolloServerPluginUserTracer ( ) ,
86- preventBatching ( ) ,
85+ PreventBatching ( ) ,
8786 ] ,
87+ // We don't want to allow batching within a single HTTP request, this defaults to false
88+ allowBatchedHttpRequests : false ,
8889 formatError : ( error ) => {
8990 logger . error ( error )
9091 let errorMessage = error . message
0 commit comments