@coderich/autograph 0.10.19 → 0.10.21

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@coderich/autograph",
3
3
  "author": "Richard Livolsi (coderich)",
4
- "version": "0.10.19",
4
+ "version": "0.10.21",
5
5
  "description": "AutoGraph",
6
6
  "keywords": [
7
7
  "graphql",
@@ -76,12 +76,13 @@ module.exports = class Pipeline {
76
76
  Pipeline.define('idField', ({ model, field, value }) => field.getIdModel().idValue(value.id || value));
77
77
  Pipeline.define('ensureArrayValue', ({ field, value }) => (field.toObject().isArray && !Array.isArray(value) ? [value] : value), { itemize: false });
78
78
 
79
- Pipeline.define('ensureId', ({ resolver, field, value }) => {
79
+ Pipeline.define('ensureId', ({ resolver, model, field, value }) => {
80
+ const path = `${model}.${field}`;
80
81
  const { type } = field.toObject();
81
82
  const ids = Array.from(new Set(ensureArray(value).map(v => `${v}`)));
82
83
 
83
84
  return resolver.match(type).where({ id: ids }).count().then((count) => {
84
- if (count !== ids.length) throw Boom.notFound(`${type} Not Found`);
85
+ if (count !== ids.length) throw Boom.notFound(`${type} Not Found`, { path });
85
86
  });
86
87
  }, { itemize: false });
87
88
 
@@ -124,29 +125,34 @@ module.exports = class Pipeline {
124
125
 
125
126
  // Required fields
126
127
  Pipeline.define('required', ({ model, field, value }) => {
127
- if (value == null) throw Boom.badRequest(`${model}.${field} is required`);
128
+ const path = `${model}.${field}`;
129
+ if (value == null) throw Boom.badRequest(`${path} is required`, { path });
128
130
  }, { ignoreNull: false });
129
131
 
130
132
  // A field cannot hold a reference to itself
131
133
  Pipeline.define('selfless', ({ model, field, parent, parentPath, value }) => {
132
- if (`${value}` === `${parentPath('id')}`) throw Boom.badRequest(`${model}.${field} cannot hold a reference to itself`);
134
+ const path = `${model}.${field}`;
135
+ if (`${value}` === `${parentPath('id')}`) throw Boom.badData(`${path} cannot hold a reference to itself`, { path });
133
136
  });
134
137
 
135
138
  // Once set it cannot be changed
136
139
  Pipeline.define('immutable', ({ model, field, docPath, parentPath, path, value }) => {
140
+ const $path = `${model}.${field}`;
137
141
  const hint = { id: parentPath('id') };
138
142
  const oldVal = docPath(path, hint);
139
- if (oldVal !== undefined && value !== undefined && `${hashObject(oldVal)}` !== `${hashObject(value)}`) throw Boom.badRequest(`${model}.${field} is immutable; cannot be changed once set ${oldVal} -> ${value}`);
143
+ if (oldVal !== undefined && value !== undefined && `${hashObject(oldVal)}` !== `${hashObject(value)}`) throw Boom.badData(`${$path} is immutable; cannot be changed once set ${oldVal} -> ${value}`, { path: $path });
140
144
  });
141
145
 
142
146
  // List of allowed values
143
147
  Pipeline.factory('Allow', (...args) => function allow({ model, field, value }) {
144
- if (args.indexOf(value) === -1) throw Boom.badRequest(`${model}.${field} allows ${args}; found '${value}'`);
148
+ const path = `${model}.${field}`;
149
+ if (args.indexOf(value) === -1) throw Boom.badData(`${path} allows ${args}; found '${value}'`, { path });
145
150
  });
146
151
 
147
152
  // List of disallowed values
148
153
  Pipeline.factory('Deny', (...args) => function deny({ model, field, value }) {
149
- if (args.indexOf(value) > -1) throw Boom.badRequest(`${model}.${field} denys ${args}; found '${value}'`);
154
+ const path = `${model}.${field}`;
155
+ if (args.indexOf(value) > -1) throw Boom.badData(`${path} denys ${args}; found '${value}'`, { path });
150
156
  });
151
157
 
152
158
  // Min/Max range
@@ -155,9 +161,10 @@ module.exports = class Pipeline {
155
161
  if (max == null) max = undefined;
156
162
 
157
163
  return function range({ model, field, value }) {
164
+ const path = `${model}.${field}`;
158
165
  const num = +value; // Coerce to number if possible
159
166
  const test = Number.isNaN(num) ? value.length : num;
160
- if (test < min || test > max) throw Boom.badRequest(`${model}.${field} must satisfy range ${min}:${max}; found '${value}'`);
167
+ if (test < min || test > max) throw Boom.badData(`${path} must satisfy range ${min}:${max}; found '${value}'`, { path });
161
168
  };
162
169
  }, { itemize: false });
163
170
  }
@@ -1,5 +1,3 @@
1
- const Boom = require('../core/Boom');
2
-
3
1
  module.exports = class Query {
4
2
  constructor(props = {}) {
5
3
  this.props = {};
@@ -21,7 +19,7 @@ module.exports = class Query {
21
19
 
22
20
  propCheck(prop, ...checks) {
23
21
  checks.forEach((check) => {
24
- if (this.props[check]) throw Boom.badRequest(`Cannot use "${prop}" while using "${check}"`);
22
+ if (this.props[check]) throw new Error(`Cannot use "${prop}" while using "${check}"`);
25
23
  });
26
24
  }
27
25
 
@@ -98,7 +96,7 @@ module.exports = class Query {
98
96
 
99
97
  skip(skip) {
100
98
  this.propCheck('skip', 'id');
101
- if (this.isCursorPaging) throw Boom.badRequest('Cannot use "skip" while using Cursor-Style Pagination');
99
+ if (this.isCursorPaging) throw new Error('Cannot use "skip" while using Cursor-Style Pagination');
102
100
  this.isClassicPaging = true;
103
101
  this.props.skip = skip;
104
102
  return this;
@@ -106,7 +104,7 @@ module.exports = class Query {
106
104
 
107
105
  limit(limit) {
108
106
  this.propCheck('limit', 'id');
109
- if (this.isCursorPaging) throw Boom.badRequest('Cannot use "limit" while using Cursor-Style Pagination');
107
+ if (this.isCursorPaging) throw new Error('Cannot use "limit" while using Cursor-Style Pagination');
110
108
  this.isClassicPaging = true;
111
109
  this.props.limit = limit;
112
110
  return this;
@@ -114,7 +112,7 @@ module.exports = class Query {
114
112
 
115
113
  first(first) {
116
114
  this.propCheck('first', 'id', 'last');
117
- if (this.isClassicPaging) throw Boom.badRequest('Cannot use "first" while using Classic-Style Pagination');
115
+ if (this.isClassicPaging) throw new Error('Cannot use "first" while using Classic-Style Pagination');
118
116
  this.isCursorPaging = true;
119
117
  this.props.first = first + 2; // Adding 2 for pagination meta info (hasNext hasPrev)
120
118
  return this;
@@ -122,7 +120,7 @@ module.exports = class Query {
122
120
 
123
121
  last(last) {
124
122
  this.propCheck('last', 'id', 'first');
125
- if (this.isClassicPaging) throw Boom.badRequest('Cannot use "last" while using Classic-Style Pagination');
123
+ if (this.isClassicPaging) throw new Error('Cannot use "last" while using Classic-Style Pagination');
126
124
  this.isCursorPaging = true;
127
125
  this.props.last = last + 2; // Adding 2 for pagination meta info (hasNext hasPrev)
128
126
  return this;
@@ -130,7 +128,7 @@ module.exports = class Query {
130
128
 
131
129
  before(before) {
132
130
  this.propCheck('before', 'id');
133
- if (this.isClassicPaging) throw Boom.badRequest('Cannot use "before" while using Classic-Style Pagination');
131
+ if (this.isClassicPaging) throw new Error('Cannot use "before" while using Classic-Style Pagination');
134
132
  this.isCursorPaging = true;
135
133
  this.props.before = before;
136
134
  return this;
@@ -138,7 +136,7 @@ module.exports = class Query {
138
136
 
139
137
  after(after) {
140
138
  this.propCheck('after', 'id');
141
- if (this.isClassicPaging) throw Boom.badRequest('Cannot use "after" while using Classic-Style Pagination');
139
+ if (this.isClassicPaging) throw new Error('Cannot use "after" while using Classic-Style Pagination');
142
140
  this.isCursorPaging = true;
143
141
  this.props.after = after;
144
142
  return this;
@@ -186,7 +186,8 @@ module.exports = class QueryResolver {
186
186
  // Can only splice arrays
187
187
  const field = model.getField(key);
188
188
  const isArray = field.isArray();
189
- if (!isArray) throw Boom.badRequest(`Cannot splice field '${model}.${field}'`);
189
+ const path = `${model}.${field}`;
190
+ if (!isArray) throw Boom.badRequest(`Cannot splice field '${path}'`, { path });
190
191
 
191
192
  return this.resolver.match(model).match(match).one({ required: true }).then(async (doc) => {
192
193
  const array = get(doc, key) || [];