@gqlbase/plugins 0.0.1 → 0.0.4

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.
Files changed (68) hide show
  1. package/dist/base/ModelPlugin/ModelPlugin.d.ts +97 -0
  2. package/dist/base/ModelPlugin/ModelPlugin.d.ts.map +1 -0
  3. package/dist/base/ModelPlugin/ModelPlugin.js +405 -0
  4. package/dist/base/ModelPlugin/ModelPlugin.js.map +1 -0
  5. package/dist/base/ModelPlugin/ModelPlugin.utils.d.ts +31 -0
  6. package/dist/base/ModelPlugin/ModelPlugin.utils.d.ts.map +1 -0
  7. package/dist/base/ModelPlugin/ModelPlugin.utils.js +57 -0
  8. package/dist/base/ModelPlugin/ModelPlugin.utils.js.map +1 -0
  9. package/dist/base/ModelPlugin/index.d.ts +3 -0
  10. package/dist/base/ModelPlugin/index.d.ts.map +1 -0
  11. package/dist/base/ModelPlugin/index.js +3 -0
  12. package/dist/base/ModelPlugin/index.js.map +1 -0
  13. package/dist/base/RelationsPlugin/RelationsPlugin.d.ts +60 -0
  14. package/dist/base/RelationsPlugin/RelationsPlugin.d.ts.map +1 -0
  15. package/dist/base/RelationsPlugin/RelationsPlugin.js +188 -0
  16. package/dist/base/RelationsPlugin/RelationsPlugin.js.map +1 -0
  17. package/dist/base/RelationsPlugin/RelationsPlugin.utils.d.ts +19 -0
  18. package/dist/base/RelationsPlugin/RelationsPlugin.utils.d.ts.map +1 -0
  19. package/dist/base/RelationsPlugin/RelationsPlugin.utils.js +30 -0
  20. package/dist/base/RelationsPlugin/RelationsPlugin.utils.js.map +1 -0
  21. package/dist/base/RelationsPlugin/index.d.ts +3 -0
  22. package/dist/base/RelationsPlugin/index.d.ts.map +1 -0
  23. package/dist/base/RelationsPlugin/index.js +3 -0
  24. package/dist/base/RelationsPlugin/index.js.map +1 -0
  25. package/dist/base/SchemaGeneratorPlugin.d.ts +7 -0
  26. package/dist/base/SchemaGeneratorPlugin.d.ts.map +1 -0
  27. package/dist/base/SchemaGeneratorPlugin.js +13 -0
  28. package/dist/base/SchemaGeneratorPlugin.js.map +1 -0
  29. package/dist/base/UtilitiesPlugin/UtilitiesPlugin.d.ts +17 -0
  30. package/dist/base/UtilitiesPlugin/UtilitiesPlugin.d.ts.map +1 -0
  31. package/dist/base/UtilitiesPlugin/UtilitiesPlugin.js +66 -0
  32. package/dist/base/UtilitiesPlugin/UtilitiesPlugin.js.map +1 -0
  33. package/dist/base/UtilitiesPlugin/UtilitiesPlugin.utils.d.ts +25 -0
  34. package/dist/base/UtilitiesPlugin/UtilitiesPlugin.utils.d.ts.map +1 -0
  35. package/dist/base/UtilitiesPlugin/UtilitiesPlugin.utils.js +38 -0
  36. package/dist/base/UtilitiesPlugin/UtilitiesPlugin.utils.js.map +1 -0
  37. package/dist/base/UtilitiesPlugin/index.d.ts +3 -0
  38. package/dist/base/UtilitiesPlugin/index.d.ts.map +1 -0
  39. package/dist/base/UtilitiesPlugin/index.js +3 -0
  40. package/dist/base/UtilitiesPlugin/index.js.map +1 -0
  41. package/dist/base/basePreset.d.ts +13 -0
  42. package/dist/base/basePreset.d.ts.map +1 -0
  43. package/dist/base/basePreset.js +19 -0
  44. package/dist/base/basePreset.js.map +1 -0
  45. package/dist/base/index.d.ts +5 -1
  46. package/dist/base/index.d.ts.map +1 -1
  47. package/dist/base/index.js +6 -1
  48. package/dist/base/index.js.map +1 -0
  49. package/dist/relay/ConnectionPlugin.d.ts +46 -0
  50. package/dist/relay/ConnectionPlugin.d.ts.map +1 -0
  51. package/dist/relay/ConnectionPlugin.js +200 -0
  52. package/dist/relay/ConnectionPlugin.js.map +1 -0
  53. package/dist/relay/NodeInterfacePlugin.d.ts +20 -0
  54. package/dist/relay/NodeInterfacePlugin.d.ts.map +1 -0
  55. package/dist/relay/NodeInterfacePlugin.js +84 -0
  56. package/dist/relay/NodeInterfacePlugin.js.map +1 -0
  57. package/dist/relay/index.d.ts +3 -0
  58. package/dist/relay/index.d.ts.map +1 -0
  59. package/dist/relay/index.js +3 -0
  60. package/dist/relay/index.js.map +1 -0
  61. package/dist/relay/relayPreset.d.ts +11 -0
  62. package/dist/relay/relayPreset.d.ts.map +1 -0
  63. package/dist/relay/relayPreset.js +15 -0
  64. package/dist/relay/relayPreset.js.map +1 -0
  65. package/package.json +3 -3
  66. package/dist/base/ModelPlugin.d.ts +0 -59
  67. package/dist/base/ModelPlugin.d.ts.map +0 -1
  68. package/dist/base/ModelPlugin.js +0 -222
