diff --git a/packages/apollo-language-server/src/errors/validation.ts b/packages/apollo-language-server/src/errors/validation.ts index c9fa38cb4d..e7c23831d2 100644 --- a/packages/apollo-language-server/src/errors/validation.ts +++ b/packages/apollo-language-server/src/errors/validation.ts @@ -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; @@ -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(