@coderich/autograph 0.9.12 → 0.9.13

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.
@@ -1,186 +0,0 @@
1
- const { get } = require('lodash');
2
- const DataService = require('./DataService');
3
- const { map, ensureArray, keyPaths, mapPromise, toGUID, hashObject } = require('../service/app.service');
4
-
5
- module.exports = class ResultSet {
6
- constructor(query, data, adjustForPagination = true) {
7
- const { resolver, model, sort, first, after, last, before } = query.toObject();
8
- const fields = model.getFields().filter(f => f.getName() !== 'id');
9
-
10
- const rs = map(data, (doc) => {
11
- if (doc == null || typeof doc !== 'object') return doc;
12
-
13
- const cache = new Map();
14
-
15
- const validKeys = [];
16
-
17
- const definition = {
18
- get id() { return doc.id || doc[model.idKey()]; },
19
- get $id() { return toGUID(model.getName(), this.id); },
20
- get $$data() { return data; },
21
- get $$model() { return model; },
22
- get $$isResultSetItem() { return true; },
23
- get $$save() { return input => resolver.match(model).id(this.id).save({ ...this, ...input }); },
24
- get $$remove() { return () => resolver.match(model).id(this.id).remove(); },
25
- get $$delete() { return () => resolver.match(model).id(this.id).delete(); },
26
- get $$cursor() {
27
- return () => {
28
- const sortPaths = keyPaths(sort);
29
- const sortValues = sortPaths.reduce((prv, path) => Object.assign(prv, { [path]: get(this, path) }), {});
30
- const sortJSON = JSON.stringify(sortValues);
31
- return Buffer.from(sortJSON).toString('base64');
32
- };
33
- },
34
- get toObject() {
35
- return () => validKeys.reduce((prev, key) => Object.assign(prev, { [key]: this[key] }), {});
36
- },
37
- };
38
-
39
- fields.forEach((field) => {
40
- const key = field.getKey();
41
- const name = field.getName();
42
- const $name = `$${name}`;
43
- const value = doc[key];
44
- validKeys.push(name);
45
-
46
- // Field attributes
47
- Object.assign(definition, {
48
- get [name]() {
49
- let $value = field.deserialize(query, value);
50
- $value = $value != null && field.isEmbedded() ? new ResultSet(query.model(field.getModelRef()), $value, false) : $value;
51
- return $value;
52
- },
53
- });
54
-
55
- // Hydrated field attributes
56
- Object.assign(definition, {
57
- get [$name]() {
58
- return (args = {}) => {
59
- // Ensure where clause
60
- args.where = args.where || {};
61
-
62
- return new Promise((resolve, reject) => {
63
- (() => {
64
- const $value = this[name];
65
-
66
- if (field.isScalar() || field.isEmbedded()) return Promise.resolve($value);
67
-
68
- const modelRef = field.getModelRef();
69
-
70
- if (field.isArray()) {
71
- if (field.isVirtual()) {
72
- args.where[[field.getVirtualField()]] = this.id; // Is where[[field.getVirtualField()]] correct?
73
- return resolver.match(modelRef).merge(args).many();
74
- }
75
-
76
- // Not a "required" query + strip out nulls
77
- args.where.id = $value;
78
- return resolver.match(modelRef).merge(args).many();
79
- }
80
-
81
- if (field.isVirtual()) {
82
- args.where[[field.getVirtualField()]] = this.id;
83
- return resolver.match(modelRef).merge(args).one();
84
- }
85
-
86
- return resolver.match(modelRef).id($value).one({ required: field.isRequired() });
87
- })().then((results) => {
88
- if (results == null) return field.resolve(query, results); // Allow field to determine
89
- return mapPromise(results, result => field.resolve(query, result)).then(() => results); // Resolve the inside fields but still return "results"!!!!
90
- }).then((resolved) => {
91
- resolve(resolved);
92
- }).catch((e) => {
93
- reject(e);
94
- });
95
- });
96
- };
97
- },
98
- });
99
-
100
- // Field count (let's assume it's a Connection Type - meaning dont try with anything else)
101
- Object.assign(definition, {
102
- get [`${$name}:count`]() {
103
- return (q = {}) => {
104
- q.where = q.where || {};
105
- if (field.isVirtual()) q.where[field.getVirtualField()] = this.id;
106
- else q.where.id = this[name];
107
- return resolver.match(field.getModelRef()).merge(q).count();
108
- };
109
- },
110
- });
111
- });
112
-
113
- // Create and return ResultSetItem
114
- const idk = new Proxy(definition, {
115
- get(target, prop, rec) {
116
- if (cache.has(prop)) return cache.get(prop);
117
- const value = Reflect.get(target, prop, rec);
118
- if (typeof value === 'function') return value.bind(target);
119
- cache.set(prop, value);
120
- return value;
121
- },
122
- set(target, prop, value) {
123
- cache.set(prop, value);
124
- return true;
125
- },
126
- ownKeys() {
127
- return validKeys;
128
- },
129
- getOwnPropertyDescriptor(target, prop) {
130
- if (validKeys.indexOf(prop) === -1) {
131
- return {
132
- writable: true,
133
- enumerable: true,
134
- configurable: true,
135
- };
136
- }
137
-
138
- return {
139
- writable: false,
140
- enumerable: false,
141
- configurable: false,
142
- };
143
- },
144
- });
145
-
146
- // console.log(idk);
147
- // // console.log(idk.toObject());
148
- return idk;
149
- });
150
-
151
- let hasNextPage = false;
152
- let hasPreviousPage = false;
153
- if (adjustForPagination && rs.length) (({ hasPreviousPage, hasNextPage } = DataService.paginateResultSet(rs, first, after, last, before)));
154
-
155
- return Object.defineProperties(rs, {
156
- $$pageInfo: {
157
- get() {
158
- const edges = ensureArray(rs);
159
-
160
- return {
161
- startCursor: get(edges, '0.$$cursor', ''),
162
- endCursor: get(edges, `${edges.length - 1}.$$cursor`, ''),
163
- hasPreviousPage,
164
- hasNextPage,
165
- };
166
- },
167
- enumerable: false,
168
- },
169
- $$isResultSet: {
170
- value: true,
171
- enumerable: false,
172
- },
173
- toObject: {
174
- get() {
175
- return () => map(this, doc => Object.entries(doc).reduce((prev, [key, value]) => {
176
- if (value === undefined) return prev;
177
- prev[key] = get(value, '$$isResultSet') ? value.toObject() : value;
178
- return prev;
179
- }, {}));
180
- },
181
- enumerable: false,
182
- configurable: true,
183
- },
184
- });
185
- }
186
- };
Binary file
@@ -1,20 +0,0 @@
1
- const { Kind, visit } = require('graphql');
2
- const Node = require('./Node');
3
-
4
- module.exports = class Field extends Node {
5
- constructor(ast) {
6
- super(ast);
7
- // this.fields = {};
8
- // if (ast) this.appendAST(ast);
9
- }
10
-
11
- appendAST(ast) {
12
- // visit(ast, {
13
- // [Kind.FIELD_DEFINITION]: (node) => {
14
- // const name = node.name.value;
15
- // if (this.fields[name]) this.fields[name].appendAST(node);
16
- // else this.fields[name] = new Field(node);
17
- // },
18
- // });
19
- }
20
- };
@@ -1,21 +0,0 @@
1
- const { graphql, execute, validate } = require('graphql');
2
- const Schema = require('./Schema');
3
-
4
- module.exports = class GraphQL {
5
- constructor(schema) {
6
- this.schema = (schema instanceof Schema ? schema : new Schema(schema)).makeExecutableSchema();
7
- }
8
-
9
- exec(source, variableValues) {
10
- const { schema } = this.schema;
11
- return graphql({ schema, source, variableValues, contextValue: schema.context });
12
- }
13
-
14
- execute(source, variableValues) {
15
-
16
- }
17
-
18
- validate(source, variableValues) {
19
-
20
- }
21
- };
@@ -1,23 +0,0 @@
1
- const { Kind, visit } = require('graphql');
2
- const Field = require('./Field');
3
- const Node = require('./Node');
4
-
5
- module.exports = class Model extends Node {
6
- constructor(ast) {
7
- super(ast);
8
- this.fields = {};
9
- if (ast) this.appendAST(ast);
10
- }
11
-
12
- appendAST(ast) {
13
- visit(ast, {
14
- [Kind.FIELD_DEFINITION]: (node) => {
15
- const name = node.name.value;
16
- if (this.fields[name]) this.fields[name].appendAST(node);
17
- else this.fields[name] = new Field(node);
18
- },
19
- });
20
-
21
- return this;
22
- }
23
- };
@@ -1,34 +0,0 @@
1
- const { Kind } = require('graphql');
2
-
3
- module.exports = class Node {
4
- constructor(ast) {
5
- this.ast = ast;
6
- }
7
-
8
- getKind() {
9
- return this.ast.kind;
10
- }
11
-
12
- getName() {
13
- return this.ast.name.value;
14
- }
15
-
16
- getValue(ast = this.ast) {
17
- const { value = {} } = ast;
18
-
19
- switch (value.kind) {
20
- case Kind.NULL: return null;
21
- case Kind.LIST: return value.values.map(el => this.getValue({ value: el }));
22
- case Kind.OBJECT: {
23
- return value.fields.reduce((prev, field) => {
24
- const node = new Node(field);
25
- return Object.assign(prev, { [node.getName()]: node.getValue() });
26
- }, {});
27
- }
28
- default: {
29
- if (ast.values) return ast.values.map(v => v.name.value);
30
- return value.value;
31
- }
32
- }
33
- }
34
- };
@@ -1,63 +0,0 @@
1
- const FS = require('fs');
2
- const Glob = require('glob');
3
- const Merge = require('deepmerge');
4
- const { Kind, parse, visit, printSchema, buildSchema } = require('graphql');
5
- const { makeExecutableSchema } = require('graphql-tools');
6
- const Model = require('./Model');
7
-
8
- const modelKinds = [Kind.OBJECT_TYPE_DEFINITION, Kind.OBJECT_TYPE_EXTENSION, Kind.INTERFACE_TYPE_DEFINITION, Kind.INTERFACE_TYPE_EXTENSION];
9
-
10
- module.exports = class Schema {
11
- constructor(schema) {
12
- this.models = {};
13
- this.schema = { typeDefs: [], context: {}, resolvers: {}, schemaDirectives: {} };
14
- if (schema) this.appendSchema(schema);
15
- }
16
-
17
- appendSchema(schema) {
18
- // Normalize schema
19
- if (typeof schema === 'string') schema = { typeDefs: [schema] };
20
- else if (schema.typeDefs && !Array.isArray(schema.typeDefs)) schema.typeDefs = [schema.typeDefs];
21
-
22
- // Merge schema
23
- this.schema = Merge(this.schema, schema);
24
-
25
- // Visit AST to maintain model definitions
26
- if (schema.typeDefs) {
27
- visit(parse(schema.typeDefs.join('\n')), {
28
- enter: (node) => {
29
- if (modelKinds.indexOf(node.kind) > -1) {
30
- const name = node.name.value;
31
- if (this.models[name]) this.models[name].appendAST(node);
32
- else this.models[name] = new Model(node);
33
- }
34
- },
35
- });
36
- }
37
-
38
- return this;
39
- }
40
-
41
- appendSchemaFromFile(file) {
42
- if (file.endsWith('.js')) this.appendSchema(require(file)); // eslint-disable-line global-require,import/no-dynamic-require
43
- else this.appendSchema(FS.readFileSync(file, 'utf8'));
44
- return this;
45
- }
46
-
47
- appendSchemaFromDirectory(dir, options) {
48
- Glob.sync(`${dir}/**/*.{js,gql,graphql}`, options).forEach(file => this.appendSchemaFromFile(file));
49
- return this;
50
- }
51
-
52
- makeExecutableSchema() {
53
- return makeExecutableSchema(this.schema);
54
- }
55
-
56
- printSchema() {
57
- return printSchema(buildSchema(this.toString()));
58
- }
59
-
60
- toString() {
61
- return this.schema.typeDefs.join('\n');
62
- }
63
- };