-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: TECH make graphql requests more verbose (#105)
* feat: TECH log graphql operation name * Log query unless operation name provided * fix: TECH trigger release
- Loading branch information
1 parent
7b195e4
commit fe26284
Showing
9 changed files
with
1,095 additions
and
915 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 |
---|---|---|
@@ -1 +1 @@ | ||
16.20.0 | ||
20.11.0 |
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,7 @@ | ||
module.exports = { | ||
testEnvironment: 'node', | ||
testPathIgnorePatterns: ['/node_modules/', 'dist', '.eslintrc.js', '/support/'], | ||
runtime: '@side/jest-runtime', | ||
transform: { '^.+\\.tsx?$': '@swc/jest' }, | ||
resetMocks: true, | ||
} |
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
93 changes: 93 additions & 0 deletions
93
packages/gcloud-express-logger/src/__tests__/requestLogger.test.ts
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,93 @@ | ||
import request from 'supertest' | ||
import { IGcloudLogger } from '..' | ||
import { createApp } from './support/app' | ||
|
||
const loggerMock = jest.mocked<IGcloudLogger>({ | ||
info: jest.fn(), | ||
warn: jest.fn(), | ||
error: jest.fn(), | ||
}) | ||
|
||
describe('requestLogger', () => { | ||
const app = createApp(loggerMock) | ||
|
||
describe('with rest API', () => { | ||
it('logs request', async () => { | ||
const body = { title: 'Backend Engineer' } | ||
|
||
await request(app) | ||
.post('/api/jobs') | ||
.set('release', '[email protected]') | ||
.set('transaction-id', 'z8zpql5') | ||
.set('transaction-name', 'PublishJob') | ||
.set('user-agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 ...') | ||
.set('referer', 'https://join.com/dashboard') | ||
.send(body) | ||
.expect(201) | ||
|
||
expect(loggerMock.info).toHaveBeenCalledWith('/api/jobs', { | ||
httpRequest: expect.objectContaining({ | ||
requestMethod: 'POST', | ||
requestUrl: '/api/jobs', | ||
status: 201, | ||
release: '[email protected]', | ||
transactionId: 'z8zpql5', | ||
transactionName: 'PublishJob', | ||
userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 ...', | ||
referer: 'https://join.com/dashboard', | ||
latency: expect.stringMatching(/\d+\.\d+ms/), | ||
remoteIp: expect.any(String), | ||
}), | ||
requestTime: expect.any(Number), | ||
reqBody: body, | ||
query: {}, | ||
}) | ||
}) | ||
}) | ||
|
||
describe('with graphql API', () => { | ||
const query = ` | ||
query ListCompanies { | ||
companies { | ||
id | ||
} | ||
} | ||
` | ||
|
||
it('logs request', async () => { | ||
await request(app).post('/graphql').send({ query }).expect(200) | ||
expect(loggerMock.info).toHaveBeenCalledWith('/graphql ListCompanies', { | ||
httpRequest: expect.objectContaining({ | ||
requestMethod: 'POST', | ||
requestUrl: '/graphql', | ||
status: 200, | ||
}), | ||
requestTime: expect.any(Number), | ||
reqBody: { query }, | ||
query: {}, | ||
}) | ||
}) | ||
|
||
it('logs operation name if provided', async () => { | ||
const operationName = 'ListCompaniesOperation' | ||
|
||
await request(app).post('/graphql').send({ query, operationName }).expect(200) | ||
|
||
expect(loggerMock.info).toHaveBeenCalledWith(`/graphql ${operationName}`, expect.any(Object)) | ||
}) | ||
|
||
it('logs mutation name', async () => { | ||
const query = ` | ||
mutation RegisterCompany { | ||
companies { | ||
id | ||
} | ||
} | ||
` | ||
|
||
await request(app).post('/graphql').send({ query }).expect(200) | ||
|
||
expect(loggerMock.info).toHaveBeenCalledWith('/graphql RegisterCompany', expect.any(Object)) | ||
}) | ||
}) | ||
}) |
32 changes: 32 additions & 0 deletions
32
packages/gcloud-express-logger/src/__tests__/support/app.ts
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,32 @@ | ||
import bodyParser from 'body-parser' | ||
import express, { Application } from 'express' | ||
import { IGcloudLogger, requestLogger } from '../../' | ||
|
||
const jobsRouter = express.Router() | ||
jobsRouter.post('/jobs', (_req, res) => { | ||
res.status(201).send('OK') | ||
}) | ||
|
||
const companiesRouter = express.Router() | ||
jobsRouter.post('/companies', (_req, res) => { | ||
res.status(201).send('OK') | ||
}) | ||
|
||
const graphqlHandler = (_req: express.Request, res: express.Response) => { | ||
res.status(200).send('OK') | ||
} | ||
|
||
export const createApp = (logger: IGcloudLogger): Application => { | ||
const app = express() | ||
|
||
app.use(requestLogger(logger)) | ||
app.use(bodyParser.json()) | ||
|
||
const restRouter = express.Router() | ||
restRouter.use([jobsRouter, companiesRouter]) | ||
|
||
app.use('/api', restRouter) | ||
app.use('/graphql', graphqlHandler) | ||
|
||
return app | ||
} |
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 |
---|---|---|
@@ -1,8 +1,8 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"include": ["./src/**/*"], | ||
"include": ["./src/**/*", "./__tests__"], | ||
"compilerOptions": { | ||
"noEmit": true, | ||
"types": ["node"], | ||
"types": ["node", "jest"], | ||
} | ||
} |
Oops, something went wrong.