@orion-js/graphql 3.4.11 → 3.4.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/errorHandler.js +10 -0
- package/lib/startGraphQL.test.js +124 -0
- package/package.json +2 -2
package/lib/errorHandler.js
CHANGED
|
@@ -5,10 +5,20 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
const crypto_1 = __importDefault(require("crypto"));
|
|
7
7
|
const helpers_1 = require("@orion-js/helpers");
|
|
8
|
+
const graphql_1 = require("graphql");
|
|
8
9
|
function errorHandler(error, data) {
|
|
9
10
|
const message = `Error in resolver "${data.name}" ${data.model ? `of model "${data.model.name}"` : ''}`;
|
|
10
11
|
if (error && error.isOrionError) {
|
|
11
12
|
console.warn(message, error);
|
|
13
|
+
throw new graphql_1.GraphQLError(error.message, {
|
|
14
|
+
originalError: error,
|
|
15
|
+
extensions: {
|
|
16
|
+
isOrionError: !!error.isOrionError,
|
|
17
|
+
isValidationError: !!error.isValidationError,
|
|
18
|
+
code: error.code,
|
|
19
|
+
info: error.getInfo()
|
|
20
|
+
}
|
|
21
|
+
});
|
|
12
22
|
}
|
|
13
23
|
else {
|
|
14
24
|
const hash = crypto_1.default
|
package/lib/startGraphQL.test.js
CHANGED
|
@@ -19,6 +19,7 @@ const supertest_1 = __importDefault(require("supertest"));
|
|
|
19
19
|
const typed_model_1 = require("@orion-js/typed-model");
|
|
20
20
|
const cleanResolvers_1 = require("./cleanResolvers");
|
|
21
21
|
const models_1 = require("@orion-js/models");
|
|
22
|
+
const helpers_1 = require("@orion-js/helpers");
|
|
22
23
|
describe('Test GraphQL Server', () => {
|
|
23
24
|
beforeEach(() => {
|
|
24
25
|
(0, cleanResolvers_1.cleanResolvers)();
|
|
@@ -108,6 +109,129 @@ describe('Test GraphQL Server', () => {
|
|
|
108
109
|
expect(response.statusCode).toBe(400);
|
|
109
110
|
expect(response.body.errors[0].message).toEqual('Cannot query field "helloWorld_doesntExists" on type "Query".');
|
|
110
111
|
});
|
|
112
|
+
it('should return validation errors correctly', async () => {
|
|
113
|
+
const resolvers = {
|
|
114
|
+
helloWorld: (0, resolvers_1.resolver)({
|
|
115
|
+
params: {
|
|
116
|
+
name: {
|
|
117
|
+
type: 'string',
|
|
118
|
+
validate: () => {
|
|
119
|
+
return 'notUnique';
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
},
|
|
123
|
+
returns: 'string',
|
|
124
|
+
async resolve({ name }) {
|
|
125
|
+
return `Hello ${name}`;
|
|
126
|
+
}
|
|
127
|
+
})
|
|
128
|
+
};
|
|
129
|
+
const app = (0, http_1.express)();
|
|
130
|
+
await (0, _1.startGraphQL)({
|
|
131
|
+
resolvers,
|
|
132
|
+
app
|
|
133
|
+
});
|
|
134
|
+
const response = await (0, supertest_1.default)(app)
|
|
135
|
+
.post('/graphql')
|
|
136
|
+
.send({
|
|
137
|
+
operationName: 'testOperation',
|
|
138
|
+
variables: {
|
|
139
|
+
name: 'Nico'
|
|
140
|
+
},
|
|
141
|
+
query: `query testOperation($name: String) {
|
|
142
|
+
helloWorld(name: $name)
|
|
143
|
+
}`
|
|
144
|
+
});
|
|
145
|
+
expect(response.statusCode).toBe(200);
|
|
146
|
+
expect(response.body).toEqual({
|
|
147
|
+
errors: [
|
|
148
|
+
{
|
|
149
|
+
message: 'Validation Error: {name: notUnique}',
|
|
150
|
+
locations: [
|
|
151
|
+
{
|
|
152
|
+
line: 2,
|
|
153
|
+
column: 11
|
|
154
|
+
}
|
|
155
|
+
],
|
|
156
|
+
path: ['helloWorld'],
|
|
157
|
+
extensions: {
|
|
158
|
+
isOrionError: true,
|
|
159
|
+
isValidationError: true,
|
|
160
|
+
code: 'validationError',
|
|
161
|
+
info: {
|
|
162
|
+
error: 'validationError',
|
|
163
|
+
message: 'Validation Error',
|
|
164
|
+
validationErrors: {
|
|
165
|
+
name: 'notUnique'
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
],
|
|
171
|
+
data: {
|
|
172
|
+
helloWorld: null
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
});
|
|
176
|
+
it('should return user errors correctly', async () => {
|
|
177
|
+
const resolvers = {
|
|
178
|
+
helloWorld: (0, resolvers_1.resolver)({
|
|
179
|
+
params: {
|
|
180
|
+
name: {
|
|
181
|
+
type: 'string'
|
|
182
|
+
}
|
|
183
|
+
},
|
|
184
|
+
returns: 'string',
|
|
185
|
+
async resolve({ name }) {
|
|
186
|
+
throw new helpers_1.UserError('code', 'message');
|
|
187
|
+
return `Hello ${name}`;
|
|
188
|
+
}
|
|
189
|
+
})
|
|
190
|
+
};
|
|
191
|
+
const app = (0, http_1.express)();
|
|
192
|
+
await (0, _1.startGraphQL)({
|
|
193
|
+
resolvers,
|
|
194
|
+
app
|
|
195
|
+
});
|
|
196
|
+
const response = await (0, supertest_1.default)(app)
|
|
197
|
+
.post('/graphql')
|
|
198
|
+
.send({
|
|
199
|
+
operationName: 'testOperation',
|
|
200
|
+
variables: {
|
|
201
|
+
name: 'Nico'
|
|
202
|
+
},
|
|
203
|
+
query: `query testOperation($name: String) {
|
|
204
|
+
helloWorld(name: $name)
|
|
205
|
+
}`
|
|
206
|
+
});
|
|
207
|
+
expect(response.statusCode).toBe(200);
|
|
208
|
+
expect(response.body).toEqual({
|
|
209
|
+
errors: [
|
|
210
|
+
{
|
|
211
|
+
message: 'message',
|
|
212
|
+
locations: [
|
|
213
|
+
{
|
|
214
|
+
line: 2,
|
|
215
|
+
column: 11
|
|
216
|
+
}
|
|
217
|
+
],
|
|
218
|
+
path: ['helloWorld'],
|
|
219
|
+
extensions: {
|
|
220
|
+
isOrionError: true,
|
|
221
|
+
isValidationError: false,
|
|
222
|
+
code: 'code',
|
|
223
|
+
info: {
|
|
224
|
+
error: 'code',
|
|
225
|
+
message: 'message'
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
],
|
|
230
|
+
data: {
|
|
231
|
+
helloWorld: null
|
|
232
|
+
}
|
|
233
|
+
});
|
|
234
|
+
});
|
|
111
235
|
it('Should make requests to schemas with typed models', async () => {
|
|
112
236
|
let Params = class Params {
|
|
113
237
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orion-js/graphql",
|
|
3
|
-
"version": "3.4.
|
|
3
|
+
"version": "3.4.12",
|
|
4
4
|
"main": "lib/index.js",
|
|
5
5
|
"author": "nicolaslopezj",
|
|
6
6
|
"license": "MIT",
|
|
@@ -51,5 +51,5 @@
|
|
|
51
51
|
"publishConfig": {
|
|
52
52
|
"access": "public"
|
|
53
53
|
},
|
|
54
|
-
"gitHead": "
|
|
54
|
+
"gitHead": "416d65a02a89193eb3e6d1bc97573ad6a949d378"
|
|
55
55
|
}
|