Skip to content

Commit 9ec0a3e

Browse files
authored
Refactor Server Factory Function (canada-ca#1810)
* update server factory function to take obj instead of regular args * update entrypoint to match changes
1 parent 1101c02 commit 9ec0a3e

3 files changed

Lines changed: 44 additions & 26 deletions

File tree

api-js/index.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,17 @@ const {
2424
options: databaseOptions({ rootPass }),
2525
})
2626

27-
Server(PORT, maxDepth, complexityCost, scalarCost, objectCost, listFactor, {
28-
query,
29-
collections,
30-
transaction,
27+
Server({
28+
maxDepth,
29+
complexityCost,
30+
scalarCost,
31+
objectCost,
32+
listFactor,
33+
context: {
34+
query,
35+
collections,
36+
transaction,
37+
},
3138
}).listen(PORT, (err) => {
3239
if (err) throw err
3340
console.log(`🚀 Server ready at http://localhost:${PORT}/graphql`)

api-js/src/__tests__/server.test.js

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import request from 'supertest'
22
import { Server } from '../server'
33

44
const {
5-
PORT = 4000,
65
DEPTH_LIMIT: maxDepth,
76
COST_LIMIT: complexityCost,
87
SCALAR_COST: scalarCost,
@@ -41,19 +40,18 @@ describe('parse server', () => {
4140
describe('endpoint is alive', () => {
4241
it('returns 200', async () => {
4342
const response = await request(
44-
Server(
45-
PORT,
43+
Server({
4644
maxDepth,
4745
complexityCost,
4846
scalarCost,
4947
objectCost,
5048
listFactor,
51-
{
49+
context: {
5250
query: jest.fn(),
5351
collections: jest.fn(),
5452
transaction: jest.fn(),
5553
},
56-
),
54+
}),
5755
)
5856
.post('/graphql')
5957
.set('Accept', 'application/json')
@@ -66,10 +64,17 @@ describe('parse server', () => {
6664
describe('query cost is too high', () => {
6765
it('returns an error message', async () => {
6866
const response = await request(
69-
Server(PORT, maxDepth, 1, 100, 100, 100, {
70-
query: jest.fn(),
71-
collections: jest.fn(),
72-
transaction: jest.fn(),
67+
Server({
68+
maxDepth,
69+
complexityCost: 1,
70+
scalarCost: 100,
71+
objectCost: 100,
72+
listFactor: 100,
73+
context: {
74+
query: jest.fn(),
75+
collections: jest.fn(),
76+
transaction: jest.fn(),
77+
},
7378
}),
7479
)
7580
.post('/graphql')
@@ -85,10 +90,17 @@ describe('parse server', () => {
8590
describe('query depth is too high', () => {
8691
it('returns an error message', async () => {
8792
const response = await request(
88-
Server(PORT, 1, 1000, 1, 1, 1, {
89-
query: jest.fn(),
90-
collections: jest.fn(),
91-
transaction: jest.fn(),
93+
Server({
94+
maxDepth: 1,
95+
complexityCost: 1000,
96+
scalarCost: 1,
97+
objectCost: 1,
98+
listFactor: 1,
99+
context: {
100+
query: jest.fn(),
101+
collections: jest.fn(),
102+
transaction: jest.fn(),
103+
},
92104
}),
93105
)
94106
.post('/graphql')

api-js/src/server.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,14 @@ const createValidationRules = (
3939
}
4040

4141
export const Server = (
42-
// TODO refactor
43-
// no longer used but preserved because of coupling to argument order
44-
_PORT = '4000',
45-
maxDepth,
46-
complexityCost,
47-
scalarCost,
48-
objectCost,
49-
listFactor,
50-
context = {},
42+
{
43+
maxDepth,
44+
complexityCost,
45+
scalarCost,
46+
objectCost,
47+
listFactor,
48+
context = {},
49+
},
5150
) => {
5251
const app = express()
5352

0 commit comments

Comments
 (0)