1 | import { getNamedType, isScalarType } from 'graphql';
|
2 | import { forEachField } from '@graphql-tools/utils';
|
3 | export function assertResolversPresent(schema, resolverValidationOptions = {}) {
|
4 | const { requireResolversForArgs, requireResolversForNonScalar, requireResolversForAllFields } = resolverValidationOptions;
|
5 | if (requireResolversForAllFields && (requireResolversForArgs || requireResolversForNonScalar)) {
|
6 | throw new TypeError('requireResolversForAllFields takes precedence over the more specific assertions. ' +
|
7 | 'Please configure either requireResolversForAllFields or requireResolversForArgs / ' +
|
8 | 'requireResolversForNonScalar, but not a combination of them.');
|
9 | }
|
10 | forEachField(schema, (field, typeName, fieldName) => {
|
11 |
|
12 | if (requireResolversForAllFields) {
|
13 | expectResolver('requireResolversForAllFields', requireResolversForAllFields, field, typeName, fieldName);
|
14 | }
|
15 |
|
16 | if (requireResolversForArgs && field.args.length > 0) {
|
17 | expectResolver('requireResolversForArgs', requireResolversForArgs, field, typeName, fieldName);
|
18 | }
|
19 |
|
20 | if (requireResolversForNonScalar !== 'ignore' && !isScalarType(getNamedType(field.type))) {
|
21 | expectResolver('requireResolversForNonScalar', requireResolversForNonScalar, field, typeName, fieldName);
|
22 | }
|
23 | });
|
24 | }
|
25 | function expectResolver(validator, behavior, field, typeName, fieldName) {
|
26 | if (!field.resolve) {
|
27 | const message = `Resolver missing for "${typeName}.${fieldName}".
|
28 | To disable this validator, use:
|
29 | resolverValidationOptions: {
|
30 | ${validator}: 'ignore'
|
31 | }`;
|
32 | if (behavior === 'error') {
|
33 | throw new Error(message);
|
34 | }
|
35 | if (behavior === 'warn') {
|
36 | console.warn(message);
|
37 | }
|
38 | return;
|
39 | }
|
40 | if (typeof field.resolve !== 'function') {
|
41 | throw new Error(`Resolver "${typeName}.${fieldName}" must be a function`);
|
42 | }
|
43 | }
|