graphql-query-merger 1.1.0 → 1.1.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 +107 -100
- package/dist/index.umd.js +1 -1
- package/package.json +1 -1
package/dist/index.es.js
CHANGED
|
@@ -1,100 +1,107 @@
|
|
|
1
|
-
import { parse as p, Kind as t, visit as m } from "graphql";
|
|
2
|
-
const
|
|
3
|
-
const o = {}, l = {}, r = m(c, {
|
|
4
|
-
[t.VARIABLE_DEFINITION](e) {
|
|
5
|
-
const n = e.variable.name.value, i = `${n}_${s}`;
|
|
6
|
-
return o[n] = i, {
|
|
7
|
-
...e,
|
|
8
|
-
variable: {
|
|
9
|
-
...e.variable,
|
|
10
|
-
name: { kind: t.NAME, value: i }
|
|
11
|
-
}
|
|
12
|
-
};
|
|
13
|
-
},
|
|
14
|
-
[t.VARIABLE](e) {
|
|
15
|
-
const n = e.name.value, i = o[n] || n;
|
|
16
|
-
return {
|
|
17
|
-
...e,
|
|
18
|
-
name: { kind: t.NAME, value: i }
|
|
19
|
-
};
|
|
20
|
-
},
|
|
21
|
-
[t.FRAGMENT_DEFINITION](e) {
|
|
22
|
-
const n = e.name.value, i = `${n}_${s}`;
|
|
23
|
-
return l[n] = i, {
|
|
24
|
-
...e,
|
|
25
|
-
name: { kind: t.NAME, value: i }
|
|
26
|
-
};
|
|
27
|
-
},
|
|
28
|
-
[t.FRAGMENT_SPREAD](e) {
|
|
29
|
-
const n = e.name.value, i = l[n] || n;
|
|
30
|
-
return {
|
|
31
|
-
...e,
|
|
32
|
-
name: { kind: t.NAME, value: i }
|
|
33
|
-
};
|
|
34
|
-
},
|
|
35
|
-
[t.DIRECTIVE](e) {
|
|
36
|
-
return m(e, {
|
|
37
|
-
[t.VARIABLE](n) {
|
|
38
|
-
const i = n.name.value, N = o[i] || i;
|
|
39
|
-
return {
|
|
40
|
-
...n,
|
|
41
|
-
name: { kind: t.NAME, value: N }
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
});
|
|
45
|
-
}
|
|
46
|
-
}), u = {};
|
|
47
|
-
for (const [e, n] of Object.entries(a)) {
|
|
48
|
-
const i = o[e] || e;
|
|
49
|
-
u[i] = n;
|
|
50
|
-
}
|
|
51
|
-
return { document: r, variables: u };
|
|
52
|
-
};
|
|
53
|
-
class
|
|
54
|
-
constructor(a) {
|
|
55
|
-
this.operationName = a, this.documents = [], this.internalVariables = [], this.uniqueIdCounter = 1, this.operationType = null;
|
|
56
|
-
}
|
|
57
|
-
push(a, s = {}) {
|
|
58
|
-
typeof a == "string" && (a = p(a));
|
|
59
|
-
const o = this.uniqueIdCounter++, { document: l, variables: r } =
|
|
60
|
-
(n) => n.kind === t.OPERATION_DEFINITION
|
|
61
|
-
);
|
|
62
|
-
if (u.length === 0)
|
|
63
|
-
throw new Error(
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
1
|
+
import { parse as p, Kind as t, print as d, visit as m } from "graphql";
|
|
2
|
+
const E = (c, a, s) => {
|
|
3
|
+
const o = {}, l = {}, r = m(c, {
|
|
4
|
+
[t.VARIABLE_DEFINITION](e) {
|
|
5
|
+
const n = e.variable.name.value, i = `${n}_${s}`;
|
|
6
|
+
return o[n] = i, {
|
|
7
|
+
...e,
|
|
8
|
+
variable: {
|
|
9
|
+
...e.variable,
|
|
10
|
+
name: { kind: t.NAME, value: i }
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
},
|
|
14
|
+
[t.VARIABLE](e) {
|
|
15
|
+
const n = e.name.value, i = o[n] || n;
|
|
16
|
+
return {
|
|
17
|
+
...e,
|
|
18
|
+
name: { kind: t.NAME, value: i }
|
|
19
|
+
};
|
|
20
|
+
},
|
|
21
|
+
[t.FRAGMENT_DEFINITION](e) {
|
|
22
|
+
const n = e.name.value, i = `${n}_${s}`;
|
|
23
|
+
return l[n] = i, {
|
|
24
|
+
...e,
|
|
25
|
+
name: { kind: t.NAME, value: i }
|
|
26
|
+
};
|
|
27
|
+
},
|
|
28
|
+
[t.FRAGMENT_SPREAD](e) {
|
|
29
|
+
const n = e.name.value, i = l[n] || n;
|
|
30
|
+
return {
|
|
31
|
+
...e,
|
|
32
|
+
name: { kind: t.NAME, value: i }
|
|
33
|
+
};
|
|
34
|
+
},
|
|
35
|
+
[t.DIRECTIVE](e) {
|
|
36
|
+
return m(e, {
|
|
37
|
+
[t.VARIABLE](n) {
|
|
38
|
+
const i = n.name.value, N = o[i] || i;
|
|
39
|
+
return {
|
|
40
|
+
...n,
|
|
41
|
+
name: { kind: t.NAME, value: N }
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
}), u = {};
|
|
47
|
+
for (const [e, n] of Object.entries(a)) {
|
|
48
|
+
const i = o[e] || e;
|
|
49
|
+
u[i] = n;
|
|
50
|
+
}
|
|
51
|
+
return { document: r, variables: u };
|
|
52
|
+
};
|
|
53
|
+
class h {
|
|
54
|
+
constructor(a) {
|
|
55
|
+
this.operationName = a, this.documents = [], this.internalVariables = [], this.uniqueIdCounter = 1, this.operationType = null;
|
|
56
|
+
}
|
|
57
|
+
push(a, s = {}) {
|
|
58
|
+
typeof a == "string" && (a = p(a));
|
|
59
|
+
const o = this.uniqueIdCounter++, { document: l, variables: r } = E(a, s, o), u = l.definitions.filter(
|
|
60
|
+
(n) => n.kind === t.OPERATION_DEFINITION
|
|
61
|
+
);
|
|
62
|
+
if (u.length === 0)
|
|
63
|
+
throw new Error(
|
|
64
|
+
"No operation definition found in the provided document."
|
|
65
|
+
);
|
|
66
|
+
const e = u[0].operation;
|
|
67
|
+
if (this.operationType && this.operationType !== e)
|
|
68
|
+
throw new Error(
|
|
69
|
+
`Cannot merge different operation types: ${this.operationType} and ${e}.`
|
|
70
|
+
);
|
|
71
|
+
return this.operationType = e, this.documents.push(l), this.internalVariables.push(r), this;
|
|
72
|
+
}
|
|
73
|
+
get query() {
|
|
74
|
+
const a = [], s = [], o = [];
|
|
75
|
+
for (const l of this.documents)
|
|
76
|
+
for (const r of l.definitions)
|
|
77
|
+
r.kind === t.OPERATION_DEFINITION ? (a.push(
|
|
78
|
+
...r.variableDefinitions || []
|
|
79
|
+
), s.push(...r.selectionSet.selections)) : r.kind === t.FRAGMENT_DEFINITION && o.push(r);
|
|
80
|
+
return {
|
|
81
|
+
kind: t.DOCUMENT,
|
|
82
|
+
definitions: [
|
|
83
|
+
{
|
|
84
|
+
kind: t.OPERATION_DEFINITION,
|
|
85
|
+
operation: this.operationType,
|
|
86
|
+
name: { kind: t.NAME, value: this.operationName || "" },
|
|
87
|
+
variableDefinitions: a,
|
|
88
|
+
selectionSet: {
|
|
89
|
+
kind: t.SELECTION_SET,
|
|
90
|
+
selections: s
|
|
91
|
+
}
|
|
92
|
+
},
|
|
93
|
+
...o
|
|
94
|
+
]
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
getQueryString() {
|
|
98
|
+
return d(this.query);
|
|
99
|
+
}
|
|
100
|
+
get variables() {
|
|
101
|
+
return Object.assign({}, ...this.internalVariables);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
const f = (c = "") => new h(c);
|
|
105
|
+
export {
|
|
106
|
+
f as mergeQueries
|
|
107
|
+
};
|
package/dist/index.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(
|
|
1
|
+
(function(a,e){typeof exports=="object"&&typeof module<"u"?e(exports,require("graphql")):typeof define=="function"&&define.amd?define(["exports","graphql"],e):(a=typeof globalThis<"u"?globalThis:a||self,e(a["graphql-query-merger"]={},a.graphql))})(this,function(a,e){"use strict";const N=(m,o,u)=>{const s={},d={},r=e.visit(m,{[e.Kind.VARIABLE_DEFINITION](n){const i=n.variable.name.value,t=`${i}_${u}`;return s[i]=t,{...n,variable:{...n.variable,name:{kind:e.Kind.NAME,value:t}}}},[e.Kind.VARIABLE](n){const i=n.name.value,t=s[i]||i;return{...n,name:{kind:e.Kind.NAME,value:t}}},[e.Kind.FRAGMENT_DEFINITION](n){const i=n.name.value,t=`${i}_${u}`;return d[i]=t,{...n,name:{kind:e.Kind.NAME,value:t}}},[e.Kind.FRAGMENT_SPREAD](n){const i=n.name.value,t=d[i]||i;return{...n,name:{kind:e.Kind.NAME,value:t}}},[e.Kind.DIRECTIVE](n){return e.visit(n,{[e.Kind.VARIABLE](i){const t=i.name.value,E=s[t]||t;return{...i,name:{kind:e.Kind.NAME,value:E}}}})}}),c={};for(const[n,i]of Object.entries(o)){const t=s[n]||n;c[t]=i}return{document:r,variables:c}};class f{constructor(o){this.operationName=o,this.documents=[],this.internalVariables=[],this.uniqueIdCounter=1,this.operationType=null}push(o,u={}){typeof o=="string"&&(o=e.parse(o));const s=this.uniqueIdCounter++,{document:d,variables:r}=N(o,u,s),c=d.definitions.filter(i=>i.kind===e.Kind.OPERATION_DEFINITION);if(c.length===0)throw new Error("No operation definition found in the provided document.");const n=c[0].operation;if(this.operationType&&this.operationType!==n)throw new Error(`Cannot merge different operation types: ${this.operationType} and ${n}.`);return this.operationType=n,this.documents.push(d),this.internalVariables.push(r),this}get query(){const o=[],u=[],s=[];for(const d of this.documents)for(const r of d.definitions)r.kind===e.Kind.OPERATION_DEFINITION?(o.push(...r.variableDefinitions||[]),u.push(...r.selectionSet.selections)):r.kind===e.Kind.FRAGMENT_DEFINITION&&s.push(r);return{kind:e.Kind.DOCUMENT,definitions:[{kind:e.Kind.OPERATION_DEFINITION,operation:this.operationType,name:{kind:e.Kind.NAME,value:this.operationName||""},variableDefinitions:o,selectionSet:{kind:e.Kind.SELECTION_SET,selections:u}},...s]}}getQueryString(){return e.print(this.query)}get variables(){return Object.assign({},...this.internalVariables)}}const l=(m="")=>new f(m);a.mergeQueries=l,Object.defineProperty(a,Symbol.toStringTag,{value:"Module"})});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "graphql-query-merger",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"description": "Merge and unify multiple GraphQL queries into a single operation with field and variable conflict resolution.",
|
|
5
5
|
"main": "dist/index.umd.js",
|
|
6
6
|
"module": "dist/index.es.js",
|