From 6bf3520300cde342e6b842066d5d4f68b11bec77 Mon Sep 17 00:00:00 2001 From: Reaper Gelera Date: Wed, 7 Feb 2024 10:23:18 +0530 Subject: [PATCH] refactor: delegate all handling to core Also sync tests to match output of core --- .gitignore | 4 +++ index.js | 33 ++++----------------- package.json | 3 +- test/index.test.js | 72 +++++++++++++++++++++++++++++----------------- 4 files changed, 56 insertions(+), 56 deletions(-) diff --git a/.gitignore b/.gitignore index 80226e3..c32ca32 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,7 @@ coverage # OS X .DS_Store + +# yalc +.yalc +yalc.lock \ No newline at end of file diff --git a/index.js b/index.js index 8a1efa9..eccee89 100644 --- a/index.js +++ b/index.js @@ -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 { @@ -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 } } }) }, diff --git a/package.json b/package.json index 4f7468f..8a5d283 100644 --- a/package.json +++ b/package.json @@ -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": "^19.0.3", diff --git a/test/index.test.js b/test/index.test.js index 6c2f9b6..41c6d2b 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -54,10 +54,12 @@ 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' @@ -65,10 +67,18 @@ tap.test('schema validation', async t => { }) 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) @@ -95,9 +105,11 @@ 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' @@ -105,10 +117,13 @@ tap.test('schema validation', async t => { }) 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) @@ -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' @@ -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' @@ -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' @@ -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' }) @@ -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' }) @@ -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',