Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow fragments to be imported by name when using the webpack loader #257

Merged
merged 3 commits into from
Apr 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Change log

### v2.12.3 (TBD)

* Allow fragments to be imported by name when using the webpack loader. <br/>
[@dobesv](https://github.com/dobesv) in [#257](https://github.com/apollographql/graphql-tag/pull/257)

### v2.12.2

* Avoid using `Object.assign` to attach extra properties to `gql`. <br/>
Expand Down
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,31 @@ For **create-react-app** < v2, you'll either need to eject or use [react-app-rew

Testing environments that don't support Webpack require additional configuration. For [Jest](https://facebook.github.io/jest/) use [jest-transform-graphql](https://github.com/remind101/jest-transform-graphql).

#### Support for fragments

With the webpack loader, you can import fragments by name:

In a file called `query.gql`:

```graphql
fragment MyFragment1 on MyType1 {
...
}

fragment MyFragment2 on MyType2 {
...
}
```

And in your JavaScript:

```javascript
import { MyFragment1, MyFragment2 } from 'query.gql'
```

Note: If your fragment references other fragments, the resulting document will
have multiple fragments in it. In this case you must still specify the fragment name when using the fragment. For example, with `@apollo/client` you would specify the `fragmentName` option when using the fragment for cache operations.

### Warnings

This package will emit a warning if you have multiple fragments of the same name. You can disable this with:
Expand Down
6 changes: 3 additions & 3 deletions loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ module.exports = function(source) {
// at compile time, and then uses those at load time to create minimal query documents
// We cannot do the latter at compile time due to how the #import code works.
let operationCount = doc.definitions.reduce(function(accum, op) {
if (op.kind === "OperationDefinition") {
if (op.kind === "OperationDefinition" || op.kind === "FragmentDefinition") {
return accum + 1;
}

Expand Down Expand Up @@ -161,12 +161,12 @@ module.exports = function(source) {

return newDoc;
}

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can imagine, that this line produces a mental dissonance on the apollo team. They don't know exactly why they can't merge this, they just know that they cannot merge it.

@dobesv removing this line will make wonders

module.exports = doc;
`

for (const op of doc.definitions) {
if (op.kind === "OperationDefinition") {
if (op.kind === "OperationDefinition" || op.kind === "FragmentDefinition") {
if (!op.name) {
if (operationCount > 1) {
throw "Query/mutation names are required for a document with multiple definitions";
Expand Down
31 changes: 28 additions & 3 deletions src/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,18 @@ describe('gql', () => {
assert.equal(Q3[1].name.value, 'F1');
assert.equal(Q3[2].name.value, 'F2');

});
const F1 = module.exports.F1.definitions;
const F2 = module.exports.F2.definitions;
const F3 = module.exports.F3.definitions;

assert.equal(F1.length, 1);
assert.equal(F1[0].name.value, 'F1');
assert.equal(F2.length, 1);
assert.equal(F2[0].name.value, 'F2');
assert.equal(F3.length, 1);
assert.equal(F3[0].name.value, 'F3');

});

it('tracks fragment dependencies across nested fragments', () => {
const jsSource = loader.call({ cacheable() {} }, `
Expand Down Expand Up @@ -183,8 +194,22 @@ describe('gql', () => {
assert.equal(Q1[2].name.value, 'F22');
assert.equal(Q1[3].name.value, 'F11');

assert.equal(Q2.length, 1);
});
assert.equal(Q2.length, 1);

const F11 = module.exports.F11.definitions;
const F22 = module.exports.F22.definitions;
const F33 = module.exports.F33.definitions;

assert.equal(F11.length, 1);
assert.equal(F11[0].name.value, 'F11');
assert.equal(F22.length, 2);
assert.equal(F22[0].name.value, 'F22');
assert.equal(F22[1].name.value, 'F11');
assert.equal(F33.length, 3);
assert.equal(F33[0].name.value, 'F33');
assert.equal(F33[1].name.value, 'F22');
assert.equal(F33[2].name.value, 'F11');
});

it('correctly imports other files through the webpack loader', () => {
const query = `#import "./fragment_definition.graphql"
Expand Down