Skip to content

Commit

Permalink
Add support for transformation options
Browse files Browse the repository at this point in the history
  • Loading branch information
DAB0mB committed Jan 12, 2019
1 parent 8037ad1 commit 13fe2a7
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 6 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@ Template literals leaded by magic comments will also be extracted :-)
`
```

Transformation options may also be provided:

- **`defaultGqlIdentifierName`** - The default GraphQL string parser identifier to look for. Defaults to `gql`, unless imported as something else from the `graphql-tag` package. This behavior can also be changed, see the `gqlPackName` option.

- **`gqlMagicComment`** - The magic comment anchor to look for when parsing GraphQL strings. Defaults to `graphql`, which may be translated into `/* GraphQL */` in code.

- **`gqlPackName`** - The name of the package that is responsible for exporting the GraphQL string parser function. Defaults to `graphql-tag`.

I recommend you to look at the [source code](src/visitor.js) for a clearer understanding of the transformation options.

supported file extensions are: `.js`, `.jsx`, `.ts`, `.tsx`, `.flow`, `.flow.js`, `.flow.jsx`, `.graphqls`, `.graphql`, `.gqls`, `.gql`.

### License
Expand Down
5 changes: 4 additions & 1 deletion src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ export default function Config(code, options) {
default: plugins.push(...dynamicFlowPlugins); break
}

Object.assign(this, {
// The _options filed will be used to retrieve the original options.
// Useful when we wanna get not config related options later on
return Object.assign(this, {
_options: options,
sourceType: 'module',
plugins,
})
Expand Down
112 changes: 112 additions & 0 deletions src/graphql-tag-pluck.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -723,4 +723,116 @@ describe('graphql-tag-pluck', () => {
}
`))
})

it('should be able to specify the default GraphQL identifier name', async () => {
const file = await tmp.file({
unsafeCleanup: true,
template: '/tmp/tmp-XXXXXX.js',
})

await fs.writeFile(file.name, freeText(`
const fragment = graphql(\`
fragment Foo on FooType {
id
}
\`)
const doc = graphql\`
query foo {
foo {
...Foo
}
}
\${fragment}
\`
`))

const gqlString = await gqlPluck.fromFile(file.name, {
defaultGqlIdentifierName: 'graphql'
})

expect(gqlString).toEqual(freeText(`
fragment Foo on FooType {
id
}
query foo {
foo {
...Foo
}
}
`))
})

it('should be able to specify the GraphQL magic comment to look for', async () => {
const file = await tmp.file({
unsafeCleanup: true,
template: '/tmp/tmp-XXXXXX.js',
})

await fs.writeFile(file.name, freeText(`
const Message = /* GQL */ \`
enum MessageTypes {
text
media
draftjs
}\`
`))

const gqlString = await gqlPluck.fromFile(file.name, {
gqlMagicComment: 'GQL'
})

expect(gqlString).toEqual(freeText(`
enum MessageTypes {
text
media
draftjs
}
`))
})

it('should be able to specify the package name of which the GraphQL identifier should be imported from', async () => {
const file = await tmp.file({
unsafeCleanup: true,
template: '/tmp/tmp-XXXXXX.js',
})

await fs.writeFile(file.name, freeText(`
import mygql from 'my-graphql-tag'
const fragment = mygql(\`
fragment Foo on FooType {
id
}
\`)
const doc = mygql\`
query foo {
foo {
...Foo
}
}
\${fragment}
\`
`))

const gqlString = await gqlPluck.fromFile(file.name, {
gqlPackName: 'my-graphql-tag'
})

expect(gqlString).toEqual(freeText(`
fragment Foo on FooType {
id
}
query foo {
foo {
...Foo
}
}
`))
})
})
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export const gqlPluckFromCodeString = (code, options = {}) => {
const out = {}
const config = new Config(code, options)
const ast = babel.parse(code, config)
const visitor = createVisitor(ast.code, out)
const visitor = createVisitor(ast.code, out, config._options)

babel.traverse(ast, visitor)

Expand Down
22 changes: 18 additions & 4 deletions src/visitor.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
import * as t from '@babel/types'
import { freeText } from './utils'

const defaultGqlIdentifierName = 'gql'
const gqlMagicComment = 'graphql'
const gqlPackName = 'graphql-tag'
const defaults = {
defaultGqlIdentifierName: 'gql',
gqlMagicComment: 'graphql',
gqlPackName: 'graphql-tag',
}

export default (code, out, options = {}) => {
// Apply defaults to options
let {
defaultGqlIdentifierName,
gqlMagicComment,
gqlPackName,
} = { ...defaults, ...options }

// Prevent case related potential errors
defaultGqlIdentifierName = defaultGqlIdentifierName.toLowerCase()
gqlMagicComment = gqlMagicComment.toLowerCase()
gqlPackName = gqlPackName.toLowerCase()

export default (code, out) => {
// Will accumulate all template literals
const gqlTemplateLiterals = []
// By default, we will look for `gql` calls
Expand Down

0 comments on commit 13fe2a7

Please sign in to comment.