Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add INTROSPECTION_DISABLED error enum, set it on validation errors #7035

Merged
merged 2 commits into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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