diff --git a/CHANGELOG.md b/CHANGELOG.md index b38acc52780..29708d26258 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,7 +11,7 @@ The version headers in this history reflect the versions of Apollo Server itself ## v2.18.0 -- _Nothing yet! Stay tuned!_ +- `apollo-engine-reporting`: Parse and validate any schema passed via `overrideReportedSchema`, and throw accordingly on unparsable or invalid schemas. ## v2.17.0 diff --git a/packages/apollo-engine-reporting/src/agent.ts b/packages/apollo-engine-reporting/src/agent.ts index cca8cb0c4bf..3db4ae698ee 100644 --- a/packages/apollo-engine-reporting/src/agent.ts +++ b/packages/apollo-engine-reporting/src/agent.ts @@ -1,10 +1,12 @@ import os from 'os'; import { gzip } from 'zlib'; import { + buildSchema, DocumentNode, GraphQLError, GraphQLSchema, printSchema, + validateSchema } from 'graphql'; import { ReportHeader, @@ -510,6 +512,26 @@ export class EngineReportingAgent { } } + // Ensure a provided override schema can be parsed and validated + if (options.overrideReportedSchema) { + try { + const validationErrors = validateSchema( + buildSchema(options.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}` + ) + } + } + if (options.reportSchema !== undefined) { this.schemaReport = options.reportSchema; } else {