Skip to content

Commit 2761105

Browse files
committed
chore: add unit tests
1 parent 434bccd commit 2761105

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { assert, describe, expect, it } from 'vitest'
2+
3+
import { server } from '../graphql-instance'
4+
5+
describe('GraphQL instance', () => {
6+
describe('Batching within an operation', () => {
7+
it('should not throw an error if a single root fields is present', async () => {
8+
const result = await server.executeOperation({
9+
query: `query HealthCheck { healthcheck { version } }`,
10+
})
11+
assert(result.body.kind === 'single')
12+
expect(result.body.singleResult.errors).toBeUndefined()
13+
})
14+
15+
it('should throw an error if multiple root fields are present', async () => {
16+
const result = await server.executeOperation({
17+
query: `query TwoHealthChecks { h1: healthcheck { version } h2: healthcheck { version } }`,
18+
})
19+
assert(result.body.kind === 'single')
20+
expect(result.body.singleResult.errors[0]).toHaveProperty(
21+
'code',
22+
'BAD_USER_INPUT',
23+
)
24+
})
25+
})
26+
})

packages/backend/src/helpers/graphql-instance.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { Kind, OperationDefinitionNode } from 'graphql/language'
88
import { applyMiddleware } from 'graphql-middleware'
99

1010
import appConfig from '@/config/app'
11+
import { BadUserInputError } from '@/errors/graphql-errors'
1112
import { typeDefs } from '@/graphql/__generated__/typeDefs.generated'
1213
import resolvers from '@/graphql/resolvers'
1314
import 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

Comments
 (0)