Skip to content

Commit 4a50650

Browse files
feat: Expose and test the count resolver.
1 parent 05c2ad3 commit 4a50650

File tree

5 files changed

+206
-2
lines changed

5 files changed

+206
-2
lines changed

src/generate/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const generateMutationCreate = require('./mutationCreateResolver')
22
const generateMutationUpdate = require('./mutationUpdateResolver')
33
const generateMutationDelete = require('./mutationDeleteResolver')
4+
const generateCount = require('./countResolver')
45
const generateModelTypes = require('./modelTypes')
56
const generateGraphQLType = require('./graphQLType')
67
const generateSchema = require('./schema')
@@ -17,6 +18,7 @@ module.exports = {
1718
generateMutationCreate,
1819
generateMutationUpdate,
1920
generateMutationDelete,
21+
generateCount,
2022
generateSchema,
2123
generateGraphqlExpressMiddleware,
2224
injectAssociations

src/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ const {
66
generateGraphqlExpressMiddleware,
77
generateSchema,
88
generateModelTypes,
9-
injectAssociations
9+
injectAssociations,
10+
generateCount
1011
} = require('./generate')
1112

1213
const removeUnusedAttributes = require('./removeUnusedAttributes')
@@ -21,6 +22,7 @@ module.exports = {
2122
generateGraphqlExpressMiddleware,
2223
generateSchema,
2324
generateModelTypes,
25+
generateCount,
2426
// Functions that you can use in your resolvers
2527
removeUnusedAttributes,
2628
injectAssociations
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Test the count resolvers Check that you can query a custom list resolver: Users ids and count 1`] = `
4+
Object {
5+
"user": Array [
6+
Object {
7+
"id": 5,
8+
},
9+
Object {
10+
"id": 10,
11+
},
12+
Object {
13+
"id": 15,
14+
},
15+
Object {
16+
"id": 20,
17+
},
18+
Object {
19+
"id": 25,
20+
},
21+
Object {
22+
"id": 30,
23+
},
24+
Object {
25+
"id": 35,
26+
},
27+
Object {
28+
"id": 40,
29+
},
30+
Object {
31+
"id": 45,
32+
},
33+
Object {
34+
"id": 50,
35+
},
36+
Object {
37+
"id": 55,
38+
},
39+
Object {
40+
"id": 60,
41+
},
42+
Object {
43+
"id": 65,
44+
},
45+
Object {
46+
"id": 70,
47+
},
48+
Object {
49+
"id": 75,
50+
},
51+
Object {
52+
"id": 80,
53+
},
54+
Object {
55+
"id": 85,
56+
},
57+
Object {
58+
"id": 90,
59+
},
60+
Object {
61+
"id": 95,
62+
},
63+
Object {
64+
"id": 100,
65+
},
66+
Object {
67+
"id": 105,
68+
},
69+
Object {
70+
"id": 110,
71+
},
72+
Object {
73+
"id": 115,
74+
},
75+
Object {
76+
"id": 120,
77+
},
78+
Object {
79+
"id": 125,
80+
},
81+
Object {
82+
"id": 130,
83+
},
84+
Object {
85+
"id": 135,
86+
},
87+
Object {
88+
"id": 140,
89+
},
90+
Object {
91+
"id": 145,
92+
},
93+
Object {
94+
"id": 150,
95+
},
96+
Object {
97+
"id": 155,
98+
},
99+
Object {
100+
"id": 160,
101+
},
102+
Object {
103+
"id": 165,
104+
},
105+
Object {
106+
"id": 170,
107+
},
108+
Object {
109+
"id": 175,
110+
},
111+
Object {
112+
"id": 180,
113+
},
114+
Object {
115+
"id": 185,
116+
},
117+
Object {
118+
"id": 190,
119+
},
120+
Object {
121+
"id": 195,
122+
},
123+
Object {
124+
"id": 200,
125+
},
126+
Object {
127+
"id": 205,
128+
},
129+
Object {
130+
"id": 210,
131+
},
132+
Object {
133+
"id": 215,
134+
},
135+
Object {
136+
"id": 220,
137+
},
138+
Object {
139+
"id": 225,
140+
},
141+
Object {
142+
"id": 230,
143+
},
144+
Object {
145+
"id": 235,
146+
},
147+
Object {
148+
"id": 240,
149+
},
150+
Object {
151+
"id": 245,
152+
},
153+
Object {
154+
"id": 250,
155+
},
156+
],
157+
"userCount": 50,
158+
}
159+
`;

src/tests/countResolver.spec.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const request = require('supertest')
2+
const { deleteTables } = require('./testDatabase.js')
3+
const { createServer, closeServer, resetDb } = require('./setupServer')
4+
5+
/**
6+
* Starting the tests
7+
*/
8+
describe('Test the count resolvers', () => {
9+
let server = null
10+
11+
beforeAll(async () => {
12+
server = await createServer()
13+
})
14+
15+
afterAll(() => closeServer(server))
16+
17+
beforeEach(async () => {
18+
await resetDb()
19+
})
20+
21+
afterEach(async () => {
22+
await deleteTables()
23+
})
24+
25+
it('Check that you can query a custom list resolver', async () => {
26+
const response = await request(server)
27+
.get(
28+
`/graphql?query=
29+
query getUserAndCount {
30+
user {
31+
id
32+
}
33+
userCount
34+
}`
35+
)
36+
.set('userId', 1)
37+
expect(response.body.data.user).not.toBeUndefined()
38+
expect(response.body.data).toMatchSnapshot('Users ids and count')
39+
expect(response.body.data.user.length).toBe(response.body.data.userCount)
40+
})
41+
})

src/tests/schema.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ graphqlSchemaDeclaration.companyType = {
1818

1919
graphqlSchemaDeclaration.user = {
2020
model: models.user,
21-
actions: ['list', 'create', 'delete', 'update'],
21+
actions: ['list', 'create', 'delete', 'update', 'count'],
2222
list: {
2323
before: (findOptions, args, context, info) => {
2424
if (typeof findOptions.where === 'undefined') {

0 commit comments

Comments
 (0)