-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathmiddleware.ts
163 lines (150 loc) · 4.11 KB
/
middleware.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
import { GraphQLSchema } from 'graphql'
import { addResolversToSchema } from '@graphql-tools/schema'
import {
IApplyOptions,
IMiddleware,
FragmentReplacement,
IMiddlewareGenerator,
GraphQLSchemaWithFragmentReplacements,
} from './types'
import { generateResolverFromSchemaAndMiddleware } from './applicator'
import { validateMiddleware } from './validation'
import { extractFragmentReplacements } from './fragments'
import { isMiddlewareGenerator } from './utils'
/**
*
* @param schema
* @param options
* @param middleware
*
* Validates middleware and generates resolvers map for provided middleware.
* Applies middleware to the current schema and returns the modified one.
*
*/
export function addMiddlewareToSchema<TSource, TContext, TArgs>(
schema: GraphQLSchema,
options: IApplyOptions,
middleware: IMiddleware<TSource, TContext, TArgs>,
): {
schema: GraphQLSchema
fragmentReplacements: FragmentReplacement[]
} {
const validMiddleware = validateMiddleware(schema, middleware)
const resolvers = generateResolverFromSchemaAndMiddleware(
schema,
options,
validMiddleware,
)
const fragmentReplacements = extractFragmentReplacements(resolvers)
const newSchema = addResolversToSchema({
schema,
resolvers,
updateResolversInPlace: false,
resolverValidationOptions: {
requireResolversForResolveType: 'ignore',
},
})
return { schema: newSchema, fragmentReplacements }
}
/**
*
* @param schema
* @param options
* @param middlewares
*
* Generates middleware from middleware generators and applies middleware to
* resolvers. Returns generated schema with all provided middleware.
*
*/
function applyMiddlewareWithOptions<TSource = any, TContext = any, TArgs = any>(
schema: GraphQLSchema,
options: IApplyOptions,
...middlewares: (
| IMiddleware<TSource, TContext, TArgs>
| IMiddlewareGenerator<TSource, TContext, TArgs>
)[]
): GraphQLSchemaWithFragmentReplacements {
const normalisedMiddlewares = middlewares.map((middleware) => {
if (isMiddlewareGenerator(middleware)) {
return middleware.generate(schema)
} else {
return middleware
}
})
const schemaWithMiddlewareAndFragmentReplacements = normalisedMiddlewares.reduceRight(
(
{
schema: currentSchema,
fragmentReplacements: currentFragmentReplacements,
},
middleware,
) => {
const {
schema: newSchema,
fragmentReplacements: newFragmentReplacements,
} = addMiddlewareToSchema(currentSchema, options, middleware)
return {
schema: newSchema,
fragmentReplacements: [
...currentFragmentReplacements,
...newFragmentReplacements,
],
}
},
{ schema, fragmentReplacements: [] },
)
const schemaWithMiddleware: GraphQLSchemaWithFragmentReplacements =
schemaWithMiddlewareAndFragmentReplacements.schema
schemaWithMiddleware.schema =
schemaWithMiddlewareAndFragmentReplacements.schema
schemaWithMiddleware.fragmentReplacements =
schemaWithMiddlewareAndFragmentReplacements.fragmentReplacements
return schemaWithMiddleware
}
// Exposed functions
/**
*
* @param schema
* @param middlewares
*
* Apply middleware to resolvers and return generated schema.
*
*/
export function applyMiddleware<TSource = any, TContext = any, TArgs = any>(
schema: GraphQLSchema,
...middlewares: (
| IMiddleware<TSource, TContext, TArgs>
| IMiddlewareGenerator<TSource, TContext, TArgs>
)[]
): GraphQLSchemaWithFragmentReplacements {
return applyMiddlewareWithOptions(
schema,
{ onlyDeclaredResolvers: false },
...middlewares,
)
}
/**
*
* @param schema
* @param middlewares
*
* Apply middleware to declared resolvers and return new schema.
*
*/
export function applyMiddlewareToDeclaredResolvers<
TSource = any,
TContext = any,
TArgs = any
>(
schema: GraphQLSchema,
...middlewares: (
| IMiddleware<TSource, TContext, TArgs>
| IMiddlewareGenerator<TSource, TContext, TArgs>
)[]
): GraphQLSchemaWithFragmentReplacements {
return applyMiddlewareWithOptions(
schema,
{ onlyDeclaredResolvers: true },
...middlewares,
)
}