Skip to content

Commit

Permalink
Refactor: Delegate all handling to core (#152)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
barelyhuman and simoneb authored Mar 13, 2024
1 parent d4c4a98 commit 424c5a9
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 528 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ coverage

# OS X
.DS_Store

# yalc
.yalc
yalc.lock
33 changes: 5 additions & 28 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ const mercurius = require('mercurius')

const PLUGIN_NAME = 'mercuriusDynamicSchema'
const STRATEGY_NAME = 'mercuriusDynamicSchemaStrategy'
const kRequestContext = Symbol('request context')

function strategyFactory({ name, strategy }) {
return {
Expand Down Expand Up @@ -39,35 +38,13 @@ async function mercuriusDynamicSchema(fastify, opts) {
async childServer => {
childServer.register(mercurius, {
schema: schema.schema,
path: schema.path,
resolvers: schema.resolvers,
graphiql: false,
routes: false
})

childServer.route({
path: schema?.path ?? '/graphql',
method: 'POST',
constraints: { [STRATEGY_NAME]: schema.name },
handler: async (req, reply) => {
let { query, operationName, variables } = req.body

if (typeof req.body === 'string') {
query = req.body
}

if (contextFn) {
req[kRequestContext] = await contextFn(req, reply)
Object.assign(req[kRequestContext], { reply, childServer })
} else {
req[kRequestContext] = { reply, app: childServer }
}

return reply.graphql(
query,
req[kRequestContext],
variables,
operationName
)
context: contextFn,
routes: true,
additionalRouteOptions: {
constraints: { [STRATEGY_NAME]: schema.name }
}
})
},
Expand Down
Loading

0 comments on commit 424c5a9

Please sign in to comment.