-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
329 additions
and
231 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } }) | ||
}) |
Oops, something went wrong.