Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: Delegate all handling to core #152

Merged
merged 4 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading