Skip to content

Commit

Permalink
Add validationErrorCode extension to "introspection disabled" errors
Browse files Browse the repository at this point in the history
This extension's value comes from a new enum exported by `@apollo/server/errors`.

Fixes #7036 

Co-authored-by: David Glasser <glasser@apollographql.com>
  • Loading branch information
barryhagan and glasser committed Oct 18, 2022
1 parent 045639e commit b3f4000
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .changeset/lovely-terms-compare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@apollo/server-integration-testsuite': patch
'@apollo/server': patch
---

Errors resulting from an attempt to use introspection when it is not enabled now have an additional `validationErrorCode: 'INTROSPECTION_DISABLED'` extension; this value is part of a new enum `ApolloServerValidationErrorCode` exported from `@apollo/server/errors`.
18 changes: 17 additions & 1 deletion packages/integration-testsuite/src/apolloServerTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ import type {
GatewayInterface,
GatewaySchemaLoadOrUpdateCallback,
} from '@apollo/server-gateway-interface';
import {
ApolloServerErrorCode,
ApolloServerValidationErrorCode,
} from '@apollo/server/errors';

const quietLogger = loglevel.getLogger('quiet');
function mockLogger() {
Expand Down Expand Up @@ -240,6 +244,12 @@ export function defineIntegrationTestSuiteApolloServerTests(
expect(introspectionResult.errors[0].message).toMatch(
/introspection/,
);
expect(introspectionResult.errors[0].extensions?.code).toEqual(
ApolloServerErrorCode.GRAPHQL_VALIDATION_FAILED,
);
expect(
introspectionResult.errors[0].extensions.validationErrorCode,
).toEqual(ApolloServerValidationErrorCode.INTROSPECTION_DISABLED);
expect(formatError.mock.calls.length).toEqual(
introspectionResult.errors.length,
);
Expand All @@ -248,6 +258,9 @@ export function defineIntegrationTestSuiteApolloServerTests(
expect(result.data).toBeUndefined();
expect(result.errors).toBeDefined();
expect(result.errors[0].message).toMatch(/Not allowed/);
expect(result.errors[0].extensions?.code).toEqual(
ApolloServerErrorCode.GRAPHQL_VALIDATION_FAILED,
);
expect(formatError.mock.calls.length).toEqual(
introspectionResult.errors.length + result.errors.length,
);
Expand Down Expand Up @@ -281,7 +294,10 @@ export function defineIntegrationTestSuiteApolloServerTests(
expect(result.errors).toBeDefined();
expect(result.errors.length).toEqual(1);
expect(result.errors[0].extensions.code).toEqual(
'GRAPHQL_VALIDATION_FAILED',
ApolloServerErrorCode.GRAPHQL_VALIDATION_FAILED,
);
expect(result.errors[0].extensions.validationErrorCode).toEqual(
ApolloServerValidationErrorCode.INTROSPECTION_DISABLED,
);
});

Expand Down
13 changes: 11 additions & 2 deletions packages/server/src/ApolloServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ import {
ensureGraphQLError,
normalizeAndFormatErrors,
} from './errorNormalize.js';
import { ApolloServerErrorCode } from './errors/index.js';
import {
ApolloServerErrorCode,
ApolloServerValidationErrorCode,
} from './errors/index.js';
import type {
ApolloServerPlugin,
BaseContext,
Expand Down Expand Up @@ -76,7 +79,13 @@ const NoIntrospection: ValidationRule = (context: ValidationContext) => ({
context.reportError(
new GraphQLError(
'GraphQL introspection is not allowed by Apollo Server, but the query contained __schema or __type. To enable introspection, pass introspection: true to ApolloServer in production',
{ nodes: [node] },
{
nodes: [node],
extensions: {
validationErrorCode:
ApolloServerValidationErrorCode.INTROSPECTION_DISABLED,
},
},
),
);
}
Expand Down
4 changes: 4 additions & 0 deletions packages/server/src/errors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ export enum ApolloServerErrorCode {
BAD_REQUEST = 'BAD_REQUEST',
}

export enum ApolloServerValidationErrorCode {
INTROSPECTION_DISABLED = 'INTROSPECTION_DISABLED',
}

/**
* unwrapResolverError is a useful helper function for `formatError` hooks.
* Errors thrown in resolvers are wrapped by graphql-js in a GraphQLError that
Expand Down

0 comments on commit b3f4000

Please sign in to comment.