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 plugin offers extensive configuration options to customize the generated React Apollo code. All options extend the base client-side plugin configuration and provide fine-grained control over code generation.
interface ReactApolloRawPluginConfig extends RawClientSideBasePluginConfig {
withComponent?: boolean;
withHOC?: boolean;
withHooks?: boolean;
withMutationFn?: boolean;
withRefetchFn?: boolean;
withFragmentHooks?: boolean;
apolloReactCommonImportFrom?: string;
apolloReactComponentsImportFrom?: string;
apolloReactHocImportFrom?: string;
apolloReactHooksImportFrom?: string;
componentSuffix?: string;
reactApolloVersion?: 2 | 3;
withResultType?: boolean;
withMutationOptionsType?: boolean;
addDocBlocks?: boolean;
defaultBaseOptions?: ReactApolloPluginConfigDefaultBaseOptions;
hooksSuffix?: string;
}
interface ReactApolloPluginConfigDefaultBaseOptions {
awaitRefetchQueries?: boolean;
errorPolicy?: string;
fetchPolicy?: string;
ignoreResults?: boolean;
notifyOnNetworkStatusChange?: boolean;
returnPartialData?: boolean;
ssr?: boolean;
[key: string]: any;
}booleanfalse// Example configuration
config: {
withComponent: true // Generates <QueryComponent>, <MutationComponent>
}booleanfalse// Example configuration
config: {
withHOC: true // Generates withQuery, withMutation HOCs
}booleantrue// Example configuration
config: {
withHooks: true // Generates useQuery, useMutation, useSubscription hooks
}booleantruebooleanfalsebooleanfalse2 | 33// Apollo Client v3 (default)
config: {
reactApolloVersion: 3 // Uses @apollo/client imports
}
// Apollo Client v2 (legacy)
config: {
reactApolloVersion: 2 // Uses separate @apollo/react-* packages
}string"@apollo/react-common" (v2) or "@apollo/client" (v3)string"@apollo/react-hooks" (v2) or "@apollo/client" (v3)string"Component"// With default suffix
config: {
componentSuffix: "Component" // Generates GetUserComponent
}
// With custom suffix
config: {
componentSuffix: "View" // Generates GetUserView
}string""booleantrue// Generated with addDocBlocks: true
/**
* __useGetUserQuery__
*
* To run a query within a React component, call `useGetUserQuery`...
*/
export function useGetUserQuery(baseOptions?: Apollo.QueryHookOptions<GetUserQuery, GetUserQueryVariables>) {
// ... implementation
}booleantruebooleantrueReactApolloPluginConfigDefaultBaseOptionsconfig: {
defaultBaseOptions: {
errorPolicy: 'all',
fetchPolicy: 'cache-first',
notifyOnNetworkStatusChange: true
}
}
// Generated code will include:
const defaultOptions = {
errorPolicy: 'all',
fetchPolicy: 'cache-first',
notifyOnNetworkStatusChange: true
} as const;config: {
withHooks: true,
withComponent: false,
withHOC: false,
withMutationFn: true,
reactApolloVersion: 3,
addDocBlocks: true
}config: {
withHooks: true,
withComponent: true,
withHOC: true,
reactApolloVersion: 2,
addDocBlocks: true
}config: {
withHooks: true,
withFragmentHooks: true,
reactApolloVersion: 3
}
// This generates useFragment hooks like:
// export function useUserFieldsFragment<F = { id: string }>(identifiers: F) {
// return Apollo.useFragment<UserFieldsFragment>({
// fragment: UserFieldsFragmentDoc,
// fragmentName: "UserFields",
// from: { __typename: "User", ...identifiers }
// });
// }config: {
hooksSuffix: "Hook" // Changes useGetUserQuery to useGetUserQueryHook
}