Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: apollographql/graphql-tag
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v2.12.3
Choose a base ref
...
head repository: apollographql/graphql-tag
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v2.12.4
Choose a head ref
  • 4 commits
  • 6 files changed
  • 3 contributors

Commits on Mar 22, 2021

  1. Copy the full SHA
    60cf313 View commit details
  2. Copy the full SHA
    2b99a7c View commit details

Commits on Apr 29, 2021

  1. Allow fragments to be imported by name when using the webpack loader (#…

    …257)
    
    * 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.
    
    * Slight README tweaks
    
    * Changelog update
    
    Co-authored-by: hwillson <hugh@octonary.com>
    dobesv and hwillson authored Apr 29, 2021
    Copy the full SHA
    81cc596 View commit details
  2. Copy the full SHA
    d401fcf View commit details
Showing with 68 additions and 13 deletions.
  1. +5 −0 CHANGELOG.md
  2. +25 −0 README.md
  3. +3 −3 loader.js
  4. +6 −6 package-lock.json
  5. +1 −1 package.json
  6. +28 −3 src/tests.ts
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.4 (2021-04-29)

* 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/>
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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:
6 changes: 3 additions & 3 deletions loader.js
Original file line number Diff line number Diff line change
@@ -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;
}

@@ -161,12 +161,12 @@ module.exports = function(source) {
return newDoc;
}
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";
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "graphql-tag",
"version": "2.12.3",
"version": "2.12.4",
"description": "A JavaScript template literal tag that parses GraphQL queries",
"main": "./main.js",
"module": "./lib/index.js",
31 changes: 28 additions & 3 deletions src/tests.ts
Original file line number Diff line number Diff line change
@@ -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() {} }, `
@@ -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"