@coderich/autograph 0.13.1 → 0.13.2

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
  "main": "index.js",
4
- "version": "0.13.1",
4
+ "version": "0.13.2",
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },
@@ -23,6 +23,7 @@
23
23
  "dataloader": "2.2.2",
24
24
  "deepmerge": "4.3.1",
25
25
  "fill-range": "7.0.1",
26
+ "graphql-parse-resolve-info": "4.13.0",
26
27
  "lodash.get": "4.4.2",
27
28
  "lodash.merge": "4.6.2",
28
29
  "lodash.uniqwith": "4.5.0",
@@ -1,5 +1,5 @@
1
1
  const Query = require('./Query');
2
- const { getGQLReturnType, mergeDeep } = require('../service/AppService');
2
+ const { getGQLReturnType, getGQLSelectFields, mergeDeep } = require('../service/AppService');
3
3
 
4
4
  module.exports = class QueryBuilder {
5
5
  #config;
@@ -41,7 +41,9 @@ module.exports = class QueryBuilder {
41
41
  * For use in GraphQL resolver methods to return the "correct" response
42
42
  */
43
43
  resolve(info) {
44
- switch (getGQLReturnType(`${info.returnType}`)) {
44
+ this.info(info);
45
+
46
+ switch (getGQLReturnType(info)) {
45
47
  case 'array': return this.many();
46
48
  case 'number': return this.count();
47
49
  case 'connection': return { count: () => this.count(), edges: () => this.many(), pageInfo: () => this.many() };
@@ -65,6 +67,11 @@ module.exports = class QueryBuilder {
65
67
  return this;
66
68
  }
67
69
 
70
+ info(info) {
71
+ this.select(getGQLSelectFields(this.#query.model, info));
72
+ return this;
73
+ }
74
+
68
75
  native(clause) {
69
76
  this.#propCheck('native', 'id', 'where');
70
77
  this.#query.isNative = true;
@@ -83,7 +90,6 @@ module.exports = class QueryBuilder {
83
90
  }
84
91
 
85
92
  select(...select) {
86
- this.#propCheck('select');
87
93
  select = select.flat();
88
94
  this.#query.select = select;
89
95
  this.#query.args.select = select;
@@ -83,7 +83,8 @@ module.exports = class Schema {
83
83
  parse() {
84
84
  if (this.#schema) return this.#schema;
85
85
 
86
- this.#schema = { models: {}, indexes: [] };
86
+ // const schema = buildASTSchema(this.#typeDefs);
87
+ this.#schema = { types: {}, models: {}, indexes: [] };
87
88
  let model, field, isField, isList;
88
89
  const thunks = [];
89
90
 
@@ -94,6 +95,8 @@ module.exports = class Schema {
94
95
  if (!allowedKinds.includes(node.kind)) return false;
95
96
 
96
97
  if (modelKinds.includes(node.kind) && !operations.includes(name)) {
98
+ // this.#schema.types[name] = schema.getType(name);
99
+
97
100
  model = this.#schema.models[name] = {
98
101
  name,
99
102
  key: name,
@@ -527,12 +530,12 @@ module.exports = class Schema {
527
530
  }, {}),
528
531
  Query: queryModels.reduce((prev, model) => {
529
532
  return Object.assign(prev, {
530
- [`get${model}`]: (doc, args, context, info) => context.autograph.resolver.match(model).args(args).one({ required: true }),
533
+ [`get${model}`]: (doc, args, context, info) => context.autograph.resolver.match(model).args(args).info(info).one({ required: true }),
531
534
  [`find${model}`]: (doc, args, context, info) => {
532
535
  return {
533
- edges: () => context.autograph.resolver.match(model).args(args).many(),
534
- count: () => context.autograph.resolver.match(model).args(args).count(),
535
- pageInfo: () => context.autograph.resolver.match(model).args(args).many(),
536
+ edges: () => context.autograph.resolver.match(model).args(args).info(info).many(),
537
+ count: () => context.autograph.resolver.match(model).args(args).info(info).count(),
538
+ pageInfo: () => context.autograph.resolver.match(model).args(args).info(info).many(),
536
539
  };
537
540
  },
538
541
  });
@@ -541,7 +544,7 @@ module.exports = class Schema {
541
544
  const { id } = args;
542
545
  const [modelName] = fromGUID(id);
543
546
  const model = schema.models[modelName];
544
- return context.autograph.resolver.match(model).id(id).one().then((result) => {
547
+ return context.autograph.resolver.match(model).id(id).info(info).one().then((result) => {
545
548
  if (result == null) return result;
546
549
  result.__typename = modelName; // eslint-disable-line no-underscore-dangle
547
550
  return result;
@@ -550,9 +553,9 @@ module.exports = class Schema {
550
553
  }),
551
554
  ...(mutationModels.length ? {
552
555
  Mutation: mutationModels.reduce((prev, model) => {
553
- if (model.crud?.includes('c')) prev[`create${model}`] = (doc, args, context, info) => context.autograph.resolver.match(model).args(args).save(args.input);
554
- if (model.crud?.includes('u')) prev[`update${model}`] = (doc, args, context, info) => context.autograph.resolver.match(model).args(args).save(args.input);
555
- if (model.crud?.includes('d')) prev[`delete${model}`] = (doc, args, context, info) => context.autograph.resolver.match(model).args(args).delete();
556
+ if (model.crud?.includes('c')) prev[`create${model}`] = (doc, args, context, info) => context.autograph.resolver.match(model).args(args).info(info).save(args.input);
557
+ if (model.crud?.includes('u')) prev[`update${model}`] = (doc, args, context, info) => context.autograph.resolver.match(model).args(args).info(info).save(args.input);
558
+ if (model.crud?.includes('d')) prev[`delete${model}`] = (doc, args, context, info) => context.autograph.resolver.match(model).args(args).info(info).delete();
556
559
  return prev;
557
560
  }, {}),
558
561
  } : {}),
@@ -561,7 +564,7 @@ module.exports = class Schema {
561
564
  [model]: Object.values(model.fields).filter(field => field.model?.isEntity).reduce((prev2, field) => {
562
565
  return Object.assign(prev2, {
563
566
  [field]: (doc, args, context, info) => {
564
- return context.autograph.resolver.match(field.model).where({ [field.linkBy]: doc[field.linkField.name] }).args(args).resolve(info);
567
+ return context.autograph.resolver.match(field.model).where({ [field.linkBy]: doc[field.linkField.name] }).args(args).info(info).resolve(info);
565
568
  },
566
569
  });
567
570
  }, {}),
@@ -1,9 +1,11 @@
1
+ const get = require('lodash.get');
1
2
  const Util = require('@coderich/util');
2
3
  const PicoMatch = require('picomatch');
3
4
  const FillRange = require('fill-range');
4
5
  const ObjectHash = require('object-hash');
5
6
  const ObjectId = require('bson-objectid');
6
7
  const DeepMerge = require('deepmerge');
8
+ const { parseResolveInfo, simplifyParsedResolveInfoFragmentWithType } = require('graphql-parse-resolve-info');
7
9
 
8
10
  exports.isGlob = str => PicoMatch.scan(str).isGlob;
9
11
  exports.globToRegex = (glob, options = {}) => PicoMatch.makeRe(glob, { nocase: true, ...options, expandRange: (a, b) => `(${FillRange(a, b, { toRegex: true })})` });
@@ -26,11 +28,19 @@ exports.finalizeWhereClause = (obj, arrayOp = '$in') => {
26
28
  }, {});
27
29
  };
28
30
 
29
- exports.getGQLReturnType = (returnType) => {
31
+ exports.getGQLReturnType = (info) => {
32
+ const returnType = `${info.returnType}`;
30
33
  const typeMap = { array: /^\[.+\].?$/, connection: /.+Connection!?$/, number: /^(Int|Float)!?$/, scalar: /.*/ };
31
34
  return Object.entries(typeMap).find(([type, pattern]) => returnType.match(pattern))[0];
32
35
  };
33
36
 
37
+ exports.getGQLSelectFields = (model, info) => {
38
+ const parsed = parseResolveInfo(info, { noLocation: true });
39
+ const { fields } = simplifyParsedResolveInfoFragmentWithType(parsed, info.returnType);
40
+ const node = get(fields, `edges.fieldsByTypeName.${model}Edge.node.fieldsByTypeName.${model}`);
41
+ return Object.keys(node || fields);
42
+ };
43
+
34
44
  exports.removeUndefinedDeep = (obj) => {
35
45
  return Util.unflatten(Object.entries(Util.flatten(obj)).reduce((prev, [key, value]) => {
36
46
  return value === undefined ? prev : Object.assign(prev, { [key]: value });