@graphitation/graphql-codegen-supermassive-typed-document-node-plugin 0.1.1 → 0.1.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/CHANGELOG.md +10 -2
- package/lib/index.d.ts +5 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +76 -0
- package/lib/index.mjs +65 -0
- package/lib/visitor.d.ts +10 -0
- package/lib/visitor.d.ts.map +1 -0
- package/lib/visitor.js +113 -0
- package/lib/visitor.mjs +103 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,12 +1,20 @@
|
|
|
1
1
|
# Change Log - @graphitation/graphql-codegen-supermassive-typed-document-node-plugin
|
|
2
2
|
|
|
3
|
-
This log was last generated on
|
|
3
|
+
This log was last generated on Mon, 20 Jun 2022 12:54:09 GMT and should not be manually modified.
|
|
4
4
|
|
|
5
5
|
<!-- Start content -->
|
|
6
6
|
|
|
7
|
+
## 0.1.2
|
|
8
|
+
|
|
9
|
+
Mon, 20 Jun 2022 12:54:09 GMT
|
|
10
|
+
|
|
11
|
+
### Patches
|
|
12
|
+
|
|
13
|
+
- version bump - npmignore modified (jakubvejr@microsoft.com)
|
|
14
|
+
|
|
7
15
|
## 0.1.1
|
|
8
16
|
|
|
9
|
-
Thu, 16 Jun 2022 21:57:
|
|
17
|
+
Thu, 16 Jun 2022 21:57:11 GMT
|
|
10
18
|
|
|
11
19
|
### Patches
|
|
12
20
|
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { PluginValidateFn, PluginFunction } from "@graphql-codegen/plugin-helpers";
|
|
2
|
+
import { RawClientSideBasePluginConfig } from "@graphql-codegen/visitor-plugin-common";
|
|
3
|
+
export declare const plugin: PluginFunction<RawClientSideBasePluginConfig>;
|
|
4
|
+
export declare const validate: PluginValidateFn<RawClientSideBasePluginConfig>;
|
|
5
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAKA,OAAO,EAEL,gBAAgB,EAChB,cAAc,EACf,MAAM,iCAAiC,CAAC;AAYzC,OAAO,EAEL,6BAA6B,EAE9B,MAAM,wCAAwC,CAAC;AAGhD,eAAO,MAAM,MAAM,EAAE,cAAc,CAAC,6BAA6B,CAoChE,CAAC;AAEF,eAAO,MAAM,QAAQ,EAAE,gBAAgB,CAAC,6BAA6B,CAiBpE,CAAC"}
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
var __async = (__this, __arguments, generator) => {
|
|
19
|
+
return new Promise((resolve, reject) => {
|
|
20
|
+
var fulfilled = (value) => {
|
|
21
|
+
try {
|
|
22
|
+
step(generator.next(value));
|
|
23
|
+
} catch (e) {
|
|
24
|
+
reject(e);
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
var rejected = (value) => {
|
|
28
|
+
try {
|
|
29
|
+
step(generator.throw(value));
|
|
30
|
+
} catch (e) {
|
|
31
|
+
reject(e);
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
35
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
36
|
+
});
|
|
37
|
+
};
|
|
38
|
+
var src_exports = {};
|
|
39
|
+
__export(src_exports, {
|
|
40
|
+
plugin: () => plugin,
|
|
41
|
+
validate: () => validate
|
|
42
|
+
});
|
|
43
|
+
module.exports = __toCommonJS(src_exports);
|
|
44
|
+
var import_graphql = require("graphql");
|
|
45
|
+
var import_path = require("path");
|
|
46
|
+
var import_visitor_plugin_common = require("@graphql-codegen/visitor-plugin-common");
|
|
47
|
+
var import_visitor = require("./visitor");
|
|
48
|
+
const plugin = (schema, documents, config) => {
|
|
49
|
+
const allAst = (0, import_graphql.concatAST)(documents.map((v) => v.document));
|
|
50
|
+
const allFragments = [
|
|
51
|
+
...allAst.definitions.filter((d) => d.kind === import_graphql.Kind.FRAGMENT_DEFINITION).map((fragmentDef) => ({
|
|
52
|
+
node: fragmentDef,
|
|
53
|
+
name: fragmentDef.name.value,
|
|
54
|
+
onType: fragmentDef.typeCondition.name.value,
|
|
55
|
+
isExternal: false
|
|
56
|
+
})),
|
|
57
|
+
...config.externalFragments || []
|
|
58
|
+
];
|
|
59
|
+
const visitor = new import_visitor.TypeScriptDocumentNodesVisitor(schema, allFragments, config, documents);
|
|
60
|
+
const visitorResult = (0, import_graphql.visit)(allAst, { leave: visitor });
|
|
61
|
+
return {
|
|
62
|
+
prepend: allAst.definitions.length === 0 ? [] : visitor.getImports(),
|
|
63
|
+
content: [
|
|
64
|
+
visitor.fragments,
|
|
65
|
+
...visitorResult.definitions.filter((t) => typeof t === "string")
|
|
66
|
+
].join("\n")
|
|
67
|
+
};
|
|
68
|
+
};
|
|
69
|
+
const validate = (schema, documents, config, outputFile) => __async(void 0, null, function* () {
|
|
70
|
+
if (config && config.documentMode === import_visitor_plugin_common.DocumentMode.string) {
|
|
71
|
+
throw new Error(`Plugin "supermassive-typed-document-node" does not allow using 'documentMode: string' configuration!`);
|
|
72
|
+
}
|
|
73
|
+
if ((0, import_path.extname)(outputFile) !== ".ts" && (0, import_path.extname)(outputFile) !== ".tsx") {
|
|
74
|
+
throw new Error(`Plugin "supermassive-typed-document-node" requires extension to be ".ts" or ".tsx"!`);
|
|
75
|
+
}
|
|
76
|
+
});
|
package/lib/index.mjs
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
var __async = (__this, __arguments, generator) => {
|
|
2
|
+
return new Promise((resolve, reject) => {
|
|
3
|
+
var fulfilled = (value) => {
|
|
4
|
+
try {
|
|
5
|
+
step(generator.next(value));
|
|
6
|
+
} catch (e) {
|
|
7
|
+
reject(e);
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
var rejected = (value) => {
|
|
11
|
+
try {
|
|
12
|
+
step(generator.throw(value));
|
|
13
|
+
} catch (e) {
|
|
14
|
+
reject(e);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
18
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
// src/index.ts
|
|
23
|
+
import {
|
|
24
|
+
visit,
|
|
25
|
+
concatAST,
|
|
26
|
+
Kind
|
|
27
|
+
} from "graphql";
|
|
28
|
+
import { extname } from "path";
|
|
29
|
+
import {
|
|
30
|
+
DocumentMode
|
|
31
|
+
} from "@graphql-codegen/visitor-plugin-common";
|
|
32
|
+
import { TypeScriptDocumentNodesVisitor } from "./visitor.mjs";
|
|
33
|
+
var plugin = (schema, documents, config) => {
|
|
34
|
+
const allAst = concatAST(documents.map((v) => v.document));
|
|
35
|
+
const allFragments = [
|
|
36
|
+
...allAst.definitions.filter((d) => d.kind === Kind.FRAGMENT_DEFINITION).map((fragmentDef) => ({
|
|
37
|
+
node: fragmentDef,
|
|
38
|
+
name: fragmentDef.name.value,
|
|
39
|
+
onType: fragmentDef.typeCondition.name.value,
|
|
40
|
+
isExternal: false
|
|
41
|
+
})),
|
|
42
|
+
...config.externalFragments || []
|
|
43
|
+
];
|
|
44
|
+
const visitor = new TypeScriptDocumentNodesVisitor(schema, allFragments, config, documents);
|
|
45
|
+
const visitorResult = visit(allAst, { leave: visitor });
|
|
46
|
+
return {
|
|
47
|
+
prepend: allAst.definitions.length === 0 ? [] : visitor.getImports(),
|
|
48
|
+
content: [
|
|
49
|
+
visitor.fragments,
|
|
50
|
+
...visitorResult.definitions.filter((t) => typeof t === "string")
|
|
51
|
+
].join("\n")
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
var validate = (schema, documents, config, outputFile) => __async(void 0, null, function* () {
|
|
55
|
+
if (config && config.documentMode === DocumentMode.string) {
|
|
56
|
+
throw new Error(`Plugin "supermassive-typed-document-node" does not allow using 'documentMode: string' configuration!`);
|
|
57
|
+
}
|
|
58
|
+
if (extname(outputFile) !== ".ts" && extname(outputFile) !== ".tsx") {
|
|
59
|
+
throw new Error(`Plugin "supermassive-typed-document-node" requires extension to be ".ts" or ".tsx"!`);
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
export {
|
|
63
|
+
plugin,
|
|
64
|
+
validate
|
|
65
|
+
};
|
package/lib/visitor.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Types } from "@graphql-codegen/plugin-helpers";
|
|
2
|
+
import { LoadedFragment, ClientSideBaseVisitor, ClientSideBasePluginConfig, RawClientSideBasePluginConfig } from "@graphql-codegen/visitor-plugin-common";
|
|
3
|
+
import { GraphQLSchema, FragmentDefinitionNode, OperationDefinitionNode } from "graphql";
|
|
4
|
+
export declare class TypeScriptDocumentNodesVisitor extends ClientSideBaseVisitor<RawClientSideBasePluginConfig, ClientSideBasePluginConfig> {
|
|
5
|
+
constructor(schema: GraphQLSchema, fragments: LoadedFragment[], rawConfig: RawClientSideBasePluginConfig, documents: Types.DocumentFile[]);
|
|
6
|
+
protected _gql(node: FragmentDefinitionNode | OperationDefinitionNode): string;
|
|
7
|
+
private _transformDocumentNodeToSupermassive;
|
|
8
|
+
protected getDocumentNodeSignature(resultType: string, variablesTypes: string, node: FragmentDefinitionNode | OperationDefinitionNode): string;
|
|
9
|
+
}
|
|
10
|
+
//# sourceMappingURL=visitor.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"visitor.d.ts","sourceRoot":"","sources":["../src/visitor.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,KAAK,EAAE,MAAM,iCAAiC,CAAC;AACxD,OAAO,EACL,cAAc,EACd,qBAAqB,EACrB,0BAA0B,EAE1B,6BAA6B,EAC9B,MAAM,wCAAwC,CAAC;AAChD,OAAO,EACL,aAAa,EACb,sBAAsB,EAItB,uBAAuB,EACxB,MAAM,SAAS,CAAC;AAQjB,qBAAa,8BAA+B,SAAQ,qBAAqB,CACvE,6BAA6B,EAC7B,0BAA0B,CAC3B;gBAEG,MAAM,EAAE,aAAa,EACrB,SAAS,EAAE,cAAc,EAAE,EAC3B,SAAS,EAAE,6BAA6B,EACxC,SAAS,EAAE,KAAK,CAAC,YAAY,EAAE;IA+BjC,SAAS,CAAC,IAAI,CACZ,IAAI,EAAE,sBAAsB,GAAG,uBAAuB,GACrD,MAAM;IA2DT,OAAO,CAAC,oCAAoC;IAY5C,SAAS,CAAC,wBAAwB,CAChC,UAAU,EAAE,MAAM,EAClB,cAAc,EAAE,MAAM,EACtB,IAAI,EAAE,sBAAsB,GAAG,uBAAuB;CAYzD"}
|
package/lib/visitor.js
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __defProps = Object.defineProperties;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
8
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
9
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
10
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
11
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
12
|
+
var __spreadValues = (a, b) => {
|
|
13
|
+
for (var prop in b || (b = {}))
|
|
14
|
+
if (__hasOwnProp.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
if (__getOwnPropSymbols)
|
|
17
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
18
|
+
if (__propIsEnum.call(b, prop))
|
|
19
|
+
__defNormalProp(a, prop, b[prop]);
|
|
20
|
+
}
|
|
21
|
+
return a;
|
|
22
|
+
};
|
|
23
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
24
|
+
var __export = (target, all) => {
|
|
25
|
+
for (var name in all)
|
|
26
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
27
|
+
};
|
|
28
|
+
var __copyProps = (to, from, except, desc) => {
|
|
29
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
30
|
+
for (let key of __getOwnPropNames(from))
|
|
31
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
32
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
33
|
+
}
|
|
34
|
+
return to;
|
|
35
|
+
};
|
|
36
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod));
|
|
37
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
38
|
+
var visitor_exports = {};
|
|
39
|
+
__export(visitor_exports, {
|
|
40
|
+
TypeScriptDocumentNodesVisitor: () => TypeScriptDocumentNodesVisitor
|
|
41
|
+
});
|
|
42
|
+
module.exports = __toCommonJS(visitor_exports);
|
|
43
|
+
var import_auto_bind = __toESM(require("auto-bind"));
|
|
44
|
+
var import_visitor_plugin_common = require("@graphql-codegen/visitor-plugin-common");
|
|
45
|
+
var import_graphql = require("graphql");
|
|
46
|
+
var import_supermassive = require("@graphitation/supermassive");
|
|
47
|
+
var import_optimize = require("@graphql-tools/optimize");
|
|
48
|
+
var import_graphql_tag = __toESM(require("graphql-tag"));
|
|
49
|
+
class TypeScriptDocumentNodesVisitor extends import_visitor_plugin_common.ClientSideBaseVisitor {
|
|
50
|
+
constructor(schema, fragments, rawConfig, documents) {
|
|
51
|
+
super(schema, fragments, __spreadValues({
|
|
52
|
+
documentMode: import_visitor_plugin_common.DocumentMode.documentNodeImportFragments,
|
|
53
|
+
documentNodeImport: "@graphql-typed-document-node/core#TypedDocumentNode"
|
|
54
|
+
}, rawConfig), {}, documents);
|
|
55
|
+
(0, import_auto_bind.default)(this);
|
|
56
|
+
if (this.config.documentMode === import_visitor_plugin_common.DocumentMode.graphQLTag) {
|
|
57
|
+
const documentNodeImport = this._parseImport(this.config.documentNodeImport || "graphql#DocumentNode");
|
|
58
|
+
const tagImport = this._generateImport(documentNodeImport, "DocumentNode", true);
|
|
59
|
+
this._imports.add(tagImport);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
_gql(node) {
|
|
63
|
+
const supermassiveNode = (0, import_supermassive.addTypesToRequestDocument)(this._schema, {
|
|
64
|
+
kind: import_graphql.Kind.DOCUMENT,
|
|
65
|
+
definitions: [node]
|
|
66
|
+
}).definitions[0];
|
|
67
|
+
const fragments = this._transformFragments(supermassiveNode);
|
|
68
|
+
const doc = this._prepareDocument(`
|
|
69
|
+
${(0, import_graphql.print)(supermassiveNode).split("\\").join("\\\\")}
|
|
70
|
+
${this._includeFragments(fragments)}`);
|
|
71
|
+
if (this.config.documentMode === import_visitor_plugin_common.DocumentMode.documentNode) {
|
|
72
|
+
let gqlObj = (0, import_graphql_tag.default)([doc]);
|
|
73
|
+
if (this.config.optimizeDocumentNode) {
|
|
74
|
+
gqlObj = (0, import_optimize.optimizeDocumentNode)(gqlObj);
|
|
75
|
+
}
|
|
76
|
+
return JSON.stringify(this._transformDocumentNodeToSupermassive(gqlObj));
|
|
77
|
+
} else if (this.config.documentMode === import_visitor_plugin_common.DocumentMode.documentNodeImportFragments) {
|
|
78
|
+
let gqlObj = (0, import_graphql_tag.default)([doc]);
|
|
79
|
+
if (this.config.optimizeDocumentNode) {
|
|
80
|
+
gqlObj = (0, import_optimize.optimizeDocumentNode)(gqlObj);
|
|
81
|
+
}
|
|
82
|
+
if (fragments.length > 0) {
|
|
83
|
+
const definitions = [
|
|
84
|
+
...gqlObj.definitions.map((t) => JSON.stringify((0, import_supermassive.addTypesToRequestDocument)(this._schema, {
|
|
85
|
+
kind: import_graphql.Kind.DOCUMENT,
|
|
86
|
+
definitions: [t]
|
|
87
|
+
}).definitions[0])),
|
|
88
|
+
...fragments.map((name) => `...${name}.definitions`)
|
|
89
|
+
].join();
|
|
90
|
+
return `{"kind":"${import_graphql.Kind.DOCUMENT}","definitions":[${definitions}]}`;
|
|
91
|
+
}
|
|
92
|
+
return JSON.stringify(this._transformDocumentNodeToSupermassive(gqlObj));
|
|
93
|
+
} else if (this.config.documentMode === import_visitor_plugin_common.DocumentMode.string) {
|
|
94
|
+
return "`" + doc + "`";
|
|
95
|
+
}
|
|
96
|
+
const gqlImport = this._parseImport(this.config.gqlImport || "graphql-tag");
|
|
97
|
+
return (gqlImport.propName || "gql") + "`" + doc + "`";
|
|
98
|
+
}
|
|
99
|
+
_transformDocumentNodeToSupermassive(document) {
|
|
100
|
+
return __spreadProps(__spreadValues({}, document), {
|
|
101
|
+
definitions: document.definitions.map((t) => (0, import_supermassive.addTypesToRequestDocument)(this._schema, {
|
|
102
|
+
kind: import_graphql.Kind.DOCUMENT,
|
|
103
|
+
definitions: [t]
|
|
104
|
+
}).definitions[0])
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
getDocumentNodeSignature(resultType, variablesTypes, node) {
|
|
108
|
+
if (this.config.documentMode === import_visitor_plugin_common.DocumentMode.documentNode || this.config.documentMode === import_visitor_plugin_common.DocumentMode.documentNodeImportFragments || this.config.documentMode === import_visitor_plugin_common.DocumentMode.graphQLTag) {
|
|
109
|
+
return ` as unknown as DocumentNode<${resultType}, ${variablesTypes}>`;
|
|
110
|
+
}
|
|
111
|
+
return super.getDocumentNodeSignature(resultType, variablesTypes, node);
|
|
112
|
+
}
|
|
113
|
+
}
|
package/lib/visitor.mjs
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defProps = Object.defineProperties;
|
|
3
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __spreadValues = (a, b) => {
|
|
9
|
+
for (var prop in b || (b = {}))
|
|
10
|
+
if (__hasOwnProp.call(b, prop))
|
|
11
|
+
__defNormalProp(a, prop, b[prop]);
|
|
12
|
+
if (__getOwnPropSymbols)
|
|
13
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
+
if (__propIsEnum.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
}
|
|
17
|
+
return a;
|
|
18
|
+
};
|
|
19
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
+
|
|
21
|
+
// src/visitor.ts
|
|
22
|
+
import autoBind from "auto-bind";
|
|
23
|
+
import {
|
|
24
|
+
ClientSideBaseVisitor,
|
|
25
|
+
DocumentMode
|
|
26
|
+
} from "@graphql-codegen/visitor-plugin-common";
|
|
27
|
+
import {
|
|
28
|
+
print,
|
|
29
|
+
Kind
|
|
30
|
+
} from "graphql";
|
|
31
|
+
import {
|
|
32
|
+
addTypesToRequestDocument
|
|
33
|
+
} from "@graphitation/supermassive";
|
|
34
|
+
import { optimizeDocumentNode } from "@graphql-tools/optimize";
|
|
35
|
+
import gqlTag from "graphql-tag";
|
|
36
|
+
var TypeScriptDocumentNodesVisitor = class extends ClientSideBaseVisitor {
|
|
37
|
+
constructor(schema, fragments, rawConfig, documents) {
|
|
38
|
+
super(schema, fragments, __spreadValues({
|
|
39
|
+
documentMode: DocumentMode.documentNodeImportFragments,
|
|
40
|
+
documentNodeImport: "@graphql-typed-document-node/core#TypedDocumentNode"
|
|
41
|
+
}, rawConfig), {}, documents);
|
|
42
|
+
autoBind(this);
|
|
43
|
+
if (this.config.documentMode === DocumentMode.graphQLTag) {
|
|
44
|
+
const documentNodeImport = this._parseImport(this.config.documentNodeImport || "graphql#DocumentNode");
|
|
45
|
+
const tagImport = this._generateImport(documentNodeImport, "DocumentNode", true);
|
|
46
|
+
this._imports.add(tagImport);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
_gql(node) {
|
|
50
|
+
const supermassiveNode = addTypesToRequestDocument(this._schema, {
|
|
51
|
+
kind: Kind.DOCUMENT,
|
|
52
|
+
definitions: [node]
|
|
53
|
+
}).definitions[0];
|
|
54
|
+
const fragments = this._transformFragments(supermassiveNode);
|
|
55
|
+
const doc = this._prepareDocument(`
|
|
56
|
+
${print(supermassiveNode).split("\\").join("\\\\")}
|
|
57
|
+
${this._includeFragments(fragments)}`);
|
|
58
|
+
if (this.config.documentMode === DocumentMode.documentNode) {
|
|
59
|
+
let gqlObj = gqlTag([doc]);
|
|
60
|
+
if (this.config.optimizeDocumentNode) {
|
|
61
|
+
gqlObj = optimizeDocumentNode(gqlObj);
|
|
62
|
+
}
|
|
63
|
+
return JSON.stringify(this._transformDocumentNodeToSupermassive(gqlObj));
|
|
64
|
+
} else if (this.config.documentMode === DocumentMode.documentNodeImportFragments) {
|
|
65
|
+
let gqlObj = gqlTag([doc]);
|
|
66
|
+
if (this.config.optimizeDocumentNode) {
|
|
67
|
+
gqlObj = optimizeDocumentNode(gqlObj);
|
|
68
|
+
}
|
|
69
|
+
if (fragments.length > 0) {
|
|
70
|
+
const definitions = [
|
|
71
|
+
...gqlObj.definitions.map((t) => JSON.stringify(addTypesToRequestDocument(this._schema, {
|
|
72
|
+
kind: Kind.DOCUMENT,
|
|
73
|
+
definitions: [t]
|
|
74
|
+
}).definitions[0])),
|
|
75
|
+
...fragments.map((name) => `...${name}.definitions`)
|
|
76
|
+
].join();
|
|
77
|
+
return `{"kind":"${Kind.DOCUMENT}","definitions":[${definitions}]}`;
|
|
78
|
+
}
|
|
79
|
+
return JSON.stringify(this._transformDocumentNodeToSupermassive(gqlObj));
|
|
80
|
+
} else if (this.config.documentMode === DocumentMode.string) {
|
|
81
|
+
return "`" + doc + "`";
|
|
82
|
+
}
|
|
83
|
+
const gqlImport = this._parseImport(this.config.gqlImport || "graphql-tag");
|
|
84
|
+
return (gqlImport.propName || "gql") + "`" + doc + "`";
|
|
85
|
+
}
|
|
86
|
+
_transformDocumentNodeToSupermassive(document) {
|
|
87
|
+
return __spreadProps(__spreadValues({}, document), {
|
|
88
|
+
definitions: document.definitions.map((t) => addTypesToRequestDocument(this._schema, {
|
|
89
|
+
kind: Kind.DOCUMENT,
|
|
90
|
+
definitions: [t]
|
|
91
|
+
}).definitions[0])
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
getDocumentNodeSignature(resultType, variablesTypes, node) {
|
|
95
|
+
if (this.config.documentMode === DocumentMode.documentNode || this.config.documentMode === DocumentMode.documentNodeImportFragments || this.config.documentMode === DocumentMode.graphQLTag) {
|
|
96
|
+
return ` as unknown as DocumentNode<${resultType}, ${variablesTypes}>`;
|
|
97
|
+
}
|
|
98
|
+
return super.getDocumentNodeSignature(resultType, variablesTypes, node);
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
export {
|
|
102
|
+
TypeScriptDocumentNodesVisitor
|
|
103
|
+
};
|