Skip to content

Commit

Permalink
🤌 Prefix is now overridable
Browse files Browse the repository at this point in the history
  • Loading branch information
heiso committed Jul 23, 2021
1 parent ec1d2de commit 5c1aa22
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
## Example

codegen.yml

```yaml
schema: ['./schema.graphql']

Expand All @@ -20,9 +21,12 @@ generated/tests.ts:
- typescript
- typescript-operations
- graphql-codegen-typescript-operations-tester
config:
prefix: test
```
schema.graphql
```graphql
type Author {
firstname: String
Expand All @@ -41,6 +45,7 @@ type Query {
```

documents.graphql

```graphql
query getBooks($var1: String!) {
books(var1: $var1) {
Expand All @@ -55,6 +60,7 @@ query getBooks($var1: String!) {
```

test.spec.ts

```typescript
import { testGetBooksQuery } from './generated/tests.ts'
import { schema } from 'path/to/my/schema'
Expand Down
10 changes: 8 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,15 @@ function getOperationFragments(
return fragments
}

export const plugin: PluginFunction = (schema, documents) => {
type Config = {
prefix?: string
}

export const plugin: PluginFunction<Config> = (schema, documents, config) => {
const imports = [`import { request, Args } from 'graphql-codegen-typescript-operations-tester'`]

const prefix = config.prefix || 'test'

const allAst = concatAST(
documents.reduce<DocumentNode[]>((acc, source) => {
if (source.document) {
Expand Down Expand Up @@ -73,7 +79,7 @@ export const plugin: PluginFunction = (schema, documents) => {
lines.push(`export const ${name}Source: string = \``)
lines.push(`${fragmentsStr}${print(node)}\`;`)
lines.push(``)
lines.push(`export function test${name}(`)
lines.push(`export function ${prefix}${name}(`)
lines.push(` graphqlArgs: Args,`)
lines.push(` variables?: ${name}Variables`)
lines.push(`) {`)
Expand Down
2 changes: 2 additions & 0 deletions test/int/codegen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ generates:
- typescript
- typescript-operations
- ../../dist/index.js
config:
prefix: test

0 comments on commit 5c1aa22

Please sign in to comment.