import { ClientSideBasePluginConfig , ClientSideBaseVisitor , LoadedFragment , } from '@graphql-codegen/visitor-plugin-common' ; import autoBind from 'auto-bind' ; import { camelCase } from 'change-case-all' ; import { GraphQLSchema , OperationDefinitionNode , print } from 'graphql' ; import { RawGraphQLApolloPluginConfig } from './config' ; export interface GraphQLRequestPluginConfig extends ClientSideBasePluginConfig { rawRequest : boolean ; } export class GraphQLApolloVisitor extends ClientSideBaseVisitor < RawGraphQLApolloPluginConfig , GraphQLRequestPluginConfig > { private _operationsToInclude : { node : OperationDefinitionNode ; documentVariableName : string ; operationType : string ; operationResultType : string ; operationVariablesTypes : string ; } [ ] = [ ] ; constructor ( schema , fragments , rawConfig ) { super ( schema , fragments , rawConfig , { } ) ; autoBind ( this ) ; const typeImport = this . config . useTypeImports ? 'import type' : 'import' ; this . _additionalImports . push ( `${ typeImport } { ApolloClient, QueryOptions, SubscriptionOptions, MutationOptions } from '@apollo/client';` ) ; } public OperationDefinition ( node ) { const operationName = node . name ?. value ; if ( ! operationName ) { console . warn ( `Anonymous GraphQL operation was ignored in "typescript-graphql-request", please make sure to name your operation: ` , print ( node ) ) ; return null ; } return super . OperationDefinition ( node ) ; } protected buildOperation ( node , documentVariableName , operationType , operationResultType , operationVariablesTypes ) { this . _operationsToInclude . push ( { node , documentVariableName , operationType , operationResultType , operationVariablesTypes , } ) ; return null ; } public get sdkContent ( ) : string { const sdkOperations = this . _operationsToInclude . map ( x => { const documentVariableName = x . documentVariableName ; const optionType = GraphQLApolloVisitor . getApolloOperationOptionType ( x . operationType ) ; const generics = [ x . operationResultType , x . operationVariablesTypes ] ; const operationName = x . node . name . value ; const genericParameter = x . operationType !== 'Mutation' ? [ ... generics ] . reverse ( ) : [ ... generics ] ; return `${ camelCase ( operationName ) }${ x . operationType }(options: Partial<${ optionType }<${ genericParameter . join ( ', ' ) }>>) {
          return client.${ GraphQLApolloVisitor . getApolloOperation ( x . operationType ) }<${ generics . join ( ', ' ) }>({...options, ${ GraphQLApolloVisitor . getDocumentFieldName ( x . operationType ) }: ${ documentVariableName }})
      }` ; } ) ; return `export const getSdk = (client: ApolloClient<any>) => ({
      ${ sdkOperations . join ( ',\n' ) }
    });
    export type SdkType = ReturnType<typeof getSdk>
` ; } private static getDocumentFieldName ( operationType ) { switch ( operationType ) { case 'Subscription' : return 'query' ; case 'Mutation' : return 'mutation' ; case 'Query' : return 'query' ; default : throw new Error ( 'unknown operation type: ' + operationType ) ; } } private static getApolloOperation ( operationType ) { switch ( operationType ) { case 'Subscription' : return 'subscribe' ; case 'Mutation' : return 'mutate' ; case 'Query' : return 'query' ; default : throw new Error ( 'unknown operation type: ' + operationType ) ; } } private static getApolloOperationOptionType ( operationType ) { switch ( operationType ) { case 'Subscription' : return 'SubscriptionOptions' ; case 'Mutation' : return 'MutationOptions' ; case 'Query' : return 'QueryOptions' ; default : throw new Error ( 'unknown operation type: ' + operationType ) ; } } }