This repository has been archived by the owner on Apr 15, 2020. It is now read-only.
forked from ardatan/graphql-tools
/
addResolveFunctionsToSchema.ts
187 lines (163 loc) · 5.8 KB
/
addResolveFunctionsToSchema.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import { SchemaError } from '.';
import {
GraphQLField,
GraphQLEnumType,
GraphQLScalarType,
GraphQLType,
GraphQLSchema,
GraphQLObjectType,
GraphQLInterfaceType,
GraphQLFieldMap,
} from 'graphql';
import {
IResolvers,
IResolverValidationOptions,
IAddResolveFunctionsToSchemaOptions,
} from '../Interfaces';
import { applySchemaTransforms } from '../transforms/transforms';
import { checkForResolveTypeResolver, extendResolversFromInterfaces } from '.';
import AddEnumAndScalarResolvers from '../transforms/AddEnumAndScalarResolvers';
function addResolveFunctionsToSchema(
options: IAddResolveFunctionsToSchemaOptions | GraphQLSchema,
legacyInputResolvers?: IResolvers,
legacyInputValidationOptions?: IResolverValidationOptions,
) {
if (options instanceof GraphQLSchema) {
console.warn(
'The addResolveFunctionsToSchema function takes named options now; see IAddResolveFunctionsToSchemaOptions',
);
options = {
schema: options,
resolvers: legacyInputResolvers,
resolverValidationOptions: legacyInputValidationOptions,
};
}
const {
schema,
resolvers: inputResolvers,
resolverValidationOptions = {},
inheritResolversFromInterfaces = false,
} = options;
const {
allowResolversNotInSchema = false,
requireResolversForResolveType,
} = resolverValidationOptions;
const resolvers = inheritResolversFromInterfaces
? extendResolversFromInterfaces(schema, inputResolvers)
: inputResolvers;
// Used to map the external value of an enum to its internal value, when
// that internal value is provided by a resolver.
const enumValueMap = Object.create(null);
// Used to store custom scalar implementations.
const scalarTypeMap = Object.create(null);
Object.keys(resolvers).forEach(typeName => {
const resolverValue = resolvers[typeName];
const resolverType = typeof resolverValue;
if (resolverType !== 'object' && resolverType !== 'function') {
throw new SchemaError(
`"${typeName}" defined in resolvers, but has invalid value "${resolverValue}". A resolver's value ` +
`must be of type object or function.`,
);
}
const type = schema.getType(typeName);
if (!type && typeName !== '__schema') {
if (allowResolversNotInSchema) {
return;
}
throw new SchemaError(
`"${typeName}" defined in resolvers, but not in schema`,
);
}
if (type instanceof GraphQLScalarType) {
scalarTypeMap[type.name] = resolverValue;
} else if (type instanceof GraphQLEnumType) {
Object.keys(resolverValue).forEach(fieldName => {
if (!type.getValue(fieldName)) {
if (allowResolversNotInSchema) {
return;
}
throw new SchemaError(
`${typeName}.${fieldName} was defined in resolvers, but enum is not in schema`,
);
}
// We've encountered an enum resolver that is being used to provide an
// internal enum value.
// Reference: https://www.apollographql.com/docs/graphql-tools/scalars.html#internal-values
//
// We're storing a map of the current enums external facing value to
// its resolver provided internal value. This map is used to transform
// the current schema to a new schema that includes enums with the new
// internal value.
enumValueMap[type.name] = enumValueMap[type.name] || {};
enumValueMap[type.name][fieldName] = resolverValue[fieldName];
});
} else {
// object type
Object.keys(resolverValue).forEach(fieldName => {
if (fieldName.startsWith('__')) {
// this is for isTypeOf and resolveType and all the other stuff.
type[fieldName.substring(2)] = resolverValue[fieldName];
return;
}
const fields = getFieldsForType(type);
if (!fields) {
if (allowResolversNotInSchema) {
return;
}
throw new SchemaError(
`${typeName} was defined in resolvers, but it's not an object`,
);
}
if (!fields[fieldName]) {
if (allowResolversNotInSchema) {
return;
}
throw new SchemaError(
`${typeName}.${fieldName} defined in resolvers, but not in schema`,
);
}
const field = fields[fieldName];
const fieldResolve = resolverValue[fieldName];
if (typeof fieldResolve === 'function') {
// for convenience. Allows shorter syntax in resolver definition file
setFieldProperties(field, { resolve: fieldResolve });
} else {
if (typeof fieldResolve !== 'object') {
throw new SchemaError(
`Resolver ${typeName}.${fieldName} must be object or function`,
);
}
setFieldProperties(field, fieldResolve);
}
});
}
});
checkForResolveTypeResolver(schema, requireResolversForResolveType);
// If there are any enum resolver functions (that are used to return
// internal enum values), create a new schema that includes enums with the
// new internal facing values.
// also parse all defaultValues in all input fields to use internal values for enums/scalars
const updatedSchema = applySchemaTransforms(schema, [
new AddEnumAndScalarResolvers(enumValueMap, scalarTypeMap),
]);
return updatedSchema;
}
function getFieldsForType(type: GraphQLType): GraphQLFieldMap<any, any> {
if (
type instanceof GraphQLObjectType ||
type instanceof GraphQLInterfaceType
) {
return type.getFields();
} else {
return undefined;
}
}
function setFieldProperties(
field: GraphQLField<any, any>,
propertiesObj: Object,
) {
Object.keys(propertiesObj).forEach(propertyName => {
field[propertyName] = propertiesObj[propertyName];
});
}
export default addResolveFunctionsToSchema;