1
1
import { mergeSchemas } from '@graphql-tools/schema'
2
- import type { GraphQLFieldResolver , GraphQLField , GraphQLSchema } from 'graphql'
2
+ import type { GraphQLField , GraphQLSchema } from 'graphql'
3
3
import { buildSchema , validateSchema , GraphQLObjectType , execute , GraphQLError } from 'graphql'
4
4
import { compose } from './compose'
5
- import { Context } from './context'
6
- import type { ExecutionContext , Environment , Middleware } from './types'
5
+ import { Context } from './context/context '
6
+ import type { ExecutionContext , Environment , Middleware , Handler } from './types'
7
7
8
8
export class EdgeQL {
9
9
private schemas : GraphQLSchema [ ] = [ ]
@@ -24,7 +24,7 @@ export class EdgeQL {
24
24
25
25
let ctx : Context
26
26
try {
27
- ctx = await Context . create ( request , env , exeContext , this . graph )
27
+ ctx = new Context ( request , env , exeContext )
28
28
} catch ( e ) {
29
29
return new Response (
30
30
JSON . stringify ( {
@@ -40,15 +40,18 @@ export class EdgeQL {
40
40
)
41
41
}
42
42
43
- await compose ( [ ...this . middlewares , this . handle ] ) ( ctx )
43
+ await ctx . graphql . init ( ctx . http . request )
44
+ ctx . graphql . schema = this . graph
45
+
46
+ await compose ( [ ...this . middlewares , this . execute ] ) ( ctx )
44
47
45
48
return ctx . json ( )
46
49
}
47
50
48
- async handle ( ctx : Context ) {
49
- if ( ! ctx . req ? .query ) {
50
- ctx . res . status = 400
51
- ctx . res . body = {
51
+ async execute ( ctx : Context ) {
52
+ if ( ! ctx . graphql . query ) {
53
+ ctx . http . status = 400
54
+ ctx . http . body = {
52
55
data : null ,
53
56
errors : [
54
57
new GraphQLError ( 'Must provide query string' , {
@@ -61,12 +64,12 @@ export class EdgeQL {
61
64
return
62
65
}
63
66
64
- if ( ! ctx . req ? .document ) {
65
- ctx . res . status = 400
66
- ctx . res . body = {
67
+ if ( ! ctx . graphql . document ) {
68
+ ctx . http . status = 400
69
+ ctx . http . body = {
67
70
data : null ,
68
71
errors : [
69
- new GraphQLError ( `could not generate document from query: ${ ctx . req ? .query } ` , {
72
+ new GraphQLError ( `could not generate document from query: ${ ctx . graphql . query } ` , {
70
73
extensions : {
71
74
status : 400 ,
72
75
} ,
@@ -76,9 +79,9 @@ export class EdgeQL {
76
79
return
77
80
}
78
81
79
- if ( ! ctx . schema ) {
80
- ctx . res . status = 400
81
- ctx . res . body = {
82
+ if ( ! ctx . graphql . schema ) {
83
+ ctx . http . status = 400
84
+ ctx . http . body = {
82
85
data : null ,
83
86
errors : [
84
87
new GraphQLError ( 'no schem registerred yet' , {
@@ -93,19 +96,19 @@ export class EdgeQL {
93
96
94
97
try {
95
98
const res = await execute ( {
96
- schema : ctx . schema ! ,
97
- document : ctx . req ? .document ?? null ,
99
+ schema : ctx . graphql . schema ! ,
100
+ document : ctx . graphql . document ?? null ,
98
101
rootValue : null ,
99
102
contextValue : ctx ,
100
- variableValues : ctx . req ? .variables ,
101
- operationName : ctx . req ? .operationName ,
103
+ variableValues : ctx . graphql . variables ,
104
+ operationName : ctx . graphql . operationName ,
102
105
} )
103
- ctx . res . status = 200
104
- ctx . res . body = res
106
+ ctx . http . status = 200
107
+ ctx . http . body = res
105
108
return
106
109
} catch ( contextError : unknown ) {
107
- ctx . res . status = 500
108
- ctx . res . body = {
110
+ ctx . http . status = 500
111
+ ctx . http . body = {
109
112
data : null ,
110
113
errors : [
111
114
new GraphQLError ( `GraphQL execution context error: ${ contextError } ` , {
@@ -122,15 +125,10 @@ export class EdgeQL {
122
125
this . middlewares . push ( fn )
123
126
}
124
127
125
- register ( schema : GraphQLSchema ) : void
126
- register ( schema : string , resolve : GraphQLFieldResolver < any , any , any , any > ) : void
127
- register ( schema : string , resolves : Record < string , GraphQLFieldResolver < any , any , any , any > > ) : void
128
- register (
129
- ...args :
130
- | [ GraphQLSchema ]
131
- | [ string , GraphQLFieldResolver < any , any , any , any > ]
132
- | [ string , Record < string , GraphQLFieldResolver < any , any , any , any > > ]
133
- ) : void {
128
+ handle ( schema : GraphQLSchema ) : void
129
+ handle ( schema : string , handler : Handler ) : void
130
+ handle ( schema : string , handlers : Record < string , Handler > ) : void
131
+ handle ( ...args : [ GraphQLSchema ] | [ string , Handler ] | [ string , Record < string , Handler > ] ) : void {
134
132
if ( args . length === 1 && typeof args [ 0 ] === 'object' ) {
135
133
const schemaValidationErrors = validateSchema ( args [ 0 ] )
136
134
if ( schemaValidationErrors . length > 0 ) {
@@ -153,7 +151,12 @@ export class EdgeQL {
153
151
if ( fields . length !== 1 ) {
154
152
throw new Error ( 'only one of Query, Mutuation, Subscription is allowed' )
155
153
} else {
156
- fields [ 0 ] . resolve = args [ 1 ]
154
+ fields [ 0 ] . resolve = async ( parents : any , arg : any , ctx : Context , info : any ) => {
155
+ ctx . graphql . args = arg
156
+ ctx . graphql . parents = parents
157
+ ctx . graphql . info = info
158
+ return await ( args [ 1 ] as Handler ) ( ctx )
159
+ }
157
160
}
158
161
159
162
this . schemas . push ( s )
@@ -166,7 +169,18 @@ export class EdgeQL {
166
169
if ( obj instanceof GraphQLObjectType ) {
167
170
for ( const f of Object . keys ( obj . getFields ( ) ) ) {
168
171
if ( args [ 1 ] [ f ] ) {
169
- obj . getFields ( ) [ f ] . resolve = args [ 1 ] [ f ]
172
+ obj . getFields ( ) [ f ] . resolve = async (
173
+ parents : any ,
174
+ arg : any ,
175
+ ctx : Context ,
176
+ info : any
177
+ ) => {
178
+ ctx . graphql . parents = parents
179
+ ctx . graphql . args = arg
180
+ ctx . graphql . info = info
181
+ const fn = ( args [ 1 ] as any ) [ f ] as Handler
182
+ return await fn ( ctx )
183
+ }
170
184
} else {
171
185
throw new Error ( `no resolve function for ${ obj . getFields ( ) [ f ] } ` )
172
186
}
0 commit comments