Skip to content

Commit

Permalink
Account for the deprecation of ValidationContext.prototype.getErrors.
Browse files Browse the repository at this point in the history
We'll use the new `onError` callback which was introduced in
`graphql@14.5.0` by graphql/graphql-js#2074 only if
the `getErrors` method that existed prior to that addition no longer exists.
  • Loading branch information
abernix committed Jan 3, 2020
1 parent 71ec6a5 commit 1633903
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions packages/apollo-language-server/src/errors/validation.ts
Expand Up @@ -51,7 +51,18 @@ export function getValidationErrors(
rules: ValidationRule[] = defaultValidationRules
) {
const typeInfo = new TypeInfo(schema);
const context = new ValidationContext(schema, document, typeInfo);

// The 4th argument to `ValidationContext` is an `onError` callback. This was
// introduced by https://github.com/graphql/graphql-js/pull/2074 and first
// published in graphql@14.5.0. It is meant to replace the `getErrors` method
// which was previously used. Since we support versions of graphql older than
// that, it's possible that this callback will not be invoked and we'll need
// to resort to using `getErrors`. Therefore, although we'll collect errors
// via this callback, if `getErrors` is present on the context we create,
// we'll go ahead and use that instead.
const errors: GraphQLError[] = [];
const onError = (err: GraphQLError) => errors.push(err);
const context = new ValidationContext(schema, document, typeInfo, onError);

if (fragments) {
(context as any)._fragments = fragments;
Expand All @@ -60,7 +71,14 @@ 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();

// @ts-ignore
// `getErrors` is gone in `graphql@15`, but we still support older versions.
if (typeof context.getErrors === "function") return context.getErrors();

// If `getErrors` doesn't exist, we must be on a `graphql@15` or higher,
// so we'll use the errors we collected via the `onError` callback.
return errors;
}

export function validateQueryDocument(
Expand Down

0 comments on commit 1633903

Please sign in to comment.