Skip to content

Commit

Permalink
refactor: delegate all handling to core
Browse files Browse the repository at this point in the history
Also sync tests to match output of core
  • Loading branch information
barelyhuman committed Feb 7, 2024
1 parent ac45409 commit 5d6095e
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 56 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,
additionalRouteProps: {
constraints: { [STRATEGY_NAME]: schema.name }
}
})
},
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
"mercurius": "^12.2.0"
},
"dependencies": {
"fastify-plugin": "^4.5.1"
"fastify-plugin": "^4.5.1",
"mercurius": "file:.yalc/mercurius"
},
"devDependencies": {
"@commitlint/cli": "^18.0.0",
Expand Down
72 changes: 45 additions & 27 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,31 @@ tap.test('schema validation', async t => {
})

const res = await app.inject({
method: 'POST',
method: 'GET',
url: '/graphql',
// subtract isn't a Query on schema1, therefore this throws a validation error
payload: '{ subtract(x: 1, y: 2) }',
query: {
query: '{ subtract(x: 1, y: 2) }'
},
headers: {
schema: 'schema1',
'Content-Type': 'text/plain'
}
})

const expectedResult = {
statusCode: 400,
code: 'MER_ERR_GQL_VALIDATION',
error: 'Bad Request',
message: 'Graphql validation error'
data: null,
errors: [
{
message: 'Cannot query field "subtract" on type "Query".',
locations: [
{
line: 1,
column: 3
}
]
}
]
}

t.equal(res.statusCode, 400)
Expand All @@ -95,20 +105,25 @@ tap.test('schema validation', async t => {
})

const res = await app.inject({
method: 'POST',
method: 'GET',
url: '/graphql',
payload: '{ add(x: 1, y: 2 }',
query: {
query: '{ add(x: 1, y: 2 }'
},
headers: {
schema: 'schema1',
'Content-Type': 'text/plain'
}
})

const expectedResult = {
statusCode: 400,
code: 'MER_ERR_GQL_VALIDATION',
error: 'Bad Request',
message: 'Graphql validation error'
data: null,
errors: [
{
message: 'Syntax Error: Expected Name, found "}".',
locations: [{ line: 1, column: 18 }]
}
]
}

t.equal(res.statusCode, 400)
Expand Down Expand Up @@ -138,9 +153,8 @@ tap.test('schema selection', async t => {
})

const response = await app.inject({
method: 'POST',
url: '/graphql',
payload: '{ add(x: 1, y: 2) }',
method: 'GET',
url: '/graphql?query={add(x: 1, y: 2)}',
headers: {
schema: 'schema1',
'Content-Type': 'text/plain'
Expand All @@ -158,9 +172,8 @@ tap.test('schema selection', async t => {
)

const response1 = await app.inject({
method: 'POST',
url: '/graphql',
payload: '{ subtract(x: 1, y: 2) }',
method: 'GET',
url: '/graphql?query={subtract(x: 1, y: 2)}',
headers: {
schema: 'schema2',
'Content-Type': 'text/plain'
Expand Down Expand Up @@ -200,9 +213,8 @@ tap.test('schema selection', async t => {
})

const response = await app.inject({
method: 'POST',
url: '/graphql',
payload: '{ add(x: 1, y: 2) }',
method: 'GET',
url: '/graphql?query={add(x: 1, y: 2)}',
headers: {
schema: 'schema2',
'Content-Type': 'text/plain'
Expand Down Expand Up @@ -238,8 +250,10 @@ tap.test('path definitions', async t => {
'Content-Type': 'text/plain',
schema: 'schema1'
},
method: 'POST',
payload: '{ add(x: 1, y: 2) }',
method: 'GET',
query: {
query: '{ add(x: 1, y: 2) }'
},
url: '/graphql'
})

Expand Down Expand Up @@ -271,8 +285,10 @@ tap.test('path definitions', async t => {
'Content-Type': 'text/plain',
schema: 'schema1'
},
method: 'POST',
payload: '{ add(x: 1, y: 2) }',
method: 'GET',
query: {
query: '{ add(x: 1, y: 2) }'
},
url: '/custom-path'
})

Expand Down Expand Up @@ -313,9 +329,11 @@ tap.test('context sharing between mercurius instances', async t => {
})

const response = await app.inject({
method: 'POST',
method: 'GET',
url: '/graphql',
payload: '{ subtract(x: 1, y: 2) }',
query: {
query: '{ subtract(x: 1, y: 2) }'
},
headers: {
schema: 'schema2',
'Content-Type': 'text/plain',
Expand Down

0 comments on commit 5d6095e

Please sign in to comment.