Skip to content

Commit 524abe5

Browse files
sebas5384leoasis
authored andcommitted
Support multiple operations per file (#7)
* Added multiple operations support and upgrading graphql-tag to >= 2.8.0. * Added graphql-tag as peer dependencies for version requirement.
1 parent 64ef301 commit 524abe5

File tree

6 files changed

+38
-6
lines changed

6 files changed

+38
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ To solve this problem, this loader works after the [graphql-tag loader](https://
1212

1313
## Installation and setup
1414

15-
You need to have the [graphql-tag](https://github.com/apollographql/graphql-tag) package installed.
15+
You need to have the [graphql-tag](https://github.com/apollographql/graphql-tag) *(>= v2.8.0)* package installed.
1616

1717
First install this package
1818

example/app.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@ el.innerHTML = `
77
<h1>Document id: ${query.documentId}</h1>
88
<pre>${JSON.stringify(query, null, 4)}</pre>
99
<hr />
10-
<h1>Document id: ${queryWithDeps.documentId}</h1>
11-
<pre>${JSON.stringify(queryWithDeps, null, 4)}</pre>
10+
<h1>Document id: ${queryWithDeps.SomeQuery.documentId}</h1>
11+
<pre>${JSON.stringify(queryWithDeps.SomeQuery, null, 4)}</pre>
12+
<hr />
13+
<h1>Document id: ${queryWithDeps.SomeMutation.documentId}</h1>
14+
<pre>${JSON.stringify(queryWithDeps.SomeMutation, null, 4)}</pre>
1215
`;
1316

1417
document.body.appendChild(el);

example/query-with-deps.graphql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,11 @@ query SomeQuery {
77
...Dep
88
}
99
}
10+
11+
mutation SomeMutation {
12+
foo {
13+
bar
14+
baz
15+
...Dep
16+
}
17+
}

index.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,21 @@ function tryAddDocumentId(options, content, querySource) {
8080

8181
const queries = Object.keys(queryMap);
8282
if (queries.length > 1) {
83-
throw new Error('Only one operation per file is allowed');
83+
queries
84+
.map(query => {
85+
const matched = query.match(/^(mutation|query)\ ([^\ \(\{]*)/)
86+
if (!matched) {
87+
return false
88+
}
89+
return {
90+
operationName: matched[2],
91+
id: generateIdForQuery(options, query)
92+
}
93+
})
94+
.filter(isValid => !!isValid)
95+
.forEach(({ id, operationName }) => {
96+
content += `${os.EOL}module.exports["${operationName}"].documentId = ${JSON.stringify(id)};`
97+
})
8498
} else if (queries.length === 1) {
8599
const queryId = generateIdForQuery(options, Object.keys(queryMap)[0]);
86100
content += `${os.EOL}doc.documentId = ${JSON.stringify(queryId)}`;

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,15 @@
1010
"start": "webpack-dev-server --config ./example/webpack.config.js"
1111
},
1212
"devDependencies": {
13-
"graphql-tag": "^2.5.0",
13+
"graphql-tag": "^2.9.2",
1414
"webpack": "^3.8.1",
1515
"webpack-dev-server": "^2.9.5"
1616
},
1717
"dependencies": {
1818
"loader-utils": "^1.1.0",
1919
"persistgraphql": "^0.3.11"
20+
},
21+
"peerDependencies": {
22+
"graphql-tag": "^2.8.0"
2023
}
2124
}

yarn.lock

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1156,10 +1156,14 @@ graphql-anywhere@^3.0.1:
11561156
version "3.1.0"
11571157
resolved "https://registry.yarnpkg.com/graphql-anywhere/-/graphql-anywhere-3.1.0.tgz#3ea0d8e8646b5cee68035016a9a7557c15c21e96"
11581158

1159-
graphql-tag@^2.0.0, graphql-tag@^2.4.2, graphql-tag@^2.5.0:
1159+
graphql-tag@^2.0.0, graphql-tag@^2.4.2:
11601160
version "2.5.0"
11611161
resolved "https://registry.yarnpkg.com/graphql-tag/-/graphql-tag-2.5.0.tgz#b43bfd8b5babcd2c205ad680c03e98b238934e0f"
11621162

1163+
graphql-tag@^2.9.2:
1164+
version "2.9.2"
1165+
resolved "https://registry.yarnpkg.com/graphql-tag/-/graphql-tag-2.9.2.tgz#2f60a5a981375f430bf1e6e95992427dc18af686"
1166+
11631167
"graphql@>=0.9.4 <0.11", graphql@^0.10.0, graphql@^0.10.3:
11641168
version "0.10.5"
11651169
resolved "https://registry.yarnpkg.com/graphql/-/graphql-0.10.5.tgz#c9be17ca2bdfdbd134077ffd9bbaa48b8becd298"

0 commit comments

Comments
 (0)