diff --git a/CHANGELOG.md b/CHANGELOG.md index ffec0cb148..ea486c9c80 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ - - `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` - - `vscode-apollo` diff --git a/packages/apollo-language-server/src/errors/validation.ts b/packages/apollo-language-server/src/errors/validation.ts index c9fa38cb4d..d960ad59b1 100644 --- a/packages/apollo-language-server/src/errors/validation.ts +++ b/packages/apollo-language-server/src/errors/validation.ts @@ -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; @@ -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(