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

schema-reporting: Check whether overrideReportedSchema is parsable/valid #4492

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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
22 changes: 22 additions & 0 deletions packages/apollo-engine-reporting/src/agent.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import os from 'os';
import { gzip } from 'zlib';
import {
buildSchema,
DocumentNode,
GraphQLError,
GraphQLSchema,
printSchema,
validateSchema
} from 'graphql';
import {
ReportHeader,
Expand Down Expand Up @@ -510,6 +512,26 @@ export class EngineReportingAgent<TContext = any> {
}
}

// 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 {
Expand Down