@gqlbase/plugins 0.0.1 → 0.0.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.
@@ -0,0 +1,19 @@
1
+ import { type ITransformerPlugin } from "@gqlbase/core/plugins";
2
+ import type { ITransformerContext } from "@gqlbase/core/context";
3
+ import { DefinitionNode, ObjectNode } from "@gqlbase/core/definition";
4
+ /**
5
+ * Adds a `Node` interface with an `id: ID!` field to the schema and ensures that all types that implement the `Node` interface also have the `id: ID!` field.
6
+ *
7
+ */
8
+ export declare class NodeInterfacePlugin implements ITransformerPlugin {
9
+ readonly name = "NodeInterfacePlugin";
10
+ readonly context: ITransformerContext;
11
+ constructor(context: ITransformerContext);
12
+ init(): void;
13
+ match(definition: DefinitionNode): boolean;
14
+ before(): void;
15
+ execute(definition: ObjectNode): void;
16
+ after(): void;
17
+ }
18
+ export declare const nodeInterfacePlugin: (options?: unknown) => import("@gqlbase/core/plugins").IPluginFactory;
19
+ //# sourceMappingURL=NodeInterfacePlugin.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"NodeInterfacePlugin.d.ts","sourceRoot":"","sources":["../../src/base/NodeInterfacePlugin.ts"],"names":[],"mappings":"AAAA,OAAO,EAAuB,KAAK,kBAAkB,EAAE,MAAM,uBAAuB,CAAC;AACrF,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AACjE,OAAO,EACL,cAAc,EAOd,UAAU,EACX,MAAM,0BAA0B,CAAC;AAGlC;;;GAGG;AAEH,qBAAa,mBAAoB,YAAW,kBAAkB;IAC5D,SAAgB,IAAI,yBAAyB;IAC7C,QAAQ,CAAC,OAAO,EAAE,mBAAmB,CAAC;gBAE1B,OAAO,EAAE,mBAAmB;IAIjC,IAAI,IAAI,IAAI;IAQnB,KAAK,CAAC,UAAU,EAAE,cAAc,GAAG,OAAO;IAU1C,MAAM,IAAI,IAAI;IAed,OAAO,CAAC,UAAU,EAAE,UAAU,GAAG,IAAI;IAmC9B,KAAK,IAAI,IAAI;CAiBrB;AAED,eAAO,MAAM,mBAAmB,uEAA2C,CAAC"}
@@ -0,0 +1,70 @@
1
+ import { createPluginFactory } from "@gqlbase/core/plugins";
2
+ import { DirectiveNode, FieldNode, InputValueNode, InterfaceNode, NamedTypeNode, NonNullTypeNode, ObjectNode, } from "@gqlbase/core/definition";
3
+ import { TransformerPluginExecutionError } from "@gqlbase/shared/errors";
4
+ /**
5
+ * Adds a `Node` interface with an `id: ID!` field to the schema and ensures that all types that implement the `Node` interface also have the `id: ID!` field.
6
+ *
7
+ */
8
+ export class NodeInterfacePlugin {
9
+ name = "NodeInterfacePlugin";
10
+ context;
11
+ constructor(context) {
12
+ this.context = context;
13
+ }
14
+ init() {
15
+ this.context.base.addNode(InterfaceNode.create("Node", [
16
+ FieldNode.create("id", NonNullTypeNode.create(NamedTypeNode.create("ID"))),
17
+ ]));
18
+ }
19
+ match(definition) {
20
+ if (definition instanceof ObjectNode) {
21
+ if (definition.hasInterface("Node") || definition.hasDirective("model")) {
22
+ return true;
23
+ }
24
+ }
25
+ return false;
26
+ }
27
+ before() {
28
+ const queryNode = this.context.document.getQueryNode();
29
+ if (!queryNode.hasField("node")) {
30
+ queryNode.addField(FieldNode.create("node", NamedTypeNode.create("Node"), [InputValueNode.create("id", NonNullTypeNode.create(NamedTypeNode.create("ID")))], [DirectiveNode.create("hasOne")]));
31
+ }
32
+ }
33
+ execute(definition) {
34
+ const nodeInterface = this.context.document.getNode("Node");
35
+ if (!nodeInterface) {
36
+ throw new TransformerPluginExecutionError(this.name, "Node Interface not found. Make sure you run `plugin.init()` before executing.");
37
+ }
38
+ // In definition has directive `@model` it should also implement `Node` interface
39
+ if (definition.hasDirective("model")) {
40
+ definition.addInterface(nodeInterface.name);
41
+ }
42
+ // Make sure that all fields declared by `Node` interface are declared by definition as well
43
+ const nodeFields = nodeInterface.fields ?? [];
44
+ for (const field of nodeFields) {
45
+ if (!definition.hasField(field.name)) {
46
+ definition.addField(FieldNode.fromDefinition(field.serialize()));
47
+ }
48
+ else {
49
+ const nodeFieldTypeName = field.type.getTypeName();
50
+ const fieldTypename = definition.getField(field.name)?.type.getTypeName();
51
+ if (nodeFieldTypeName !== fieldTypename) {
52
+ throw new TransformerPluginExecutionError(this.name, `Field ${field.name} in ${definition.name} has different type than the one declared in Node interface. Expected ${nodeFieldTypeName}, got ${fieldTypename}`);
53
+ }
54
+ }
55
+ }
56
+ }
57
+ after() {
58
+ const iface = this.context.document.getNode("Node");
59
+ if (!iface || !(iface instanceof InterfaceNode)) {
60
+ throw new TransformerPluginExecutionError(this.name, "Node Interface not found. Make sure you run `plugin.init()` before executing.");
61
+ }
62
+ // Node interface should have only 1 field, the `id`
63
+ for (const field of iface.fields ?? []) {
64
+ if (field.name !== "id") {
65
+ iface.removeField(field.name);
66
+ }
67
+ }
68
+ }
69
+ }
70
+ export const nodeInterfacePlugin = createPluginFactory(NodeInterfacePlugin);
@@ -1,2 +1,3 @@
1
1
  export { modelPlugin, isModel, ModelPlugin, type ModelPluginOptions, ModelOperation, ModelDirective, } from "./ModelPlugin.js";
2
+ export { NodeInterfacePlugin, nodeInterfacePlugin } from "./NodeInterfacePlugin.js";
2
3
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/base/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,WAAW,EACX,OAAO,EACP,WAAW,EACX,KAAK,kBAAkB,EACvB,cAAc,EACd,cAAc,GACf,MAAM,kBAAkB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/base/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,WAAW,EACX,OAAO,EACP,WAAW,EACX,KAAK,kBAAkB,EACvB,cAAc,EACd,cAAc,GACf,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,mBAAmB,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC"}
@@ -1 +1,2 @@
1
1
  export { modelPlugin, isModel, ModelPlugin, ModelOperation, ModelDirective, } from "./ModelPlugin.js";
2
+ export { NodeInterfacePlugin, nodeInterfacePlugin } from "./NodeInterfacePlugin.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gqlbase/plugins",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "description": "Collection of plugins and presets for gqlbase, providing common transformations and utilities that can be used in GraphQL schema transformation processes.",
5
5
  "sideEffects": false,
6
6
  "type": "module",