Skip to content

Commit

Permalink
Added option to import custom file types (#53)
Browse files Browse the repository at this point in the history
* Added option to import custom file types

* Add section to README documenting defaults and describing usage
  • Loading branch information
DaBs authored and detrohutt committed Mar 3, 2019
1 parent c9b6f4d commit a1b6e46
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ Option | Type | Default | Description
-|-|-|-
`nodePath` | String | value of NODE_PATH environment variable | **Intended for use with [react-app-rewire-inline-import-graphql-ast](https://github.com/detrohutt/react-app-rewire-inline-import-graphql-ast)** -- Used to allow resolution of absolute paths to your `.gql`/`.graphql` files. If you already have your `NODE_PATH` variable set in your environment, you don't need to set this option. **Not** currently respected by `#import` syntax.
`runtime` | Boolean | false | **Enabling this option requires `graphql-tag` to be installed as a peerDependency.** -- Instead of inlining the parsed AST object, which is very large, this option inlines your GraphQL source code along with an import of the `gql` function from `graphql-tag` and parses your GraphQL source code with `gql` at runtime.
`extensions` | Array | [] | Enables loading GraphQL SDL files that have a custom extension, e.g. '.prisma'


## For users of create-react-app

Expand Down
5 changes: 4 additions & 1 deletion plugin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ export default ({ types: t, template }) => ({
exit(curPath, { opts, file }) {
const importPath = curPath.node.source.value
const jsFilename = file.opts.filename
let { extensions = [] } = opts

if (importPath.endsWith('.graphql') || importPath.endsWith('.gql')) {
extensions = [...extensions, '.graphql', '.gql']

if (extensions.some(extension => importPath.endsWith(extension))) {
if (opts.runtime) {
try {
require('graphql-tag')
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/imports/named/extension.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import extension from '../../shared/extension.graph'
3 changes: 3 additions & 0 deletions tests/fixtures/shared/extension.graph
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
query named {
test
}
12 changes: 12 additions & 0 deletions tests/imports.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,18 @@ describe.each([{ runtime: true }, {}])('plugin options = %j', opts => {
})
})

describe('non-standard extension', () => {
test(`import extension from 'extension.graph'`, () => {
const { code } = transformWithPlugin('./fixtures/imports/named/extension.js', {
...opts,
extensions: ['.graph']
})
let _graphqlTag, _graphqlTag2, extension
eval(code)
expect(extension.kind).toBe('Document')
})
})

describe('single fragment document', () => {
test(`import frag from '../../shared/fragments/fragment.graphql'`, () => {
const { code } = transformWithPlugin('./fixtures/imports/fragments/simple.js', opts)
Expand Down

0 comments on commit a1b6e46

Please sign in to comment.