Skip to content

Commit e70aa9c

Browse files
committed
Allow fragments to be imported by name when using the webpack loader
Any packages that use graphql-tag/loader under the hood will also benefit.
1 parent 50a850e commit e70aa9c

File tree

3 files changed

+56
-5
lines changed

3 files changed

+56
-5
lines changed

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,32 @@ And in your JavaScript:
137137
import { MyQuery1, MyQuery2 } from 'query.gql'
138138
```
139139

140+
#### Support for fragments
141+
142+
With the webpack loader, you can import fragments by name:
143+
144+
In a file called `query.gql`:
145+
```graphql
146+
fragment MyFragment1 on MyType1 {
147+
...
148+
}
149+
150+
fragment MyFragment2 on MyType2 {
151+
...
152+
}
153+
```
154+
155+
And in your JavaScript:
156+
157+
```javascript
158+
import { MyFragment1, MyFragment2 } from 'query.gql'
159+
```
160+
161+
Note: If your fragment references other fragments, the resulting document will
162+
have multiple fragments in it. In this case you must still specify the fragment
163+
name when using the fragment. For example, with `apollo-client` you would specify
164+
the `fragmentName` option when using the fragment for cache operations.
165+
140166
### Warnings
141167

142168
This package will emit a warning if you have multiple fragments of the same name. You can disable this with:

loader.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ module.exports = function(source) {
5353
// at compile time, and then uses those at load time to create minimal query documents
5454
// We cannot do the latter at compile time due to how the #import code works.
5555
let operationCount = doc.definitions.reduce(function(accum, op) {
56-
if (op.kind === "OperationDefinition") {
56+
if (op.kind === "OperationDefinition" || op.kind === "FragmentDefinition") {
5757
return accum + 1;
5858
}
5959

@@ -161,12 +161,12 @@ module.exports = function(source) {
161161
162162
return newDoc;
163163
}
164-
165-
module.exports = doc;
164+
165+
module.exports = Object.assign({default: doc}, doc);
166166
`
167167

168168
for (const op of doc.definitions) {
169-
if (op.kind === "OperationDefinition") {
169+
if (op.kind === "OperationDefinition" || op.kind === "FragmentDefinition") {
170170
if (!op.name) {
171171
if (operationCount > 1) {
172172
throw "Query/mutation names are required for a document with multiple definitions";

test/graphql.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ const assert = require('chai').assert;
9292

9393
gql.disableExperimentalFragmentVariables()
9494
});
95-
95+
9696
// see https://github.com/apollographql/graphql-tag/issues/168
9797
it('does not nest queries needlessly in named exports', () => {
9898
const jsSource = loader.call({ cacheable() {} }, `
@@ -143,6 +143,17 @@ const assert = require('chai').assert;
143143
assert.equal(Q3[1].name.value, 'F1');
144144
assert.equal(Q3[2].name.value, 'F2');
145145

146+
const F1 = module.exports.F1.definitions;
147+
const F2 = module.exports.F2.definitions;
148+
const F3 = module.exports.F3.definitions;
149+
150+
assert.equal(F1.length, 1);
151+
assert.equal(F1[0].name.value, 'F1');
152+
assert.equal(F2.length, 1);
153+
assert.equal(F2[0].name.value, 'F2');
154+
assert.equal(F3.length, 1);
155+
assert.equal(F3[0].name.value, 'F3');
156+
146157
});
147158

148159
it('tracks fragment dependencies across nested fragments', () => {
@@ -182,6 +193,20 @@ const assert = require('chai').assert;
182193
assert.equal(Q1[3].name.value, 'F11');
183194

184195
assert.equal(Q2.length, 1);
196+
197+
const F11 = module.exports.F11.definitions;
198+
const F22 = module.exports.F22.definitions;
199+
const F33 = module.exports.F33.definitions;
200+
201+
assert.equal(F11.length, 1);
202+
assert.equal(F11[0].name.value, 'F11');
203+
assert.equal(F22.length, 2);
204+
assert.equal(F22[0].name.value, 'F22');
205+
assert.equal(F22[1].name.value, 'F11');
206+
assert.equal(F33.length, 3);
207+
assert.equal(F33[0].name.value, 'F33');
208+
assert.equal(F33[1].name.value, 'F22');
209+
assert.equal(F33[2].name.value, 'F11');
185210
});
186211

187212
it('correctly imports other files through the webpack loader', () => {

0 commit comments

Comments
 (0)