Skip to content

Commit

Permalink
schema-reporting: Check if overrideReportedSchema is parsable/valid
Browse files Browse the repository at this point in the history
The Apollo API has recently changed such that `reportServerInfo` will now return
error for schemas that don't parse or validate.

This PR changes `apollo-engine-reporting` to check in the `EngineReportingAgent`
constructor whether override schemas successfully parse and validate, so that we
may return early before talking to the Apollo API.

(Originally #4492 by @sachindshinde, merged directly into release-2.18.0;
because the main change being released with v2.18 is #4453 which entirely
replaced the file which that PR changed, this is being reconstructed as part of
PR #4453.)
  • Loading branch information
glasser committed Sep 18, 2020
1 parent 7a7c118 commit a897b59
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ The version headers in this history reflect the versions of Apollo Server itself

- `apollo-server-core`: The default logger implementation (if you don't specify your own `logger` or specify `debug`) now logs at the INFO level instead of the WARN level. The main effect is on a few built-in plugins which log one INFO message at startup; if a custom plugin logs at the INFO level then those messages will be visible by default as well. [PR #4453](https://github.com/apollographql/apollo-server/pull/4453)

- `apollo-server-core`: Parse and validate any schema passed via `overrideReportedSchema` to the schema reporting plugin, and throw accordingly on unparsable or invalid schemas.

- Using Apollo Server from TypeScript now requires TypeScript 3.8 due to the use of the `import type` and `export type` directives. (If this proves to be a major problem we can revert this choice, but it makes it easier for us to ensure that certain large dependencies are only loaded when needed.) [PR #4453](https://github.com/apollographql/apollo-server/pull/4453)

- Updated `@apollographql/graphql-playground-react` to 1.7.33 to include [an upstream fix](https://github.com/apollographql/graphql-playground/commit/1c102692bfbb717688827204186c15cb92629b3a). [PR #4550](https://github.com/apollographql/apollo-server/pull/4550)
Expand Down
21 changes: 20 additions & 1 deletion packages/apollo-server-core/src/plugin/schemaReporting/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os from 'os';
import type { InternalApolloServerPlugin } from '../internalPlugin';
import { v4 as uuidv4 } from 'uuid';
import { printSchema } from 'graphql';
import { printSchema, validateSchema, buildSchema } from 'graphql';
import { SchemaReporter } from './schemaReporter';
import createSHA from '../../utils/createSHA';

Expand Down Expand Up @@ -78,6 +78,25 @@ export function ApolloServerPluginSchemaReporting(
);
}

// Ensure a provided override schema can be parsed and validated
if (overrideReportedSchema) {
try {
const validationErrors = validateSchema(
buildSchema(overrideReportedSchema, { noLocation: true }),
);
if (validationErrors.length) {
throw new Error(
validationErrors.map((error) => error.message).join('\n'),
);
}
} catch (err) {
throw new Error(
'The schema provided to overrideReportedSchema failed to parse or' +
`validate: ${err.message}`,
);
}
}

const executableSchema = overrideReportedSchema ?? printSchema(schema);
const executableSchemaId = computeExecutableSchemaId(executableSchema);

Expand Down

0 comments on commit a897b59

Please sign in to comment.