Skip to content

Commit

Permalink
fix: handle undefined reply object inside mercurius context (#30)
Browse files Browse the repository at this point in the history
* fix: handle undefined reply object inside mercurius context

---------

Co-authored-by: Davide Arena <d.arena@treedom.net>
  • Loading branch information
DavideArena and DavideArena authored Oct 18, 2023
1 parent 0d1d50f commit 79950ce
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ Here a complete example when you turn on all the log options:
}
```

If the [mercurius `graphql` decorator](https://github.com/mercurius-js/mercurius/blob/master/docs/api/options.md#appgraphqlsource-context-variables-operationname) is used, it is necessary to provide a `context` object: `app.graphql(query, { reply })`.
Otherwise, this plugin will ignore the request.

## Install

```
Expand Down
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ function mercuriusLogging (app, opts, next) {
}

function logGraphQLDetails (opts, schema, document, context) {
// Reply object could be undefined. In this case, we can't log.
if (!context.reply) {
return
}

const queryOps = readOps(document, 'query', opts)
const mutationOps = readOps(document, 'mutation', opts)

Expand Down
75 changes: 75 additions & 0 deletions test/handle-reply-undefined.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
'use strict'

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

test('should handle without logging when context.reply is undefined', async (t) => {
t.plan(1)

const app = buildApp(t)

app.get('/custom-endpoint', async function () {
const query = `query {
four: add(x: 2, y: 2)
six: add(x: 3, y: 3)
echo(msg: "hello")
counter
}`
return app.graphql(query)
})

const response = await app.inject({
method: 'GET',
headers: { 'content-type': 'application/json' },
url: '/custom-endpoint'
})

t.deepEqual(response.json(), {
data: {
four: 4,
six: 6,
echo: 'hellohello',
counter: 0
}
})
})

test('should log when using graphql mercurius decorator providing reply object inside context', 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 })

app.get('/custom-endpoint', async function (_, reply) {
const query = `query {
four: add(x: 2, y: 2)
six: add(x: 3, y: 3)
echo(msg: "hello")
counter
}`
return app.graphql(query, { reply })
})

const response = await app.inject({
method: 'GET',
headers: { 'content-type': 'application/json' },
url: '/custom-endpoint'
})

t.deepEqual(response.json(), {
data: {
four: 4,
six: 6,
echo: 'hellohello',
counter: 0
}
})
})

0 comments on commit 79950ce

Please sign in to comment.