Skip to content
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
8 changes: 4 additions & 4 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 16.9
node-version: 20
- run: npm i
- run: npm run lint
- run: npm test
Expand All @@ -27,9 +27,9 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 16.9
node-version: 20
- run: npm i
- run: npm run coverage
- run: npm run test:coverage
- uses: codecov/codecov-action@v1

publish-npm:
Expand All @@ -42,7 +42,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 16.9
node-version: 20
registry-url: https://registry.npmjs.org/
scope: '@hydre'
- run: npm ci
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"./tinyhttp": "./src/tinyhttp.js"
},
"scripts": {
"test": "node --test test/**/*.test.js",
"test": "node --test test/*.test.js",
"test:coverage": "c8 --reporter=text --reporter=html --reporter=lcov npm test",
"typecheck": "tsc --noEmit",
"lint": "eslint . && prettier . --check",
Expand Down
12 changes: 10 additions & 2 deletions src/fastify.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,16 @@ const Fastify = ({ body }, reply) => {
} catch (error) {
const graphql_error =
error instanceof GraphQLError ? error : new GraphQLError(error.message)
reply.status(200).type('application/json').send({ errors: [graphql_error] })
return { query: null, variable_values: null, operation_name: null, reply: () => {} }
reply
.status(200)
.type('application/json')
.send({ errors: [graphql_error] })
return {
query: null,
variable_values: null,
operation_name: null,
reply: () => {},
}
}

const { query, variables, operationName, operation_name } = body
Expand Down
7 changes: 6 additions & 1 deletion src/koa.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ const Koa = context => {
context.status = 200
context.type = 'application/json'
context.body = { errors: [graphql_error] }
return { query: null, variable_values: null, operation_name: null, reply: () => {} }
return {
query: null,
variable_values: null,
operation_name: null,
reply: () => {},
}
}

const { query, variables, operationName, operation_name } = request_body
Expand Down
6 changes: 4 additions & 2 deletions src/lambda.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const Lambda = ({ body: raw_body }, context, reply) => {
let parsed
try {
parsed = JSON.parse(raw_body)
} catch (error) {
} catch {
reply(null, {
statusCode: 400,
body: JSON.stringify({
Expand All @@ -24,7 +24,9 @@ const Lambda = ({ body: raw_body }, context, reply) => {
reply(null, {
statusCode: 200,
body: JSON.stringify({
errors: [error instanceof GraphQLError ? error : { message: error.message }],
errors: [
error instanceof GraphQLError ? error : { message: error.message },
],
}),
})
return { query: null, variable_values: null, operation_name: null, reply }
Expand Down
7 changes: 6 additions & 1 deletion src/tinyhttp.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ const TinyHttp = ({ body = {} }, response) => {
const graphql_error =
error instanceof GraphQLError ? error : new GraphQLError(error.message)
response.status(200).json({ errors: [graphql_error] })
return { query: null, variable_values: null, operation_name: null, reply: () => {} }
return {
query: null,
variable_values: null,
operation_name: null,
reply: () => {},
}
}

const { query, variables, operationName, operation_name } = body
Expand Down
4 changes: 2 additions & 2 deletions test/fastify.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import graphql_http from '../src/fastify.js'
async function create_server(options) {
const fastify = Fastify()
fastify.post('/', graphql_http(options))
await fastify.listen({ port: 3001 })
await fastify.listen({ port: 3002 })
return fastify
}

async function request({ query, variables = {}, operation_name = null } = {}) {
const response = await fetch('http://localhost:3001', {
const response = await fetch('http://localhost:3002', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Expand Down
15 changes: 9 additions & 6 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,15 @@ test('koa adapter', async t => {
}
})

await t.test('should return error for query exceeding size limit', async () => {
const huge_query = `{ ${'me { name } '.repeat(20000)} }`
const { errors } = await request({ query: huge_query })
assert.ok(errors)
assert.ok(errors[0].message.toLowerCase().includes('too large'))
})
await t.test(
'should return error for query exceeding size limit',
async () => {
const huge_query = `{ ${'me { name } '.repeat(20000)} }`
const { errors } = await request({ query: huge_query })
assert.ok(errors)
assert.ok(errors[0].message.toLowerCase().includes('too large'))
},
)

await t.test('should timeout slow build_context', async () => {
const Koa = (await import('koa')).default
Expand Down
27 changes: 15 additions & 12 deletions test/lambda.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,18 +146,21 @@ test('lambda adapter', async t => {
assert.ok(body.errors[0].message.toLowerCase().includes('json'))
})

await t.test('should return error for query exceeding size limit', async () => {
const huge_query = `{ ${'me { name } '.repeat(20000)} }`
const event = create_lambda_event({
query: huge_query,
})
const result = await invoke_lambda(event)

assert.strictEqual(result.statusCode, 200)
const body = JSON.parse(result.body)
assert.ok(body.errors)
assert.ok(body.errors[0].message.toLowerCase().includes('too large'))
})
await t.test(
'should return error for query exceeding size limit',
async () => {
const huge_query = `{ ${'me { name } '.repeat(20000)} }`
const event = create_lambda_event({
query: huge_query,
})
const result = await invoke_lambda(event)

assert.strictEqual(result.statusCode, 200)
const body = JSON.parse(result.body)
assert.ok(body.errors)
assert.ok(body.errors[0].message.toLowerCase().includes('too large'))
},
)

await t.test('should return error for non-object request body', async () => {
const event = {
Expand Down