-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathgraphQLInsertType.ts
51 lines (38 loc) · 1.91 KB
/
graphQLInsertType.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
import { GraphQLInputObjectType, GraphQLList, GraphQLEnumType, GraphQLNonNull, GraphQLScalarType, GraphQLObjectType, GraphQLInputType } from 'graphql';
import { cache, setSuffix, getUnresolvedFieldsTypes, typesCache, FieldMap } from './common';
export function getGraphQLInsertType(graphQLType: GraphQLObjectType, ...excludedFields: string[]) : GraphQLInputObjectType {
const inputTypeName = setSuffix(graphQLType.name, 'Type', 'InsertType');
return cache(typesCache, inputTypeName, () => new GraphQLInputObjectType({
name: inputTypeName,
fields: getGraphQLInsertTypeFields(graphQLType, ...excludedFields)
}));
}
function getGraphQLInsertTypeFields(graphQLType: GraphQLObjectType, ...excludedFields: string[]) : () => FieldMap<GraphQLInputType> {
return () => {
const fields = getUnresolvedFieldsTypes(graphQLType, getGraphQLInsertTypeNested, ...excludedFields)();
const idField = fields['_id'];
if (idField && idField.type instanceof GraphQLNonNull) {
idField.type = idField.type.ofType;
}
return fields;
};
}
function getGraphQLInsertTypeNested(
type: GraphQLScalarType | GraphQLEnumType | GraphQLNonNull<any> | GraphQLObjectType | GraphQLList<any>,
...excludedFields: string[]) : GraphQLInputType {
if (type instanceof GraphQLScalarType ||
type instanceof GraphQLEnumType) {
return type;
}
if (type instanceof GraphQLNonNull) {
return new GraphQLNonNull(getGraphQLInsertTypeNested(type.ofType));
}
if (type instanceof GraphQLList) {
return new GraphQLList(getGraphQLInsertTypeNested(type.ofType));
}
const inputTypeName = setSuffix(type.name, 'Type', 'InsertType');
return cache(typesCache, inputTypeName, () => new GraphQLInputObjectType({
name: inputTypeName,
fields: getUnresolvedFieldsTypes(type, getGraphQLInsertTypeNested, ...excludedFields)
}));
}