-
-
Notifications
You must be signed in to change notification settings - Fork 796
/
addResolveFunctionsToSchema.ts
229 lines (200 loc) · 6.68 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import {
GraphQLField,
GraphQLEnumType,
GraphQLScalarType,
GraphQLType,
GraphQLSchema,
GraphQLObjectType,
GraphQLInterfaceType,
GraphQLFieldMap,
} from 'graphql';
import {
IResolvers,
IResolverValidationOptions,
IAddResolveFunctionsToSchemaOptions,
} from '../Interfaces';
import SchemaError from './SchemaError';
import checkForResolveTypeResolver from './checkForResolveTypeResolver';
import extendResolversFromInterfaces from './extendResolversFromInterfaces';
import {
parseInputValue,
serializeInputValue,
healSchema,
forEachField,
forEachDefaultValue,
} from '../utils';
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,
defaultFieldResolver,
resolverValidationOptions = {},
inheritResolversFromInterfaces = false,
} = options;
const {
allowResolversNotInSchema = false,
requireResolversForResolveType,
} = resolverValidationOptions;
const resolvers = inheritResolversFromInterfaces
? extendResolversFromInterfaces(schema, inputResolvers)
: inputResolvers;
const typeMap = schema.getTypeMap();
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) {
const config = type.toConfig();
Object.keys(resolverValue).forEach(fieldName => {
// Below is necessary as legacy code for scalar type specification allowed
// hardcoding within the resolver an object with fields '__serialize',
// '__parse', and '__parseLiteral', see examples in testMocking.ts.
// Luckily, the fields on GraphQLScalarType and GraphQLScalarTypeConfig
// are named the same.
if (fieldName.startsWith('__')) {
config[fieldName.substring(2)] = resolverValue[fieldName];
} else {
config[fieldName] = resolverValue[fieldName];
}
});
// healSchema called later to update all fields to new type
typeMap[type.name] = new GraphQLScalarType(config);
} else if (type instanceof GraphQLEnumType) {
// 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
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`,
);
}
});
const config = type.toConfig();
const values = type.getValues();
const newValues = {};
values.forEach(value => {
const newValue = Object.keys(resolverValue).includes(
value.name,
)
? resolverValue[value.name]
: value.name;
newValues[value.name] = {
value: newValue,
deprecationReason: value.deprecationReason,
description: value.description,
astNode: value.astNode,
};
});
// healSchema called later to update all fields to new type
typeMap[type.name] = new GraphQLEnumType({
...config,
values: newValues,
});
} 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
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);
// serialize all default values prior to healing fields with new scalar/enum types.
forEachDefaultValue(schema, serializeInputValue);
// schema may have new scalar/enum types that require healing
healSchema(schema);
// reparse all default values with new parsing functions.
forEachDefaultValue(schema, parseInputValue);
if (defaultFieldResolver) {
forEachField(schema, field => {
if (!field.resolve) {
field.resolve = defaultFieldResolver;
}
});
}
return schema;
}
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;