11import { PluginFunction } from '@graphql-codegen/plugin-helpers'
2+ import { pascalCase } from 'change-case-all'
23import {
34 concatAST ,
45 DocumentNode ,
6+ ExecutionResult ,
57 FragmentDefinitionNode ,
8+ graphql ,
9+ GraphQLArgs ,
10+ GraphQLSchema ,
611 OperationDefinitionNode ,
712 print ,
813 visit ,
@@ -32,7 +37,7 @@ function getOperationFragments(
3237}
3338
3439export const plugin : PluginFunction = ( schema , documents ) => {
35- const imports = [ `import { graphql, ExecutionResult } from 'graphql'` ]
40+ const imports = [ `import { request, Args } from 'graphql-codegen-typescript-operations-tester '` ]
3641
3742 const allAst = concatAST (
3843 documents . reduce < DocumentNode [ ] > ( ( acc , source ) => {
@@ -55,28 +60,26 @@ export const plugin: PluginFunction = (schema, documents) => {
5560 if ( ! node . name ) return
5661
5762 const type = node . operation === 'mutation' ? 'Mutation' : 'Query'
58- const name = ` ${ node . name . value [ 0 ] . toUpperCase ( ) } ${ node . name . value . substr (
59- 1 ,
60- node . name . value . length - 1
61- ) } `
63+
64+ // Mimic the default naming convention.
65+ // See https://www.graphql-code-generator.com/docs/getting-started/naming-convention#namingconvention
66+ const name = pascalCase ( ` ${ node . name . value } ${ type } ` )
6267
6368 const fragments = getOperationFragments ( node , allFragments )
6469 const fragmentsStr =
6570 fragments . size > 0 ? `${ Array . from ( fragments . values ( ) ) . map ( print ) } \n` : ''
6671
6772 lines . push ( `` )
68- lines . push ( `export const ${ name } ${ type } Source: string = \`` )
73+ lines . push ( `export const ${ name } Source: string = \`` )
6974 lines . push ( `${ fragmentsStr } ${ print ( node ) } \`;` )
7075 lines . push ( `` )
71- lines . push ( `export function test${ name } ${ type } (` )
72- lines . push ( ` graphqlRequester: (` )
73- lines . push ( ` query: string,` )
74- lines . push ( ` variables: ${ name } ${ type } Variables` )
75- lines . push ( ` ) => Promise<ExecutionResult<${ name } ${ type } >>,` )
76- lines . push ( ` variables: ${ name } ${ type } Variables` )
76+ lines . push ( `export function test${ name } (` )
77+ lines . push ( ` graphqlArgs: Args,` )
78+ lines . push ( ` variables?: ${ name } Variables` )
7779 lines . push ( `) {` )
78- lines . push ( ` const query = ${ name } ${ type } Source` )
79- lines . push ( ` return graphqlRequester(query, variables)` )
80+ lines . push (
81+ ` return request<${ name } >({ ...graphqlArgs, source: ${ name } Source, variableValues: variables })`
82+ )
8083 lines . push ( `};` )
8184 } ,
8285 } )
@@ -88,3 +91,11 @@ export const plugin: PluginFunction = (schema, documents) => {
8891 content : content ,
8992 }
9093}
94+
95+ export type Args = { schema : GraphQLSchema } & Partial < GraphQLArgs >
96+
97+ export async function request < TData extends Record < string , unknown > = Record < string , unknown > > (
98+ graphqlArgs : GraphQLArgs
99+ ) : Promise < ExecutionResult < TData > > {
100+ return graphql ( graphqlArgs ) as Promise < ExecutionResult < TData > >
101+ }
0 commit comments