GraphQL Code Generator plugin for generating TypeScript-typed React Apollo components, hooks, and HOCs from GraphQL operations
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
The main plugin function processes GraphQL schema and documents to generate React Apollo-specific TypeScript code. It creates hooks, components, HOCs, and types based on the provided configuration.
declare const plugin: PluginFunction<ReactApolloRawPluginConfig, Types.ComplexPluginOutput>;
function plugin(
schema: GraphQLSchema,
documents: Types.DocumentFile[],
config: ReactApolloRawPluginConfig,
): Types.ComplexPluginOutput;GraphQLSchema - The GraphQL schema to generate code fromTypes.DocumentFile[] - Array of GraphQL document files containing operationsReactApolloRawPluginConfig - Plugin configuration optionsReturns a Types.ComplexPluginOutput object containing:
interface ComplexPluginOutput {
prepend: string[]; // Import statements and setup code
content: string; // Generated React Apollo code
}The plugin is typically used through GraphQL Code Generator configuration:
// codegen.ts
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
schema: 'schema.graphql',
documents: 'src/**/*.graphql',
generates: {
'src/generated/graphql.tsx': {
plugins: ['typescript', 'typescript-operations', 'typescript-react-apollo'],
config: {
withHooks: true,
withComponent: false,
withHOC: false,
withMutationFn: true,
withRefetchFn: false,
reactApolloVersion: 3,
addDocBlocks: true
}
}
}
};The plugin generates different types of code based on configuration:
use[OperationName]Queryuse[OperationName]LazyQueryuse[OperationName]SuspenseQueryuse[OperationName]Mutationuse[OperationName]Subscriptionwith[OperationName][OperationName][OperationType]Result[OperationName]MutationFn[OperationName]HookResultThe plugin works by:
ReactApolloVisitor instance// Internal implementation pattern
const allAst = concatAST(documents.map(v => v.document));
const visitor = new ReactApolloVisitor(schema, allFragments, config, documents);
const visitorResult = oldVisit(allAst, { leave: visitor });
return {
prepend: visitor.getImports(),
content: [visitor.fragments, ...visitorResult.definitions].join('\n'),
};