Skip to content

Commit

Permalink
fix: handle GET request (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
Eomm authored Aug 15, 2023
1 parent b550029 commit b190e29
Show file tree
Hide file tree
Showing 4 changed files with 329 additions and 231 deletions.
12 changes: 8 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,22 @@ function logGraphQLDetails (opts, schema, document, context) {
const queryOps = readOps(document, 'query', opts)
const mutationOps = readOps(document, 'mutation', opts)

const requestBody = context.reply.request.method !== 'GET'
? context.reply.request.body
: context.reply.request.query

// operationName can be provided at the request level, or it can be provided by query/mutation.
// If it's provided by both, the request level takes precedence.
const operationName = context.reply.request.body.operationName || readOperationName(document)
const operationName = requestBody.operationName || readOperationName(document)
const isCurrentOperation = (op) => op.operationName === operationName

// Runs on a single operation at a time in a batched query, so we need to pull out
// the relevant operation from the batch to be able to log variables for it.
const isBatch = Array.isArray(context.reply.request.body)
const isBatch = Array.isArray(requestBody)
const isDetailedLog = opts.logVariables || opts.logBody
const currentBody = isDetailedLog && isBatch
? context.reply.request.body.find(isCurrentOperation)
: context.reply.request.body
? requestBody.find(isCurrentOperation)
: requestBody

context.reply.request.log[opts.logLevel]({
req: opts.logRequest === true ? context.reply.request : undefined,
Expand Down
68 changes: 68 additions & 0 deletions test/_helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
'use strict'

const Fastify = require('fastify')
const mercurius = require('mercurius')
const split = require('split2')

const mercuriusLogging = require('..')

const schema = `
type Query {
echo(msg: String!): String
add(x: Int!, y: Int!): Int
counter: Int!
}
type Mutation {
plusOne: Int
minusOne: Int
}
`

let counter = 0

const resolvers = {
Query: {
echo: async (_, args) => {
const { msg } = args
return msg.repeat(2)
},
add: async (_, args) => {
const { x, y } = args
return x + y
},
counter: async () => {
return counter
}
},
Mutation: {
plusOne: async (_, args) => {
return ++counter
},
minusOne: async (_, args) => {
return --counter
}
}
}

exports.buildApp = function buildApp (t, logger, opts) {
const app = Fastify({
logger,
disableRequestLogging: true
})
t.teardown(app.close.bind(app))

app.register(mercurius, {
schema,
resolvers,
allowBatchedQueries: true
})
app.register(mercuriusLogging, opts)

return app
}

exports.jsonLogger = function jsonLogger (onData) {
const stream = split(JSON.parse)
stream.on('data', onData)
return stream
}
85 changes: 85 additions & 0 deletions test/issue-22.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
'use strict'

const test = require('ava')
const { buildApp, jsonLogger } = require('./_helper')

test('should deal GET request', async (t) => {
t.plan(4)

const stream = jsonLogger(
line => {
t.is(line.req, undefined)
t.is(line.reqId, 'req-1')
t.deepEqual(line.graphql, {
queries: ['add', 'add', 'echo', 'counter']
})
})

const app = buildApp(t, { stream })

const query = `query {
four: add(x: 2, y: 2)
six: add(x: 3, y: 3)
echo(msg: "hello")
counter
}`

const response = await app.inject({
method: 'GET',
headers: { 'content-type': 'application/json' },
url: '/graphql',
query: { query }
})
t.deepEqual(response.json(), {
data: {
four: 4,
six: 6,
echo: 'hellohello',
counter: 0
}
})
})

test('should log the whole request when operationName is set', async (t) => {
t.plan(3)

const query = `
query boom($num: Int!) {
a: add(x: $num, y: $num)
b: add(x: $num, y: $num)
}
query baam($num: Int!, $bin: Int!) {
c: add(x: $num, y: $bin)
d: add(x: $num, y: $bin)
}
`

const stream = jsonLogger(
line => {
t.is(line.reqId, 'req-1')
t.deepEqual(line.graphql, {
queries: ['add', 'add', 'add', 'add'],
operationName: 'baam',
body: query,
variables: '{"num":2,"bin":3}'
})
})

const app = buildApp(t, { stream }, {
logBody: true,
logVariables: true
})

const response = await app.inject({
method: 'GET',
headers: { 'content-type': 'application/json' },
url: '/graphql',
query: {
query,
operationName: 'baam',
variables: JSON.stringify({ num: 2, bin: 3 })
}
})

t.deepEqual(response.json(), { data: { c: 5, d: 5 } })
})
Loading

0 comments on commit b190e29

Please sign in to comment.