@orion-js/graphql 3.4.10 → 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.
@@ -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
@@ -28,7 +28,10 @@ exports.default = (0, models_1.createModel)({
28
28
  result: (0, resolvers_1.resolver)({
29
29
  returns: String,
30
30
  resolve: async function ({ resolver }) {
31
- return resolver.returns.name;
31
+ const returns = (0, lodash_1.isArray)(resolver.returns) ? resolver.returns[0] : resolver.returns;
32
+ if (resolverReturnsIsModel(returns))
33
+ return returns.name;
34
+ return;
32
35
  }
33
36
  }),
34
37
  basicResultQuery: (0, resolvers_1.resolver)({
@@ -34,6 +34,32 @@ describe('Get params tests', () => {
34
34
  expect(result).toEqual(undefined);
35
35
  expect(basicResultQuery).toEqual('');
36
36
  });
37
+ it('Should return the correct information for a resolver that returns a string', async () => {
38
+ resolversStore_1.resolversStore.aaResolver = (0, resolvers_1.resolver)({
39
+ params: {
40
+ aParam: {
41
+ type: String
42
+ }
43
+ },
44
+ returns: [String],
45
+ async resolve() {
46
+ return 'a';
47
+ }
48
+ });
49
+ const response = (await params_1.default.execute({
50
+ params: {
51
+ name: 'aaResolver',
52
+ mutation: false
53
+ }
54
+ }));
55
+ const params = await response.params();
56
+ const result = await response.result();
57
+ const basicResultQuery = await response.basicResultQuery();
58
+ expect(response.name).toEqual('aaResolver');
59
+ expect(params).toEqual({ aParam: { type: 'string', __graphQLType: 'String' } });
60
+ expect(result).toEqual(undefined);
61
+ expect(basicResultQuery).toEqual('');
62
+ });
37
63
  it('Should return the correct information for a resolver that returns a array of a model', async () => {
38
64
  const model = (0, models_1.createModel)({
39
65
  name: 'Item',
@@ -65,7 +91,7 @@ describe('Get params tests', () => {
65
91
  const basicResultQuery = await response.basicResultQuery();
66
92
  expect(response.name).toEqual('bResolver');
67
93
  expect(params).toEqual({ aParam: { type: 'string', __graphQLType: 'String' } });
68
- expect(result).toEqual(undefined);
94
+ expect(result).toEqual('Item');
69
95
  expect(basicResultQuery).toEqual('{ name }');
70
96
  });
71
97
  it('Should return the correct information for a resolver that returns a model', async () => {
@@ -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.10",
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": "cb0152649af2360759c9aa65a1cd0435e82ef259"
54
+ "gitHead": "416d65a02a89193eb3e6d1bc97573ad6a949d378"
55
55
  }