-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathquery.ts
152 lines (140 loc) · 4.64 KB
/
query.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
import { GraphQLFieldConfig, GraphQLInt, GraphQLObjectType } from 'graphql'
import { defaultArgs, defaultListArgs } from 'graphql-sequelize'
import {
GlobalPreCallback,
GraphqlSchemaDeclarationType,
ModelDeclarationType,
OutputTypes,
SequelizeModels,
} from '../../types'
import generateCountResolver from '../queryResolvers/count'
import generateListResolver from '../queryResolvers/list'
function getModelsFields(
allSchemaDeclarations: GraphqlSchemaDeclarationType,
outputTypes: OutputTypes,
models: SequelizeModels,
globalPreCallback: GlobalPreCallback
) {
return Object.keys(outputTypes).reduce((fields, modelTypeName) => {
const modelType = outputTypes[modelTypeName]
const schemaDeclaration = allSchemaDeclarations[
modelType.name
] as ModelDeclarationType
if (typeof schemaDeclaration === 'undefined') {
// If a model is not defined, we just ignore it.
return fields
}
// eslint-disable-next-line no-prototype-builtins
if (!schemaDeclaration.hasOwnProperty('model')) {
throw new Error(
`You provided an empty/undefined model for the endpoint ${modelType}. Please provide a Sequelize model.`
)
}
// One can exclude a given model from the root query.
// It will only be used through associations.
if (schemaDeclaration.excludeFromRoot === true) {
return fields
}
const result =
schemaDeclaration.actions &&
schemaDeclaration.actions.indexOf('count') > -1
? {
...fields,
// LIST RESOLVER
[modelType.name]: generateListResolver(
modelType,
allSchemaDeclarations,
outputTypes,
models,
globalPreCallback
),
// COUNT RESOLVER
[`${modelType.name}Count`]: {
type: GraphQLInt,
args: {
...defaultArgs(schemaDeclaration.model),
...defaultListArgs(),
...(schemaDeclaration.count && schemaDeclaration.count.extraArg
? schemaDeclaration.count.extraArg
: // If not count extraArgs are specified, we use the list one by default
// as we already use the list before by default.
schemaDeclaration.list && schemaDeclaration.list.extraArg
? schemaDeclaration.list.extraArg
: {}),
},
resolve: generateCountResolver(
schemaDeclaration.model,
schemaDeclaration,
globalPreCallback
),
},
}
: {
...fields,
// LIST RESOLVER
[modelType.name]: generateListResolver(
modelType,
allSchemaDeclarations,
outputTypes,
models,
globalPreCallback
),
}
return result
}, {})
}
function getCustomEndpoints(
allSchemaDeclarations: GraphqlSchemaDeclarationType,
outputTypes: OutputTypes
) {
return Object.keys(allSchemaDeclarations).reduce((fields, endpointKey) => {
// We ignore all endpoints matching a model type.
if (outputTypes[endpointKey]) {
return fields
}
const endpointDeclaration = allSchemaDeclarations[
endpointKey
] as GraphQLFieldConfig<any, any, any>
// @todo counts should only be added if configured in the schema declaration
return {
...fields,
// The full endpoints must be manually declared.
[endpointKey]: endpointDeclaration,
}
}, {})
}
/**
* Returns a root `GraphQLObjectType` used as query for `GraphQLSchema`.
*
* It creates an object whose properties are `GraphQLObjectType` created
* from Sequelize models.
* @param {*} models The sequelize models used to create the root `GraphQLSchema`
*/
export default function generateQueryRootResolver(
allSchemaDeclarations: GraphqlSchemaDeclarationType,
outputTypes: any,
models: any,
globalPreCallback: any
) {
// Endpoints depending on a model
const modelFields = getModelsFields(
allSchemaDeclarations,
outputTypes,
models,
globalPreCallback
)
// Custom endpoints, without models specified.
const customEndpoints = getCustomEndpoints(allSchemaDeclarations, outputTypes)
const modelsKeys = Object.keys(modelFields)
Object.keys(customEndpoints).filter((value) => {
if (modelsKeys.indexOf(value) !== -1) {
throw new Error(
`You created the custom endpoint (${value}) on the same key of an already defined model endpoint.`
)
}
})
return new GraphQLObjectType({
name: 'Root_Query',
fields: { ...modelFields, ...customEndpoints },
})
}