|
1 | | -import { makeExecutableSchema, addMockFunctionsToSchema } from 'graphql-tools'; |
2 | | -import pluralize from 'pluralize'; |
3 | 1 | export { default as createApolloClient } from './createApolloClient'; |
4 | | -export { default as GraphQLClientServer } from './graphQLClientServer'; |
5 | | - |
6 | | -export default function() { |
7 | | - const typeDefs = {}; |
8 | | - const schema = makeExecutableSchema({ typeDefs }); |
9 | | - |
10 | | - const currentData = {}; |
11 | | - |
12 | | - const mockQueriesForEntity = entity => { |
13 | | - const entityData = currentData[pluralize(entity).toLowerCase()]; |
14 | | - |
15 | | - return { |
16 | | - [`getPageOf${pluralize(entity)}`]: ( |
17 | | - r, |
18 | | - { page, perPage, filter }, |
19 | | - ) => { |
20 | | - const filters = JSON.parse(filter); |
21 | | - let items = entityData; |
22 | | - |
23 | | - if (filters.ids) { |
24 | | - items = items.filter(d => |
25 | | - filters.ids.includes(d.id.toString()), |
26 | | - ); |
27 | | - } else { |
28 | | - Object.keys(filters) |
29 | | - .filter(key => key !== 'q') |
30 | | - .forEach(key => { |
31 | | - if (key.indexOf('_lte') !== -1) { |
32 | | - // less than or equal |
33 | | - const realKey = key.replace(/(_lte)$/, ''); |
34 | | - items = items.filter( |
35 | | - d => d[realKey] <= filters[key], |
36 | | - ); |
37 | | - return; |
38 | | - } |
39 | | - if (key.indexOf('_gte') !== -1) { |
40 | | - // less than or equal |
41 | | - const realKey = key.replace(/(_gte)$/, ''); |
42 | | - items = items.filter( |
43 | | - d => d[realKey] >= filters[key], |
44 | | - ); |
45 | | - return; |
46 | | - } |
47 | | - if (key.indexOf('_lt') !== -1) { |
48 | | - // less than or equal |
49 | | - const realKey = key.replace(/(_lt)$/, ''); |
50 | | - items = items.filter( |
51 | | - d => d[realKey] < filters[key], |
52 | | - ); |
53 | | - return; |
54 | | - } |
55 | | - if (key.indexOf('_gt') !== -1) { |
56 | | - // less than or equal |
57 | | - const realKey = key.replace(/(_gt)$/, ''); |
58 | | - items = items.filter( |
59 | | - d => d[realKey] > filters[key], |
60 | | - ); |
61 | | - return; |
62 | | - } |
63 | | - |
64 | | - items = items.filter(d => d[key] == filters[key]); |
65 | | - }); |
66 | | - |
67 | | - if (filters.q) { |
68 | | - items = items.filter(d => |
69 | | - Object.keys(d).some(key => |
70 | | - d[key].toString().includes(filters.q), |
71 | | - ), |
72 | | - ); |
73 | | - } |
74 | | - } |
75 | | - |
76 | | - if (page !== undefined && perPage) { |
77 | | - items = items.slice( |
78 | | - page * perPage, |
79 | | - page * perPage + perPage, |
80 | | - ); |
81 | | - } |
82 | | - |
83 | | - return { |
84 | | - items, |
85 | | - totalCount: entityData.length, |
86 | | - }; |
87 | | - }, |
88 | | - [`get${entity}`]: (r, { id }) => entityData.find(d => d.id == id), |
89 | | - }; |
90 | | - }; |
91 | | - |
92 | | - const mockMutationsForEntity = entity => { |
93 | | - let entityData = currentData[pluralize(entity).toLowerCase()]; |
94 | | - |
95 | | - return { |
96 | | - [`create${entity}`]: (root, { data }) => { |
97 | | - const { __typename, ...parsedData } = JSON.parse(data); |
98 | | - const newEntity = { |
99 | | - id: entityData[entityData.length - 1].id + 1, |
100 | | - ...parsedData, |
101 | | - }; |
102 | | - |
103 | | - entityData.push(newEntity); |
104 | | - return newEntity; |
105 | | - }, |
106 | | - [`update${entity}`]: (root, { data }) => { |
107 | | - const { id, __typename, ...parsedData } = JSON.parse(data); |
108 | | - const parsedId = parseInt(id, 10); |
109 | | - const indexOfEntity = entityData.findIndex( |
110 | | - e => e.id === parsedId, |
111 | | - ); |
112 | | - |
113 | | - entityData[indexOfEntity] = { id: parsedId, ...parsedData }; |
114 | | - return parsedData; |
115 | | - }, |
116 | | - [`remove${entity}`]: (root, { id }) => { |
117 | | - entityData = entityData.filter(e => e.id !== id); |
118 | | - }, |
119 | | - }; |
120 | | - }; |
121 | | - |
122 | | - const mocks = { |
123 | | - Query: () => ({ |
124 | | - ...mockQueriesForEntity('Customer'), |
125 | | - ...mockQueriesForEntity('Category'), |
126 | | - ...mockQueriesForEntity('Product'), |
127 | | - ...mockQueriesForEntity('Command'), |
128 | | - ...mockQueriesForEntity('Review'), |
129 | | - }), |
130 | | - Mutation: () => ({ |
131 | | - ...mockMutationsForEntity('Customer'), |
132 | | - ...mockMutationsForEntity('Category'), |
133 | | - ...mockMutationsForEntity('Product'), |
134 | | - ...mockMutationsForEntity('Command'), |
135 | | - ...mockMutationsForEntity('Review'), |
136 | | - }), |
137 | | - }; |
138 | | - |
139 | | - addMockFunctionsToSchema({ |
140 | | - schema, |
141 | | - mocks, |
142 | | - }); |
143 | | -} |
| 2 | +export { default as graphQLClientServer } from './graphQLClientServer'; |
| 3 | +export { default as jsonGraphqlExpress } from './jsonGraphqlExpress'; |
0 commit comments