Skip to content

Commit 5f6ce90

Browse files
authored
Merge pull request #834 from graphql/export-validate-with
Allow passing custom TypeInfo to validate()
2 parents d18a10d + c3f5ab8 commit 5f6ce90

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

src/validation/__tests__/validation-test.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import { describe, it } from 'mocha';
1111
import { expect } from 'chai';
1212
import { testSchema } from './harness';
1313
import { validate, specifiedRules } from '../';
14-
import { visitUsingRules } from '../validate';
1514
import { parse } from '../../language';
1615
import { TypeInfo } from '../../utilities/TypeInfo';
1716

@@ -57,12 +56,7 @@ describe('Validate: Supports full validation', () => {
5756
}
5857
`);
5958

60-
const errors = visitUsingRules(
61-
testSchema,
62-
typeInfo,
63-
ast,
64-
specifiedRules
65-
);
59+
const errors = validate(testSchema, ast, specifiedRules, typeInfo);
6660

6761
const errorMessages = errors.map(err => err.message);
6862

src/validation/validate.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,15 @@ import { specifiedRules } from './specifiedRules';
4545
* Each validation rules is a function which returns a visitor
4646
* (see the language/visitor API). Visitor methods are expected to return
4747
* GraphQLErrors, or Arrays of GraphQLErrors when invalid.
48+
*
49+
* Optionally a custom TypeInfo instance may be provided. If not provided, one
50+
* will be created from the provided schema.
4851
*/
4952
export function validate(
5053
schema: GraphQLSchema,
5154
ast: DocumentNode,
52-
rules?: Array<any>
55+
rules?: Array<any>,
56+
typeInfo?: TypeInfo,
5357
): Array<GraphQLError> {
5458
invariant(schema, 'Must provide schema');
5559
invariant(ast, 'Must provide document');
@@ -58,8 +62,12 @@ export function validate(
5862
'Schema must be an instance of GraphQLSchema. Also ensure that there are ' +
5963
'not multiple versions of GraphQL installed in your node_modules directory.'
6064
);
61-
const typeInfo = new TypeInfo(schema);
62-
return visitUsingRules(schema, typeInfo, ast, rules || specifiedRules);
65+
return visitUsingRules(
66+
schema,
67+
typeInfo || new TypeInfo(schema),
68+
ast,
69+
rules || specifiedRules
70+
);
6371
}
6472

6573
/**

0 commit comments

Comments
 (0)