Skip to content

Commit 424c5a9

Browse files
barelyhumansimoneb
andauthored
Refactor: Delegate all handling to core (#152)
* refactor: delegate all handling to core Also sync tests to match output of core * chore: point to latest mercurius * chore: add mercurius to dev dependencies * fix: prop name --------- Co-authored-by: Simone Busoli <simone.busoli@gmail.com>
1 parent d4c4a98 commit 424c5a9

File tree

5 files changed

+178
-528
lines changed

5 files changed

+178
-528
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,7 @@ coverage
1010

1111
# OS X
1212
.DS_Store
13+
14+
# yalc
15+
.yalc
16+
yalc.lock

index.js

+5-28
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ const mercurius = require('mercurius')
33

44
const PLUGIN_NAME = 'mercuriusDynamicSchema'
55
const STRATEGY_NAME = 'mercuriusDynamicSchemaStrategy'
6-
const kRequestContext = Symbol('request context')
76

87
function strategyFactory({ name, strategy }) {
98
return {
@@ -39,35 +38,13 @@ async function mercuriusDynamicSchema(fastify, opts) {
3938
async childServer => {
4039
childServer.register(mercurius, {
4140
schema: schema.schema,
41+
path: schema.path,
4242
resolvers: schema.resolvers,
4343
graphiql: false,
44-
routes: false
45-
})
46-
47-
childServer.route({
48-
path: schema?.path ?? '/graphql',
49-
method: 'POST',
50-
constraints: { [STRATEGY_NAME]: schema.name },
51-
handler: async (req, reply) => {
52-
let { query, operationName, variables } = req.body
53-
54-
if (typeof req.body === 'string') {
55-
query = req.body
56-
}
57-
58-
if (contextFn) {
59-
req[kRequestContext] = await contextFn(req, reply)
60-
Object.assign(req[kRequestContext], { reply, childServer })
61-
} else {
62-
req[kRequestContext] = { reply, app: childServer }
63-
}
64-
65-
return reply.graphql(
66-
query,
67-
req[kRequestContext],
68-
variables,
69-
operationName
70-
)
44+
context: contextFn,
45+
routes: true,
46+
additionalRouteOptions: {
47+
constraints: { [STRATEGY_NAME]: schema.name }
7148
}
7249
})
7350
},

0 commit comments

Comments
 (0)