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

Replace deprecated use of getErrors with onError #1908

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -20,6 +20,7 @@
- <First `apollo-graphql` related entry goes here>
- `apollo-language-server`
- Fix definition navigation for vscode using localSchemaFile [#1996](https://github.com/apollographql/apollo-tooling/pull/1996)
- Replace deprecated use of `getErrors` with `onError` [#1908](https://github.com/apollographql/apollo-tooling/pull/1908)
- `apollo-tools`
- <First `apollo-tools` related entry goes here>
- `vscode-apollo`
Expand Down
21 changes: 19 additions & 2 deletions packages/apollo-language-server/src/errors/validation.ts
Expand Up @@ -51,7 +51,15 @@ export function getValidationErrors(
rules: ValidationRule[] = defaultValidationRules
) {
const typeInfo = new TypeInfo(schema);
const context = new ValidationContext(schema, document, typeInfo);

const errors: GraphQLError[] = [];

// The fourth `onError` parameter in the constructor of `ValidationContext`
// was introduced in `graphql@14.5.0`. It is safe to use this in pre-14.5.0
// versions too, as there was no fourth parameter in any earlier version.
const context = new ValidationContext(schema, document, typeInfo, error =>
errors.push(error)
);

if (fragments) {
(context as any)._fragments = fragments;
Expand All @@ -60,7 +68,16 @@ export function getValidationErrors(
const visitors = rules.map(rule => rule(context));
// Visit the whole document with each instance of all provided rules.
visit(document, visitWithTypeInfo(typeInfo, visitInParallel(visitors)));
return context.getErrors();

// In `graphql@15.0.0`, `context.getErrors` was removed.
// If this function is available, then we prefer to use it, as the user
// may be on a version of `graphql` that is pre-14.5.0, and therefore `errors`
// will be empty as it's never called above.
// This can be removed in a major release if pre-14.5.0 support is dropped.
if (typeof context.getErrors === "function") {
return context.getErrors();
}
return errors;
}

export function validateQueryDocument(
Expand Down