Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log GraphQL variables and errors in responses. #2

Merged
merged 2 commits into from
May 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 64 additions & 6 deletions src/__tests__/createLoggerMiddleware-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,31 @@ const createServer = logger => {
server.get('/500', () => {
throw new Error('500');
});
server.post('/graphql', (req, res) => res.json({data: {publicId: '1'}}));
server.post('/graphql', (req, res) => {
if (req.body.operationName === 'error') {
res.status(500);
res.json({
data: {
errors: [
{
message: '401: Unauthorized',
locations: [{line: 3, column: 3}],
path: ['pizzas'],
extensions: {
statusCode: 401,
statusText: 'Unauthorized',
responseText: 'Unauthorized',
method: 'GET',
url: 'https://api.example.com/pizzas'
}
}
]
}
});
} else {
res.json({data: {publicId: '1'}});
}
});
return server;
};

Expand Down Expand Up @@ -50,20 +74,28 @@ describe('createLoggerMiddleware', () => {
await supertest(server)
.post('/graphql')
.send(mockGraphQLPayload);
await supertest(server)
.post('/graphql')
.send({...mockGraphQLPayload, operationName: 'error'});
await supertest(server).get('/500');

const stdoutCalls = process.stdout.write.mock.calls.map(call => call[0]);
expect(stdoutCalls[0]).toMatch(/DEBUG: HTTP GET \/ statusCode=200, url=\//);
expect(stdoutCalls[0]).toMatch(
/DEBUG: HTTP GET \/ statusCode=200,.*url=\//
);
expect(stdoutCalls[1]).toMatch(
/WARN: HTTP GET \/404 statusCode=404, url=\/404/
/WARN: HTTP GET \/404 statusCode=404,[\s\S]*url=\/404/
);
expect(stdoutCalls[2]).toMatch(
/DEBUG: HTTP POST \/graphql statusCode=200, url=\/graphql/
/DEBUG: HTTP POST \/graphql statusCode=200,.*url=\/graphql/
);

const stderrCalls = process.stderr.write.mock.calls.map(call => call[0]);
expect(stderrCalls[0]).toMatch(
/ERROR: HTTP GET \/500 statusCode=500, url=\/500/
/ERROR: HTTP POST \/graphql statusCode=500[\s\S]*url=\/graphql/
);
expect(stderrCalls[1]).toMatch(
/ERROR: HTTP GET \/500 statusCode=500[\s\S]*url=\/500/
);

logger.destroy();
Expand All @@ -78,6 +110,9 @@ describe('createLoggerMiddleware', () => {
await supertest(server)
.post('/graphql')
.send(mockGraphQLPayload);
await supertest(server)
.post('/graphql')
.send({...mockGraphQLPayload, operationName: 'error'});
await supertest(server).get('/500');

const stdoutCalls = process.stdout.write.mock.calls.map(call =>
Expand All @@ -99,19 +134,42 @@ describe('createLoggerMiddleware', () => {
expect(stdoutCalls[0].meta.req.headers).toBeTruthy();
expect(stdoutCalls[0].meta.req.method).toBe('GET');
expect(stdoutCalls[0].meta.req.originalUrl).toBe('/');
expect(stdoutCalls[0].meta.req.body).toBe(undefined);
expect(stdoutCalls[0].meta.res).toBeTruthy();
expect(stdoutCalls[0].meta.res.statusCode).toBe(200);
expect(typeof stdoutCalls[0].meta.responseTime).toBe('number');

expect(stdoutCalls[1].level).toBe('WARN');

expect(stdoutCalls[2].level).toBe('DEBUG');
expect(stdoutCalls[2].meta.graphql).toEqual({operationName: 'createPizza'});
expect(stdoutCalls[2].meta.graphql).toEqual({
operationName: 'createPizza',
variables: {pizza: {toppings: ['salami']}}
});

const stderrCalls = process.stderr.write.mock.calls.map(call =>
JSON.parse(call[0])
);
expect(stderrCalls[0].level).toBe('ERROR');
expect(stderrCalls[0].meta.res.body).toEqual({
data: {
errors: [
{
extensions: {
method: 'GET',
responseText: 'Unauthorized',
statusCode: 401,
statusText: 'Unauthorized',
url: 'https://api.example.com/pizzas'
},
locations: [{column: 3, line: 3}],
message: '401: Unauthorized',
path: ['pizzas']
}
]
}
});
expect(stderrCalls[1].level).toBe('ERROR');

logger.destroy();
});
Expand Down
25 changes: 24 additions & 1 deletion src/createLoggerMiddleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import {Router} from 'express';
import expressWinston from 'express-winston';
import bodyParser from 'body-parser';

expressWinston.responseWhitelist.push('body');

/**
* Allows to integrate the logger with an express server.
*/
Expand Down Expand Up @@ -30,7 +32,10 @@ export default ({logger}) => {
const meta = {name: 'express'};

if (req.method === 'POST' && req.body && req.body.operationName) {
meta.graphql = {operationName: req.body.operationName};
meta.graphql = {
operationName: req.body.operationName,
variables: req.body.variables
};
}

return meta;
Expand All @@ -51,6 +56,24 @@ export default ({logger}) => {
}

return req[propName];
},

responseFilter(res, propName) {
if (propName === 'body') {
// Ignore logging the body of responses,
// except for if a GraphQL error happened.
if (
typeof res.body === 'object' &&
typeof res.body.data === 'object' &&
res.body.data.errors
) {
return res[propName];
}

return;
}

return res[propName];
}
})
);
Expand Down