-
Notifications
You must be signed in to change notification settings - Fork 470
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Account for the deprecation of
ValidationContext.prototype.getErrors
.
We'll use the new `onError` callback which was introduced in `[email protected]` by graphql/graphql-js#2074 only if the `getErrors` method that existed prior to that addition no longer exists.
- Loading branch information
Showing
1 changed file
with
20 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 [email protected]. 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( | ||
|