@@ -1,222 +0,0 @@
1
- import { DirectiveDefinitionNode, DirectiveNode, EnumNode, FieldNode, InputObjectNode, InputValueNode, ListTypeNode, NamedTypeNode, NonNullTypeNode, ObjectNode, ScalarNode, } from "@gqlbase/core/definition";
2
- import { createPluginFactory, InternalDirective } from "@gqlbase/core/plugins";
3
- import { TransformerPluginExecutionError } from "@gqlbase/shared/errors";
4
- import { camelCase, pascalCase, pluralize } from "@gqlbase/shared/format";
5
- export const ModelDirective = {
6
- MODEL: "model",
7
- };
8
- export const ModelOperation = {
9
- GET: "get",
10
- LIST: "list",
11
- SYNC: "sync",
12
- SUBSCRIBE: "subscribe",
13
- CREATE: "create",
14
- UPDATE: "update",
15
- UPSERT: "upsert",
16
- DELETE: "delete",
17
- READ: "read",
18
- WRITE: "write",
19
- };
20
- export const DEFAULT_READ_OPERATIONS = ["get", "list"];
21
- export const DEFAULT_WRITE_OPERATIONS = ["create", "update", "delete"];
22
- export const isModel = (definition) => {
23
- return definition instanceof ObjectNode && definition.hasDirective(ModelDirective.MODEL);
24
- };
25
- export class ModelPlugin {
26
- name = "ModelPlugin";
27
- context;
28
- _defaultOperations;
29
- constructor(context, options = { operations: ["read", "write"] }) {
30
- this.context = context;
31
- this._defaultOperations = this._expandOperations(options.operations);
32
- }
33
- // #region Operations
34
- _expandOperations(operations) {
35
- const expandedOperations = new Set();
36
- for (const operation of operations) {
37
- if (operation === "read") {
38
- DEFAULT_READ_OPERATIONS.forEach((op) => expandedOperations.add(op));
39
- }
40
- else if (operation === "write") {
41
- DEFAULT_WRITE_OPERATIONS.forEach((op) => expandedOperations.add(op));
42
- }
43
- else {
44
- expandedOperations.add(operation);
45
- }
46
- }
47
- return Array.from(expandedOperations);
48
- }
49
- _getOperationNames(object) {
50
- const directive = object.getDirective("model");
51
- if (!directive) {
52
- throw new TransformerPluginExecutionError(this.name, `@model directive not found for type ${object.name}`);
53
- }
54
- const args = directive.getArgumentsJSON();
55
- if (args.operations && args.operations.length > 0) {
56
- return this._expandOperations(args.operations);
57
- }
58
- return this._defaultOperations;
59
- }
60
- _createGetQuery(model) {
61
- const fieldName = camelCase("get", model.name);
62
- const queryNode = this.context.document.getQueryNode();
63
- // We allow users to implement custom definition for fields.
64
- // So, if the field already exists, we skip creating it.
65
- if (!queryNode.hasField(fieldName)) {
66
- const field = FieldNode.create(fieldName, NamedTypeNode.create(model.name), [InputValueNode.create("id", NonNullTypeNode.create("ID"))], [DirectiveNode.create("hasOne")]);
67
- queryNode.addField(field);
68
- }
69
- }
70
- _createListQuery(model) {
71
- const fieldName = camelCase("list", pluralize(model.name));
72
- const queryNode = this.context.document.getQueryNode();
73
- if (!queryNode.hasField(fieldName)) {
74
- const field = FieldNode.create(fieldName, NamedTypeNode.create(model.name), null, [
75
- DirectiveNode.create("hasMany"),
76
- ]);
77
- queryNode.addField(field);
78
- }
79
- }
80
- _shouldSkipFieldFromMutationInput(field) {
81
- return (field.hasDirective("readOnly") ||
82
- field.hasDirective("serverOnly") ||
83
- field.hasDirective("clientOnly") ||
84
- field.hasDirective("hasOne") ||
85
- field.hasDirective("hasMany"));
86
- }
87
- _isBuildInScalarType(typeName) {
88
- return ["ID", "String", "Int", "Float", "Boolean"].includes(typeName);
89
- }
90
- /**
91
- * For each field in the model
92
- * 1. If has `@readOnly` or connection directives - skip
93
- * 2. If scalar or enum - add to input;
94
- * 3. If union - skip;
95
- * 4. If object or interface - check definition;
96
- * 4.1 If `@model` - skip;
97
- * 4.2 If implements `Node` interface - skip
98
- * 4.3 If fields are `@readOnly` - skip;
99
- */
100
- _createMutationInput(model, inputName, requiredFields = []) {
101
- const input = InputObjectNode.create(inputName);
102
- if (inputName.startsWith("Delete")) {
103
- input.addField(InputValueNode.create("id", NonNullTypeNode.create(NamedTypeNode.create("ID"))));
104
- }
105
- else {
106
- for (const field of model.fields ?? []) {
107
- if (this._shouldSkipFieldFromMutationInput(field)) {
108
- continue;
109
- }
110
- const fieldTypeName = field.type.getTypeName();
111
- if (requiredFields.includes(field.name)) {
112
- input.addField(InputValueNode.create(field.name, NonNullTypeNode.create(fieldTypeName)));
113
- continue;
114
- }
115
- // Buildin scalars
116
- if (this._isBuildInScalarType(fieldTypeName)) {
117
- input.addField(InputValueNode.create(field.name, NamedTypeNode.create(fieldTypeName)));
118
- continue;
119
- }
120
- const typeDef = this.context.document.getNode(fieldTypeName);
121
- if (!typeDef) {
122
- throw new TransformerPluginExecutionError(this.name, `Unknown type ${fieldTypeName}`);
123
- }
124
- if (typeDef instanceof ScalarNode || typeDef instanceof EnumNode) {
125
- input.addField(InputValueNode.create(field.name, NamedTypeNode.create(fieldTypeName)));
126
- continue;
127
- }
128
- if (typeDef instanceof ObjectNode) {
129
- if (typeDef.hasDirective("model") ||
130
- typeDef.hasDirective("readOnly") ||
131
- typeDef.hasDirective("serverOnly") ||
132
- typeDef.hasDirective("clientOnly") ||
133
- typeDef.hasInterface("Node")) {
134
- continue;
135
- }
136
- const inputName = pascalCase(fieldTypeName, "input");
137
- if (!this.context.document.hasNode(inputName)) {
138
- this._createMutationInput(typeDef, inputName);
139
- }
140
- input.addField(InputValueNode.create(field.name, NamedTypeNode.create(inputName)));
141
- }
142
- }
143
- }
144
- this.context.document.addNode(input);
145
- }
146
- _createDeleteMutationField(model, fieldName) {
147
- const mutationNode = this.context.document.getMutationNode();
148
- if (!mutationNode.hasField(fieldName)) {
149
- const field = FieldNode.create(fieldName, NamedTypeNode.create(model.name), [
150
- InputValueNode.create("id", NonNullTypeNode.create(NamedTypeNode.create("ID"))),
151
- ]);
152
- mutationNode.addField(field);
153
- }
154
- }
155
- _createMutationField(model, fieldName, inputName, requiredFields) {
156
- const mutationNode = this.context.document.getMutationNode();
157
- if (!this.context.document.getNode(inputName)) {
158
- this._createMutationInput(model, inputName, requiredFields);
159
- }
160
- if (!mutationNode.hasField(fieldName)) {
161
- const field = FieldNode.create(fieldName, NamedTypeNode.create(model.name), [
162
- InputValueNode.create("input", NonNullTypeNode.create(NamedTypeNode.create(inputName))),
163
- ]);
164
- mutationNode.addField(field);
165
- }
166
- }
167
- _createMutation(model, verb) {
168
- const fieldName = camelCase(verb, model.name);
169
- const inputName = pascalCase(verb, model.name, "input");
170
- if (verb === "delete") {
171
- this._createDeleteMutationField(model, fieldName);
172
- }
173
- else if (verb === "update") {
174
- this._createMutationField(model, fieldName, inputName, ["id"]);
175
- }
176
- else {
177
- this._createMutationField(model, fieldName, inputName);
178
- }
179
- }
180
- // #endregion Operations
181
- init() {
182
- this.context.base
183
- .addNode(EnumNode.create("ModelOperation", Object.values(ModelOperation), [
184
- DirectiveNode.create(InternalDirective.INTERNAL),
185
- ]))
186
- .addNode(DirectiveDefinitionNode.create("model", ["OBJECT"], [
187
- InputValueNode.create("operations", ListTypeNode.create(NonNullTypeNode.create("ModelOperation"))),
188
- ]));
189
- }
190
- match(definition) {
191
- return isModel(definition);
192
- }
193
- execute(definition) {
194
- // 1. Add operation fields
195
- const operations = this._getOperationNames(definition);
196
- for (const verb of operations) {
197
- switch (verb) {
198
- case "get":
199
- this._createGetQuery(definition);
200
- break;
201
- case "list":
202
- this._createListQuery(definition);
203
- break;
204
- case "create":
205
- case "upsert":
206
- case "update":
207
- case "delete":
208
- this._createMutation(definition, verb);
209
- break;
210
- default:
211
- continue;
212
- }
213
- }
214
- }
215
- cleanup(definition) {
216
- definition.removeDirective(ModelDirective.MODEL);
217
- }
218
- after() {
219
- this.context.document.removeNode(ModelDirective.MODEL).removeNode("ModelOperation");
220
- }
221
- }
222
- export const modelPlugin = createPluginFactory(ModelPlugin);