GraphQL Code Generator执行报错:GraphQL文档验证失败问题求助
Let’s break down why you’re hitting this validation error and walk through actionable fixes:
1. Missing RegularUser Fragment Definition
The most probable cause is the ...RegularUser fragment used in your Login mutation—GraphQL can’t locate its definition, which triggers the validation failure.
Create a fragment file (e.g., src/graphql/fragments/user.graphql) with a definition that matches your backend User type:
fragment RegularUser on User { id email # Add all other fields you need from the User type here }
Since your codegen.yml includes all .graphql files under src/graphql/**/, this fragment will be automatically picked up once created.
2. Align Backend Schema with Frontend Mutation Expectations
Double-check that your backend’s UserResponse type (returned by the login mutation) exactly matches what your frontend mutation expects:
- Ensure your
UserResponseobject type in the backend includes botherrors(array ofFieldError) anduser(nullableUser) fields, like this:@ObjectType() export class FieldError { @Field() field: string; @Field() message: string; } @ObjectType() export class UserResponse { @Field(() => [FieldError], { nullable: true }) errors?: FieldError[]; @Field(() => User, { nullable: true }) user?: User; } - Confirm the
Usertype in your backend has all the fields referenced in theRegularUserfragment.
3. Ensure Backend Service is Accessible
Your codegen.yml pulls the schema from http://localhost:4000/graphql—make sure your backend server is running on that port. If the schema isn’t reachable, codegen can’t validate your frontend documents against it.
4. Clean Stale Generated Files and Regenerate
Stale generated code can cause unexpected issues. Delete the existing output file and rerun codegen:
rm src/generated/graphql.tsx && yarn run graphql-codegen --config codegen.yml
If none of these resolve the error, run codegen with the verbose flag to get more specific validation details:
yarn run graphql-codegen --config codegen.yml --verbose
This will show you the exact rule that’s failing (e.g., a missing field or undefined fragment) to narrow down the problem further.
内容的提问来源于stack exchange,提问作者skele

