From harness-claude
> Generate type-safe TypeScript code from GraphQL schemas and operations to eliminate manual type maintenance
npx claudepluginhub intense-visions/harness-engineering --plugin harness-claudeThis skill uses the workspace's default tool permissions.
> Generate type-safe TypeScript code from GraphQL schemas and operations to eliminate manual type maintenance
Generates GraphQL schema generator operations, configurations, and code for API development. Auto-activates on 'graphql schema generator' phrases or related queries.
Structures GraphQL client code using fragments, normalized caching with typePolicies, optimistic updates, and cache strategies for responsive React/Vue/Svelte UIs.
Guides writing GraphQL queries, mutations, subscriptions, and fragments with best practices for efficiency, naming, variables, directives, and optimization. Use for data fetching patterns, type generation, linting, and reviews.
Share bugs, ideas, or general feedback.
Generate type-safe TypeScript code from GraphQL schemas and operations to eliminate manual type maintenance
npm install -D @graphql-codegen/cli @graphql-codegen/typescript @graphql-codegen/typescript-operations @graphql-codegen/typescript-resolvers
codegen.ts configuration file at the project root. Use the TypeScript config format for type-safe configuration.import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
schema: 'src/schema/**/*.graphql',
documents: 'src/**/*.{ts,tsx}',
generates: {
'src/__generated__/types.ts': {
plugins: ['typescript', 'typescript-operations'],
config: {
strictScalars: true,
scalars: {
DateTime: 'string',
JSON: 'Record<string, unknown>',
},
},
},
'src/__generated__/resolvers.ts': {
plugins: ['typescript', 'typescript-resolvers'],
config: {
useIndexSignature: true,
contextType: '../context#Context',
},
},
},
};
export default config;
TypedDocumentNode for end-to-end type safety. This plugin generates typed document nodes that carry their variables and result types — Apollo Client and urql pick them up automatically.// codegen generates:
// export const GetUserDocument: TypedDocumentNode<GetUserQuery, GetUserQueryVariables>
// Usage — variables and data are fully typed with no extra annotations:
const { data } = useQuery(GetUserDocument, { variables: { id: '1' } });
// data.user.name is typed as string
typescript-react-apollo or typescript-urql plugin to generate useGetUserQuery() hooks directly.// codegen.ts addition
'src/__generated__/hooks.ts': {
plugins: ['typescript', 'typescript-operations', 'typescript-react-apollo'],
config: {
withHooks: true,
withComponent: false,
},
},
near-operation-file preset to co-locate generated types next to the files that define the operations, instead of one giant generated file.generates: {
'src/': {
preset: 'near-operation-file',
presetConfig: { extension: '.generated.ts', baseTypesPath: '__generated__/types.ts' },
plugins: ['typescript-operations', 'typescript-react-apollo'],
},
},
{
"scripts": {
"codegen": "graphql-codegen",
"codegen:watch": "graphql-codegen --watch"
}
}
Enable strictScalars to force explicit scalar mappings. Without this, custom scalars default to any, defeating the purpose of code generation.
Type your resolvers with the generated Resolvers type. This catches mismatches between your schema and resolver implementations at compile time.
import { Resolvers } from './__generated__/resolvers';
export const resolvers: Resolvers = {
Query: {
user: async (_parent, { id }, context) => {
// args.id is typed as string, return type is enforced
return context.dataSources.users.findById(id);
},
},
};
.gitignore or commit them — pick one strategy and enforce it. Committing ensures CI does not need the schema endpoint; ignoring ensures no stale generated code.Plugin ecosystem: typescript (base types from schema), typescript-operations (types from queries/mutations), typescript-resolvers (resolver signatures), typescript-react-apollo / typescript-urql (framework hooks), typed-document-node (framework-agnostic typed documents).
Schema sources: Codegen can read schemas from .graphql files, a running endpoint, a JSON introspection result, or code-first builders. Prefer .graphql files for CI stability.
Fragment handling: Codegen generates types for fragments and composes them into operation types. Use fragments for shared fields to keep generated types DRY.
Enum handling: By default, codegen generates TypeScript enum. Set enumsAsTypes: true to generate union string literals instead, which work better with tree-shaking and are simpler to use.
Common mistakes:
any types leaking throughnode_modules or build output (generate into src/)https://the-guild.dev/graphql/codegen