graphql-query-merger 1.0.0 → 1.0.1

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/dist/index.es.js CHANGED
@@ -1,106 +1,143 @@
1
- var d = (c, t) => () => (t || c((t = { exports: {} }).exports, t), t.exports);
2
- var T = d((A, m) => {
3
- const { Kind: i, parse: E, visit: N } = require("graphql");
4
- function I(c, t, s) {
5
- const o = {}, l = {}, r = N(c, {
6
- [i.VARIABLE_DEFINITION](e) {
7
- const n = e.variable.name.value, a = `${n}_${s}`;
8
- return o[n] = a, {
9
- ...e,
10
- variable: {
11
- ...e.variable,
12
- name: { kind: i.NAME, value: a }
13
- }
14
- };
15
- },
16
- [i.VARIABLE](e) {
17
- const n = e.name.value, a = o[n] || n;
18
- return {
19
- ...e,
20
- name: { kind: i.NAME, value: a }
21
- };
22
- },
23
- [i.FRAGMENT_DEFINITION](e) {
24
- const n = e.name.value, a = `${n}_${s}`;
25
- return l[n] = a, {
26
- ...e,
27
- name: { kind: i.NAME, value: a }
28
- };
29
- },
30
- [i.FRAGMENT_SPREAD](e) {
31
- const n = e.name.value, a = l[n] || n;
32
- return {
33
- ...e,
34
- name: { kind: i.NAME, value: a }
35
- };
36
- },
37
- [i.DIRECTIVE](e) {
38
- return N(e, {
39
- [i.VARIABLE](n) {
40
- const a = n.name.value, p = o[a] || a;
41
- return {
42
- ...n,
43
- name: { kind: i.NAME, value: p }
44
- };
45
- }
46
- });
1
+ var __getOwnPropNames = Object.getOwnPropertyNames;
2
+ var __commonJS = (cb, mod) => function __require() {
3
+ return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
4
+ };
5
+ var require_index_es = __commonJS({
6
+ "index.es.js"(exports, module) {
7
+ const { Kind, parse, visit } = require("graphql");
8
+ function renameVariablesAndFragments(document, variables, uniqueId) {
9
+ const variableNameMap = {};
10
+ const fragmentNameMap = {};
11
+ const newDocument = visit(document, {
12
+ [Kind.VARIABLE_DEFINITION](node) {
13
+ const oldName = node.variable.name.value;
14
+ const newName = `${oldName}_${uniqueId}`;
15
+ variableNameMap[oldName] = newName;
16
+ return {
17
+ ...node,
18
+ variable: {
19
+ ...node.variable,
20
+ name: { kind: Kind.NAME, value: newName }
21
+ }
22
+ };
23
+ },
24
+ [Kind.VARIABLE](node) {
25
+ const oldName = node.name.value;
26
+ const newName = variableNameMap[oldName] || oldName;
27
+ return {
28
+ ...node,
29
+ name: { kind: Kind.NAME, value: newName }
30
+ };
31
+ },
32
+ [Kind.FRAGMENT_DEFINITION](node) {
33
+ const oldName = node.name.value;
34
+ const newName = `${oldName}_${uniqueId}`;
35
+ fragmentNameMap[oldName] = newName;
36
+ return {
37
+ ...node,
38
+ name: { kind: Kind.NAME, value: newName }
39
+ };
40
+ },
41
+ [Kind.FRAGMENT_SPREAD](node) {
42
+ const oldName = node.name.value;
43
+ const newName = fragmentNameMap[oldName] || oldName;
44
+ return {
45
+ ...node,
46
+ name: { kind: Kind.NAME, value: newName }
47
+ };
48
+ },
49
+ [Kind.DIRECTIVE](node) {
50
+ return visit(node, {
51
+ [Kind.VARIABLE](varNode) {
52
+ const oldName = varNode.name.value;
53
+ const newName = variableNameMap[oldName] || oldName;
54
+ return {
55
+ ...varNode,
56
+ name: { kind: Kind.NAME, value: newName }
57
+ };
58
+ }
59
+ });
60
+ }
61
+ });
62
+ const newVariables = {};
63
+ for (const [key, value] of Object.entries(variables)) {
64
+ const newKey = variableNameMap[key] || key;
65
+ newVariables[newKey] = value;
47
66
  }
48
- }), u = {};
49
- for (const [e, n] of Object.entries(t)) {
50
- const a = o[e] || e;
51
- u[a] = n;
52
- }
53
- return { document: r, variables: u };
54
- }
55
- class f {
56
- constructor(t) {
57
- this.operationName = t, this.documents = [], this.internalVariables = [], this.uniqueIdCounter = 1, this.operationType = null;
67
+ return { document: newDocument, variables: newVariables };
58
68
  }
59
- push(t, s = {}) {
60
- typeof t == "string" && (t = E(t));
61
- const o = this.uniqueIdCounter++, { document: l, variables: r } = I(t, s, o), u = l.definitions.filter(
62
- (n) => n.kind === i.OPERATION_DEFINITION
63
- );
64
- if (u.length === 0)
65
- throw new Error(
66
- "No se encontró una definición de operación en el documento proporcionado."
67
- );
68
- const e = u[0].operation;
69
- if (this.operationType && this.operationType !== e)
70
- throw new Error(
71
- `No se pueden combinar diferentes tipos de operaciones: ${this.operationType} y ${e}.`
69
+ class GraphQLQueryMerger {
70
+ constructor(operationName) {
71
+ this.operationName = operationName;
72
+ this.documents = [];
73
+ this.internalVariables = [];
74
+ this.uniqueIdCounter = 1;
75
+ this.operationType = null;
76
+ }
77
+ push(document, variables = {}) {
78
+ if (typeof document === "string") {
79
+ document = parse(document);
80
+ }
81
+ const uniqueId = this.uniqueIdCounter++;
82
+ const { document: renamedDocument, variables: renamedVariables } = renameVariablesAndFragments(document, variables, uniqueId);
83
+ const operationDefinitions = renamedDocument.definitions.filter(
84
+ (def) => def.kind === Kind.OPERATION_DEFINITION
72
85
  );
73
- return this.operationType = e, this.documents.push(l), this.internalVariables.push(r), this;
74
- }
75
- get query() {
76
- const t = [], s = [], o = [];
77
- for (const l of this.documents)
78
- for (const r of l.definitions)
79
- r.kind === i.OPERATION_DEFINITION ? (t.push(...r.variableDefinitions || []), s.push(...r.selectionSet.selections)) : r.kind === i.FRAGMENT_DEFINITION && o.push(r);
80
- return {
81
- kind: i.DOCUMENT,
82
- definitions: [
83
- {
84
- kind: i.OPERATION_DEFINITION,
85
- operation: this.operationType,
86
- name: { kind: i.NAME, value: this.operationName || "" },
87
- variableDefinitions: t,
88
- selectionSet: {
89
- kind: i.SELECTION_SET,
90
- selections: s
86
+ if (operationDefinitions.length === 0) {
87
+ throw new Error(
88
+ "No se encontró una definición de operación en el documento proporcionado."
89
+ );
90
+ }
91
+ const operationType = operationDefinitions[0].operation;
92
+ if (this.operationType && this.operationType !== operationType) {
93
+ throw new Error(
94
+ `No se pueden combinar diferentes tipos de operaciones: ${this.operationType} y ${operationType}.`
95
+ );
96
+ }
97
+ this.operationType = operationType;
98
+ this.documents.push(renamedDocument);
99
+ this.internalVariables.push(renamedVariables);
100
+ return this;
101
+ }
102
+ get query() {
103
+ const allVariableDefinitions = [];
104
+ const allSelections = [];
105
+ const allFragmentDefinitions = [];
106
+ for (const doc of this.documents) {
107
+ for (const definition of doc.definitions) {
108
+ if (definition.kind === Kind.OPERATION_DEFINITION) {
109
+ allVariableDefinitions.push(...definition.variableDefinitions || []);
110
+ allSelections.push(...definition.selectionSet.selections);
111
+ } else if (definition.kind === Kind.FRAGMENT_DEFINITION) {
112
+ allFragmentDefinitions.push(definition);
91
113
  }
92
- },
93
- ...o
94
- ]
95
- };
114
+ }
115
+ }
116
+ return {
117
+ kind: Kind.DOCUMENT,
118
+ definitions: [
119
+ {
120
+ kind: Kind.OPERATION_DEFINITION,
121
+ operation: this.operationType,
122
+ name: { kind: Kind.NAME, value: this.operationName || "" },
123
+ variableDefinitions: allVariableDefinitions,
124
+ selectionSet: {
125
+ kind: Kind.SELECTION_SET,
126
+ selections: allSelections
127
+ }
128
+ },
129
+ ...allFragmentDefinitions
130
+ ]
131
+ };
132
+ }
133
+ get variables() {
134
+ return Object.assign({}, ...this.internalVariables);
135
+ }
96
136
  }
97
- get variables() {
98
- return Object.assign({}, ...this.internalVariables);
137
+ function mergeQueries(operationName = "") {
138
+ return new GraphQLQueryMerger(operationName);
99
139
  }
140
+ module.exports = { mergeQueries };
100
141
  }
101
- function h(c = "") {
102
- return new f(c);
103
- }
104
- m.exports = { mergeQueries: h };
105
142
  });
106
- export default T();
143
+ export default require_index_es();
package/dist/index.umd.js CHANGED
@@ -1 +1,139 @@
1
- (function(e){typeof define=="function"&&define.amd?define(e):e()})(function(){"use strict";const{Kind:e,parse:m,visit:N}=require("graphql");function p(c,a,r){const o={},l={},s=N(c,{[e.VARIABLE_DEFINITION](n){const i=n.variable.name.value,t=`${i}_${r}`;return o[i]=t,{...n,variable:{...n.variable,name:{kind:e.NAME,value:t}}}},[e.VARIABLE](n){const i=n.name.value,t=o[i]||i;return{...n,name:{kind:e.NAME,value:t}}},[e.FRAGMENT_DEFINITION](n){const i=n.name.value,t=`${i}_${r}`;return l[i]=t,{...n,name:{kind:e.NAME,value:t}}},[e.FRAGMENT_SPREAD](n){const i=n.name.value,t=l[i]||i;return{...n,name:{kind:e.NAME,value:t}}},[e.DIRECTIVE](n){return N(n,{[e.VARIABLE](i){const t=i.name.value,E=o[t]||t;return{...i,name:{kind:e.NAME,value:E}}}})}}),u={};for(const[n,i]of Object.entries(a)){const t=o[n]||n;u[t]=i}return{document:s,variables:u}}class d{constructor(a){this.operationName=a,this.documents=[],this.internalVariables=[],this.uniqueIdCounter=1,this.operationType=null}push(a,r={}){typeof a=="string"&&(a=m(a));const o=this.uniqueIdCounter++,{document:l,variables:s}=p(a,r,o),u=l.definitions.filter(i=>i.kind===e.OPERATION_DEFINITION);if(u.length===0)throw new Error("No se encontró una definición de operación en el documento proporcionado.");const n=u[0].operation;if(this.operationType&&this.operationType!==n)throw new Error(`No se pueden combinar diferentes tipos de operaciones: ${this.operationType} y ${n}.`);return this.operationType=n,this.documents.push(l),this.internalVariables.push(s),this}get query(){const a=[],r=[],o=[];for(const l of this.documents)for(const s of l.definitions)s.kind===e.OPERATION_DEFINITION?(a.push(...s.variableDefinitions||[]),r.push(...s.selectionSet.selections)):s.kind===e.FRAGMENT_DEFINITION&&o.push(s);return{kind:e.DOCUMENT,definitions:[{kind:e.OPERATION_DEFINITION,operation:this.operationType,name:{kind:e.NAME,value:this.operationName||""},variableDefinitions:a,selectionSet:{kind:e.SELECTION_SET,selections:r}},...o]}}get variables(){return Object.assign({},...this.internalVariables)}}function f(c=""){return new d(c)}module.exports={mergeQueries:f}});
1
+ (function(factory) {
2
+ typeof define === "function" && define.amd ? define(factory) : factory();
3
+ })(function() {
4
+ "use strict";
5
+ const { Kind, parse, visit } = require("graphql");
6
+ function renameVariablesAndFragments(document, variables, uniqueId) {
7
+ const variableNameMap = {};
8
+ const fragmentNameMap = {};
9
+ const newDocument = visit(document, {
10
+ [Kind.VARIABLE_DEFINITION](node) {
11
+ const oldName = node.variable.name.value;
12
+ const newName = `${oldName}_${uniqueId}`;
13
+ variableNameMap[oldName] = newName;
14
+ return {
15
+ ...node,
16
+ variable: {
17
+ ...node.variable,
18
+ name: { kind: Kind.NAME, value: newName }
19
+ }
20
+ };
21
+ },
22
+ [Kind.VARIABLE](node) {
23
+ const oldName = node.name.value;
24
+ const newName = variableNameMap[oldName] || oldName;
25
+ return {
26
+ ...node,
27
+ name: { kind: Kind.NAME, value: newName }
28
+ };
29
+ },
30
+ [Kind.FRAGMENT_DEFINITION](node) {
31
+ const oldName = node.name.value;
32
+ const newName = `${oldName}_${uniqueId}`;
33
+ fragmentNameMap[oldName] = newName;
34
+ return {
35
+ ...node,
36
+ name: { kind: Kind.NAME, value: newName }
37
+ };
38
+ },
39
+ [Kind.FRAGMENT_SPREAD](node) {
40
+ const oldName = node.name.value;
41
+ const newName = fragmentNameMap[oldName] || oldName;
42
+ return {
43
+ ...node,
44
+ name: { kind: Kind.NAME, value: newName }
45
+ };
46
+ },
47
+ [Kind.DIRECTIVE](node) {
48
+ return visit(node, {
49
+ [Kind.VARIABLE](varNode) {
50
+ const oldName = varNode.name.value;
51
+ const newName = variableNameMap[oldName] || oldName;
52
+ return {
53
+ ...varNode,
54
+ name: { kind: Kind.NAME, value: newName }
55
+ };
56
+ }
57
+ });
58
+ }
59
+ });
60
+ const newVariables = {};
61
+ for (const [key, value] of Object.entries(variables)) {
62
+ const newKey = variableNameMap[key] || key;
63
+ newVariables[newKey] = value;
64
+ }
65
+ return { document: newDocument, variables: newVariables };
66
+ }
67
+ class GraphQLQueryMerger {
68
+ constructor(operationName) {
69
+ this.operationName = operationName;
70
+ this.documents = [];
71
+ this.internalVariables = [];
72
+ this.uniqueIdCounter = 1;
73
+ this.operationType = null;
74
+ }
75
+ push(document, variables = {}) {
76
+ if (typeof document === "string") {
77
+ document = parse(document);
78
+ }
79
+ const uniqueId = this.uniqueIdCounter++;
80
+ const { document: renamedDocument, variables: renamedVariables } = renameVariablesAndFragments(document, variables, uniqueId);
81
+ const operationDefinitions = renamedDocument.definitions.filter(
82
+ (def) => def.kind === Kind.OPERATION_DEFINITION
83
+ );
84
+ if (operationDefinitions.length === 0) {
85
+ throw new Error(
86
+ "No se encontró una definición de operación en el documento proporcionado."
87
+ );
88
+ }
89
+ const operationType = operationDefinitions[0].operation;
90
+ if (this.operationType && this.operationType !== operationType) {
91
+ throw new Error(
92
+ `No se pueden combinar diferentes tipos de operaciones: ${this.operationType} y ${operationType}.`
93
+ );
94
+ }
95
+ this.operationType = operationType;
96
+ this.documents.push(renamedDocument);
97
+ this.internalVariables.push(renamedVariables);
98
+ return this;
99
+ }
100
+ get query() {
101
+ const allVariableDefinitions = [];
102
+ const allSelections = [];
103
+ const allFragmentDefinitions = [];
104
+ for (const doc of this.documents) {
105
+ for (const definition of doc.definitions) {
106
+ if (definition.kind === Kind.OPERATION_DEFINITION) {
107
+ allVariableDefinitions.push(...definition.variableDefinitions || []);
108
+ allSelections.push(...definition.selectionSet.selections);
109
+ } else if (definition.kind === Kind.FRAGMENT_DEFINITION) {
110
+ allFragmentDefinitions.push(definition);
111
+ }
112
+ }
113
+ }
114
+ return {
115
+ kind: Kind.DOCUMENT,
116
+ definitions: [
117
+ {
118
+ kind: Kind.OPERATION_DEFINITION,
119
+ operation: this.operationType,
120
+ name: { kind: Kind.NAME, value: this.operationName || "" },
121
+ variableDefinitions: allVariableDefinitions,
122
+ selectionSet: {
123
+ kind: Kind.SELECTION_SET,
124
+ selections: allSelections
125
+ }
126
+ },
127
+ ...allFragmentDefinitions
128
+ ]
129
+ };
130
+ }
131
+ get variables() {
132
+ return Object.assign({}, ...this.internalVariables);
133
+ }
134
+ }
135
+ function mergeQueries(operationName = "") {
136
+ return new GraphQLQueryMerger(operationName);
137
+ }
138
+ module.exports = { mergeQueries };
139
+ });
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "graphql-query-merger",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "Merge and unify multiple GraphQL queries into a single operation with field and variable conflict resolution.",
5
- "main": "dist/index.cjs.js",
5
+ "main": "dist/index.umd.js",
6
6
  "module": "dist/index.es.js",
7
7
  "types": "dist/index.d.ts",
8
8
  "files": [