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

Add default export from graphql-tag/loader modules #281

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,12 @@ console.log(query);

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 multiple operations
#### Support for multiple operations per file

With the webpack loader, you can also import operations by name:
With the webpack loader, the default export from a graphql file contains all the operations found in
the original file. Typically a GraphQL client library expects to find only one operation, so if
you want to put multiple operations in the same file, you should import individual operations
by name:

In a file called `query.gql`:
```graphql
Expand All @@ -136,7 +139,7 @@ And in your JavaScript:
```javascript
import { MyQuery1, MyQuery2 } from 'query.gql'
```

### Warnings

This package will emit a warning if you have multiple fragments of the same name. You can disable this with:
Expand Down
4 changes: 2 additions & 2 deletions loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ module.exports = function(source) {

return newDoc;
}

module.exports = doc;
module.exports = Object.assign({default: doc}, doc);
`

for (const op of doc.definitions) {
Expand Down
6 changes: 5 additions & 1 deletion test/graphql.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ const assert = require('chai').assert;
eval(jsSource);

assert.deepEqual(module.exports.definitions, module.exports.Q1.definitions);
assert.deepEqual(module.exports.default.definitions, module.exports.Q1.definitions);
});

it('parses multiple queries through webpack loader', () => {
Expand All @@ -81,6 +82,9 @@ const assert = require('chai').assert;
assert.equal(module.exports.Q2.kind, 'Document');
assert.equal(module.exports.Q1.definitions.length, 1);
assert.equal(module.exports.Q2.definitions.length, 1);
assert.equal(module.exports.default.definitions.length, 2);
assert.equal(module.exports.default.definitions[0], module.exports.Q1.definitions[0]);
assert.equal(module.exports.default.definitions[1], module.exports.Q2.definitions[0]);
});

it('parses fragments with variable definitions', () => {
Expand All @@ -92,7 +96,7 @@ const assert = require('chai').assert;

gql.disableExperimentalFragmentVariables()
});

// see https://github.com/apollographql/graphql-tag/issues/168
it('does not nest queries needlessly in named exports', () => {
const jsSource = loader.call({ cacheable() {} }, `
Expand Down