graphql-query-merger 1.0.3 → 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/README.md +231 -231
- package/dist/index.es.js +14 -7
- package/dist/index.umd.js +1 -1
- package/package.json +30 -30
package/README.md
CHANGED
|
@@ -1,231 +1,231 @@
|
|
|
1
|
-
# GraphQL Query Merger
|
|
2
|
-
|
|
3
|
-
**GraphQL Query Merger** is a utility library designed to merge multiple GraphQL queries into a single, cohesive query. This helps developers streamline their GraphQL requests by combining related operations efficiently, avoiding redundancy and improving maintainability.
|
|
4
|
-
|
|
5
|
-
---
|
|
6
|
-
|
|
7
|
-
## Table of Contents
|
|
8
|
-
|
|
9
|
-
- [GraphQL Query Merger](#graphql-query-merger)
|
|
10
|
-
- [Table of Contents](#table-of-contents)
|
|
11
|
-
- [Features](#features)
|
|
12
|
-
- [Installation](#installation)
|
|
13
|
-
- [Usage](#usage)
|
|
14
|
-
- [Importing the Library](#importing-the-library)
|
|
15
|
-
- [Combining Queries](#combining-queries)
|
|
16
|
-
- [Basic Query Combination](#basic-query-combination)
|
|
17
|
-
- [Combining Queries with Variables](#combining-queries-with-variables)
|
|
18
|
-
- [Conditional Query Combination](#conditional-query-combination)
|
|
19
|
-
- [Notes](#notes)
|
|
20
|
-
|
|
21
|
-
---
|
|
22
|
-
|
|
23
|
-
## Features
|
|
24
|
-
|
|
25
|
-
- **Merge GraphQL Queries**: Seamlessly combine multiple queries into one.
|
|
26
|
-
- **Variable Deduplication**: Automatically handles conflicts by renaming variables.
|
|
27
|
-
- **Supports Conditional Queries**: Add queries dynamically based on runtime logic.
|
|
28
|
-
- **Customizable Query Names**: Optionally name the resulting query.
|
|
29
|
-
|
|
30
|
-
---
|
|
31
|
-
|
|
32
|
-
## Installation
|
|
33
|
-
|
|
34
|
-
To install the library, use npm or yarn:
|
|
35
|
-
|
|
36
|
-
```bash
|
|
37
|
-
npm install graphql-query-merger graphql
|
|
38
|
-
```
|
|
39
|
-
|
|
40
|
-
> **Note**: `graphql-query-merger` has a peer dependency on `graphql`. Ensure that the `graphql` package is installed alongside it.
|
|
41
|
-
|
|
42
|
-
---
|
|
43
|
-
|
|
44
|
-
## Usage
|
|
45
|
-
|
|
46
|
-
### Importing the Library
|
|
47
|
-
|
|
48
|
-
To use the library, import the necessary functions:
|
|
49
|
-
|
|
50
|
-
```javascript
|
|
51
|
-
const { mergeQueries } = require('graphql-query-merger');
|
|
52
|
-
const { print } = require('graphql');
|
|
53
|
-
const { gql } = require('graphql-tag');
|
|
54
|
-
```
|
|
55
|
-
|
|
56
|
-
- **`mergeQueries()`**: Initializes a new query merger instance.
|
|
57
|
-
- **`push(query, variables?)`**: Adds a query to the merger. Optionally accepts a variables object.
|
|
58
|
-
- **`print()`**: Converts the resulting query to a printable string format.
|
|
59
|
-
|
|
60
|
-
### Combining Queries
|
|
61
|
-
|
|
62
|
-
#### Basic Query Combination
|
|
63
|
-
|
|
64
|
-
You can combine queries written as strings or `gql`-tagged templates.
|
|
65
|
-
|
|
66
|
-
```javascript
|
|
67
|
-
const query_1 = /* GraphQL */ `
|
|
68
|
-
{
|
|
69
|
-
user {
|
|
70
|
-
id
|
|
71
|
-
name
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
`;
|
|
75
|
-
|
|
76
|
-
const query_2 = /* GraphQL */ gql`
|
|
77
|
-
{
|
|
78
|
-
post {
|
|
79
|
-
id
|
|
80
|
-
title
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
`;
|
|
84
|
-
|
|
85
|
-
const combined = mergeQueries().push(query_1).push(query_2);
|
|
86
|
-
|
|
87
|
-
console.log(print(combined.query));
|
|
88
|
-
```
|
|
89
|
-
|
|
90
|
-
**Output**:
|
|
91
|
-
|
|
92
|
-
```graphql
|
|
93
|
-
{
|
|
94
|
-
user {
|
|
95
|
-
id
|
|
96
|
-
name
|
|
97
|
-
}
|
|
98
|
-
post {
|
|
99
|
-
id
|
|
100
|
-
title
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
```
|
|
104
|
-
|
|
105
|
-
#### Combining Queries with Variables
|
|
106
|
-
|
|
107
|
-
When combining queries that include variables, the library automatically renames conflicting variable names to maintain uniqueness.
|
|
108
|
-
|
|
109
|
-
```javascript
|
|
110
|
-
const query_1 = /* GraphQL */ gql`
|
|
111
|
-
query ($id: ID!) {
|
|
112
|
-
user(id: $id) {
|
|
113
|
-
id
|
|
114
|
-
name
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
`;
|
|
118
|
-
|
|
119
|
-
const query_2 = /* GraphQL */ `
|
|
120
|
-
query ($id: ID!) {
|
|
121
|
-
post(id: $id) {
|
|
122
|
-
id
|
|
123
|
-
title
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
`;
|
|
127
|
-
|
|
128
|
-
const combined = mergeQueries().push(query_1, { id: 1 }).push(query_2, { id: 2 });
|
|
129
|
-
|
|
130
|
-
console.log(print(combined.query));
|
|
131
|
-
console.log(combined.variables);
|
|
132
|
-
```
|
|
133
|
-
|
|
134
|
-
**Output**:
|
|
135
|
-
|
|
136
|
-
```graphql
|
|
137
|
-
query ($id_1: ID!, $id_2: ID!) {
|
|
138
|
-
user(id: $id_1) {
|
|
139
|
-
id
|
|
140
|
-
name
|
|
141
|
-
}
|
|
142
|
-
post(id: $id_2) {
|
|
143
|
-
id
|
|
144
|
-
title
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
```
|
|
148
|
-
|
|
149
|
-
**Variables**:
|
|
150
|
-
|
|
151
|
-
```json
|
|
152
|
-
{
|
|
153
|
-
"id_1": 1,
|
|
154
|
-
"id_2": 2
|
|
155
|
-
}
|
|
156
|
-
```
|
|
157
|
-
|
|
158
|
-
- **Automatic Variable Renaming**: Conflicting variable names (`id`) are renamed (`id_1`, `id_2`).
|
|
159
|
-
|
|
160
|
-
#### Conditional Query Combination
|
|
161
|
-
|
|
162
|
-
You can dynamically add queries based on runtime conditions.
|
|
163
|
-
|
|
164
|
-
```javascript
|
|
165
|
-
const combined = mergeQueries('optionalQueryName');
|
|
166
|
-
|
|
167
|
-
combined.push(
|
|
168
|
-
/* GraphQL */ `
|
|
169
|
-
query ($email: String!) {
|
|
170
|
-
user(email: $email) {
|
|
171
|
-
id
|
|
172
|
-
name
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
`,
|
|
176
|
-
{ email: 'example@test.com' },
|
|
177
|
-
);
|
|
178
|
-
|
|
179
|
-
if (true) {
|
|
180
|
-
combined.push(
|
|
181
|
-
/* GraphQL */ `
|
|
182
|
-
query ($id: ID!) {
|
|
183
|
-
post(id: $id) {
|
|
184
|
-
id
|
|
185
|
-
title
|
|
186
|
-
date
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
`,
|
|
190
|
-
{ id: 10 },
|
|
191
|
-
);
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
console.log(print(combined.query));
|
|
195
|
-
console.log(combined.variables);
|
|
196
|
-
```
|
|
197
|
-
|
|
198
|
-
**Output**:
|
|
199
|
-
|
|
200
|
-
```graphql
|
|
201
|
-
query optionalQueryName($email_1: String!, $id_2: ID!) {
|
|
202
|
-
user(email: $email_1) {
|
|
203
|
-
id
|
|
204
|
-
name
|
|
205
|
-
}
|
|
206
|
-
post(id: $id_2) {
|
|
207
|
-
id
|
|
208
|
-
title
|
|
209
|
-
date
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
```
|
|
213
|
-
|
|
214
|
-
**Variables**:
|
|
215
|
-
|
|
216
|
-
```json
|
|
217
|
-
{
|
|
218
|
-
"email_1": "example@test.com",
|
|
219
|
-
"id_2": 10
|
|
220
|
-
}
|
|
221
|
-
```
|
|
222
|
-
|
|
223
|
-
---
|
|
224
|
-
|
|
225
|
-
## Notes
|
|
226
|
-
|
|
227
|
-
1. **Variable Scope**: The library renames variables in the combined query to ensure no conflicts occur between different queries.
|
|
228
|
-
2. **Query Formats**: Queries can be provided as:
|
|
229
|
-
- String literals
|
|
230
|
-
- `gql`-tagged templates (recommended for syntax highlighting and validation).
|
|
231
|
-
3. **Error Handling**: Ensure all variables required by the queries are provided when invoking `push()`. Missing variables will lead to runtime errors.
|
|
1
|
+
# GraphQL Query Merger
|
|
2
|
+
|
|
3
|
+
**GraphQL Query Merger** is a utility library designed to merge multiple GraphQL queries into a single, cohesive query. This helps developers streamline their GraphQL requests by combining related operations efficiently, avoiding redundancy and improving maintainability.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## Table of Contents
|
|
8
|
+
|
|
9
|
+
- [GraphQL Query Merger](#graphql-query-merger)
|
|
10
|
+
- [Table of Contents](#table-of-contents)
|
|
11
|
+
- [Features](#features)
|
|
12
|
+
- [Installation](#installation)
|
|
13
|
+
- [Usage](#usage)
|
|
14
|
+
- [Importing the Library](#importing-the-library)
|
|
15
|
+
- [Combining Queries](#combining-queries)
|
|
16
|
+
- [Basic Query Combination](#basic-query-combination)
|
|
17
|
+
- [Combining Queries with Variables](#combining-queries-with-variables)
|
|
18
|
+
- [Conditional Query Combination](#conditional-query-combination)
|
|
19
|
+
- [Notes](#notes)
|
|
20
|
+
|
|
21
|
+
---
|
|
22
|
+
|
|
23
|
+
## Features
|
|
24
|
+
|
|
25
|
+
- **Merge GraphQL Queries**: Seamlessly combine multiple queries into one.
|
|
26
|
+
- **Variable Deduplication**: Automatically handles conflicts by renaming variables.
|
|
27
|
+
- **Supports Conditional Queries**: Add queries dynamically based on runtime logic.
|
|
28
|
+
- **Customizable Query Names**: Optionally name the resulting query.
|
|
29
|
+
|
|
30
|
+
---
|
|
31
|
+
|
|
32
|
+
## Installation
|
|
33
|
+
|
|
34
|
+
To install the library, use npm or yarn:
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
npm install graphql-query-merger graphql
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
> **Note**: `graphql-query-merger` has a peer dependency on `graphql`. Ensure that the `graphql` package is installed alongside it.
|
|
41
|
+
|
|
42
|
+
---
|
|
43
|
+
|
|
44
|
+
## Usage
|
|
45
|
+
|
|
46
|
+
### Importing the Library
|
|
47
|
+
|
|
48
|
+
To use the library, import the necessary functions:
|
|
49
|
+
|
|
50
|
+
```javascript
|
|
51
|
+
const { mergeQueries } = require('graphql-query-merger');
|
|
52
|
+
const { print } = require('graphql');
|
|
53
|
+
const { gql } = require('graphql-tag');
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
- **`mergeQueries()`**: Initializes a new query merger instance.
|
|
57
|
+
- **`push(query, variables?)`**: Adds a query to the merger. Optionally accepts a variables object.
|
|
58
|
+
- **`print()`**: Converts the resulting query to a printable string format.
|
|
59
|
+
|
|
60
|
+
### Combining Queries
|
|
61
|
+
|
|
62
|
+
#### Basic Query Combination
|
|
63
|
+
|
|
64
|
+
You can combine queries written as strings or `gql`-tagged templates.
|
|
65
|
+
|
|
66
|
+
```javascript
|
|
67
|
+
const query_1 = /* GraphQL */ `
|
|
68
|
+
{
|
|
69
|
+
user {
|
|
70
|
+
id
|
|
71
|
+
name
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
`;
|
|
75
|
+
|
|
76
|
+
const query_2 = /* GraphQL */ gql`
|
|
77
|
+
{
|
|
78
|
+
post {
|
|
79
|
+
id
|
|
80
|
+
title
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
`;
|
|
84
|
+
|
|
85
|
+
const combined = mergeQueries().push(query_1).push(query_2);
|
|
86
|
+
|
|
87
|
+
console.log(print(combined.query));
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
**Output**:
|
|
91
|
+
|
|
92
|
+
```graphql
|
|
93
|
+
{
|
|
94
|
+
user {
|
|
95
|
+
id
|
|
96
|
+
name
|
|
97
|
+
}
|
|
98
|
+
post {
|
|
99
|
+
id
|
|
100
|
+
title
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
#### Combining Queries with Variables
|
|
106
|
+
|
|
107
|
+
When combining queries that include variables, the library automatically renames conflicting variable names to maintain uniqueness.
|
|
108
|
+
|
|
109
|
+
```javascript
|
|
110
|
+
const query_1 = /* GraphQL */ gql`
|
|
111
|
+
query ($id: ID!) {
|
|
112
|
+
user(id: $id) {
|
|
113
|
+
id
|
|
114
|
+
name
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
`;
|
|
118
|
+
|
|
119
|
+
const query_2 = /* GraphQL */ `
|
|
120
|
+
query ($id: ID!) {
|
|
121
|
+
post(id: $id) {
|
|
122
|
+
id
|
|
123
|
+
title
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
`;
|
|
127
|
+
|
|
128
|
+
const combined = mergeQueries().push(query_1, { id: 1 }).push(query_2, { id: 2 });
|
|
129
|
+
|
|
130
|
+
console.log(print(combined.query));
|
|
131
|
+
console.log(combined.variables);
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
**Output**:
|
|
135
|
+
|
|
136
|
+
```graphql
|
|
137
|
+
query ($id_1: ID!, $id_2: ID!) {
|
|
138
|
+
user(id: $id_1) {
|
|
139
|
+
id
|
|
140
|
+
name
|
|
141
|
+
}
|
|
142
|
+
post(id: $id_2) {
|
|
143
|
+
id
|
|
144
|
+
title
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
**Variables**:
|
|
150
|
+
|
|
151
|
+
```json
|
|
152
|
+
{
|
|
153
|
+
"id_1": 1,
|
|
154
|
+
"id_2": 2
|
|
155
|
+
}
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
- **Automatic Variable Renaming**: Conflicting variable names (`id`) are renamed (`id_1`, `id_2`).
|
|
159
|
+
|
|
160
|
+
#### Conditional Query Combination
|
|
161
|
+
|
|
162
|
+
You can dynamically add queries based on runtime conditions.
|
|
163
|
+
|
|
164
|
+
```javascript
|
|
165
|
+
const combined = mergeQueries('optionalQueryName');
|
|
166
|
+
|
|
167
|
+
combined.push(
|
|
168
|
+
/* GraphQL */ `
|
|
169
|
+
query ($email: String!) {
|
|
170
|
+
user(email: $email) {
|
|
171
|
+
id
|
|
172
|
+
name
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
`,
|
|
176
|
+
{ email: 'example@test.com' },
|
|
177
|
+
);
|
|
178
|
+
|
|
179
|
+
if (true) {
|
|
180
|
+
combined.push(
|
|
181
|
+
/* GraphQL */ `
|
|
182
|
+
query ($id: ID!) {
|
|
183
|
+
post(id: $id) {
|
|
184
|
+
id
|
|
185
|
+
title
|
|
186
|
+
date
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
`,
|
|
190
|
+
{ id: 10 },
|
|
191
|
+
);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
console.log(print(combined.query));
|
|
195
|
+
console.log(combined.variables);
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
**Output**:
|
|
199
|
+
|
|
200
|
+
```graphql
|
|
201
|
+
query optionalQueryName($email_1: String!, $id_2: ID!) {
|
|
202
|
+
user(email: $email_1) {
|
|
203
|
+
id
|
|
204
|
+
name
|
|
205
|
+
}
|
|
206
|
+
post(id: $id_2) {
|
|
207
|
+
id
|
|
208
|
+
title
|
|
209
|
+
date
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
**Variables**:
|
|
215
|
+
|
|
216
|
+
```json
|
|
217
|
+
{
|
|
218
|
+
"email_1": "example@test.com",
|
|
219
|
+
"id_2": 10
|
|
220
|
+
}
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
---
|
|
224
|
+
|
|
225
|
+
## Notes
|
|
226
|
+
|
|
227
|
+
1. **Variable Scope**: The library renames variables in the combined query to ensure no conflicts occur between different queries.
|
|
228
|
+
2. **Query Formats**: Queries can be provided as:
|
|
229
|
+
- String literals
|
|
230
|
+
- `gql`-tagged templates (recommended for syntax highlighting and validation).
|
|
231
|
+
3. **Error Handling**: Ensure all variables required by the queries are provided when invoking `push()`. Missing variables will lead to runtime errors.
|
package/dist/index.es.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { parse as p, Kind as t, visit as m } from "graphql";
|
|
2
|
-
const
|
|
1
|
+
import { parse as p, Kind as t, print as d, visit as m } from "graphql";
|
|
2
|
+
const E = (c, a, s) => {
|
|
3
3
|
const o = {}, l = {}, r = m(c, {
|
|
4
4
|
[t.VARIABLE_DEFINITION](e) {
|
|
5
5
|
const n = e.variable.name.value, i = `${n}_${s}`;
|
|
@@ -50,17 +50,19 @@ const d = (c, a, s) => {
|
|
|
50
50
|
}
|
|
51
51
|
return { document: r, variables: u };
|
|
52
52
|
};
|
|
53
|
-
class
|
|
53
|
+
class h {
|
|
54
54
|
constructor(a) {
|
|
55
55
|
this.operationName = a, this.documents = [], this.internalVariables = [], this.uniqueIdCounter = 1, this.operationType = null;
|
|
56
56
|
}
|
|
57
57
|
push(a, s = {}) {
|
|
58
58
|
typeof a == "string" && (a = p(a));
|
|
59
|
-
const o = this.uniqueIdCounter++, { document: l, variables: r } =
|
|
59
|
+
const o = this.uniqueIdCounter++, { document: l, variables: r } = E(a, s, o), u = l.definitions.filter(
|
|
60
60
|
(n) => n.kind === t.OPERATION_DEFINITION
|
|
61
61
|
);
|
|
62
62
|
if (u.length === 0)
|
|
63
|
-
throw new Error(
|
|
63
|
+
throw new Error(
|
|
64
|
+
"No operation definition found in the provided document."
|
|
65
|
+
);
|
|
64
66
|
const e = u[0].operation;
|
|
65
67
|
if (this.operationType && this.operationType !== e)
|
|
66
68
|
throw new Error(
|
|
@@ -72,7 +74,9 @@ class E {
|
|
|
72
74
|
const a = [], s = [], o = [];
|
|
73
75
|
for (const l of this.documents)
|
|
74
76
|
for (const r of l.definitions)
|
|
75
|
-
r.kind === t.OPERATION_DEFINITION ? (a.push(
|
|
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);
|
|
76
80
|
return {
|
|
77
81
|
kind: t.DOCUMENT,
|
|
78
82
|
definitions: [
|
|
@@ -90,11 +94,14 @@ class E {
|
|
|
90
94
|
]
|
|
91
95
|
};
|
|
92
96
|
}
|
|
97
|
+
getQueryString() {
|
|
98
|
+
return d(this.query);
|
|
99
|
+
}
|
|
93
100
|
get variables() {
|
|
94
101
|
return Object.assign({}, ...this.internalVariables);
|
|
95
102
|
}
|
|
96
103
|
}
|
|
97
|
-
const f = (c = "") => new
|
|
104
|
+
const f = (c = "") => new h(c);
|
|
98
105
|
export {
|
|
99
106
|
f as mergeQueries
|
|
100
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,30 +1,30 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "graphql-query-merger",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "Merge and unify multiple GraphQL queries into a single operation with field and variable conflict resolution.",
|
|
5
|
-
"main": "dist/index.umd.js",
|
|
6
|
-
"module": "dist/index.es.js",
|
|
7
|
-
"types": "dist/index.d.ts",
|
|
8
|
-
"files": [
|
|
9
|
-
"dist"
|
|
10
|
-
],
|
|
11
|
-
"scripts": {
|
|
12
|
-
"build": "vite build",
|
|
13
|
-
"test": "jest"
|
|
14
|
-
},
|
|
15
|
-
"keywords": [
|
|
16
|
-
"graphql",
|
|
17
|
-
"query",
|
|
18
|
-
"merge"
|
|
19
|
-
],
|
|
20
|
-
"author": "A. Juárez",
|
|
21
|
-
"license": "ISC",
|
|
22
|
-
"devDependencies": {
|
|
23
|
-
"@babel/preset-env": "^7.26.0",
|
|
24
|
-
"babel-jest": "^29.7.0",
|
|
25
|
-
"graphql": "^16.9.0",
|
|
26
|
-
"graphql-tag": "^2.12.6",
|
|
27
|
-
"jest": "^29.7.0",
|
|
28
|
-
"vite": "^5.4.11"
|
|
29
|
-
}
|
|
30
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "graphql-query-merger",
|
|
3
|
+
"version": "1.1.1",
|
|
4
|
+
"description": "Merge and unify multiple GraphQL queries into a single operation with field and variable conflict resolution.",
|
|
5
|
+
"main": "dist/index.umd.js",
|
|
6
|
+
"module": "dist/index.es.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist"
|
|
10
|
+
],
|
|
11
|
+
"scripts": {
|
|
12
|
+
"build": "vite build",
|
|
13
|
+
"test": "jest"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"graphql",
|
|
17
|
+
"query",
|
|
18
|
+
"merge"
|
|
19
|
+
],
|
|
20
|
+
"author": "A. Juárez",
|
|
21
|
+
"license": "ISC",
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@babel/preset-env": "^7.26.0",
|
|
24
|
+
"babel-jest": "^29.7.0",
|
|
25
|
+
"graphql": "^16.9.0",
|
|
26
|
+
"graphql-tag": "^2.12.6",
|
|
27
|
+
"jest": "^29.7.0",
|
|
28
|
+
"vite": "^5.4.11"
|
|
29
|
+
}
|
|
30
|
+
}
|