From 5c1aa22cef87de191a043bd2db61ffb0bf209556 Mon Sep 17 00:00:00 2001 From: Alexandre Moatty Date: Fri, 23 Jul 2021 11:31:19 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=A4=8C=20Prefix=20is=20now=20overridable?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 6 ++++++ src/index.ts | 10 ++++++++-- test/int/codegen.yml | 2 ++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4ffde81..5dc8fce 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ ## Example codegen.yml + ```yaml schema: ['./schema.graphql'] @@ -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 @@ -41,6 +45,7 @@ type Query { ``` documents.graphql + ```graphql query getBooks($var1: String!) { books(var1: $var1) { @@ -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' diff --git a/src/index.ts b/src/index.ts index c5f1eb8..32303b4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -36,9 +36,15 @@ function getOperationFragments( return fragments } -export const plugin: PluginFunction = (schema, documents) => { +type Config = { + prefix?: string +} + +export const plugin: PluginFunction = (schema, documents, config) => { const imports = [`import { request, Args } from 'graphql-codegen-typescript-operations-tester'`] + const prefix = config.prefix || 'test' + const allAst = concatAST( documents.reduce((acc, source) => { if (source.document) { @@ -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(`) {`) diff --git a/test/int/codegen.yml b/test/int/codegen.yml index f7a0a48..0aa084c 100644 --- a/test/int/codegen.yml +++ b/test/int/codegen.yml @@ -7,3 +7,5 @@ generates: - typescript - typescript-operations - ../../dist/index.js + config: + prefix: test