NestJS GraphQL cannot work out the output type of a field or query
Cannot determine a GraphQL output type for the "<name>". Make sure your class is decorated with an appropriate decorator.Short answer
In code-first mode Nest reads TypeScript types through decorators, and arrays, promises and unions disappear at runtime. When the mapping is ambiguous, you must give the decorator a type function: @Field(() => [Post]) for lists, @Query(() => Author) for resolvers.
Fix it fast
1Find out which cause you have
The message names the field or resolver method. Open that class member and look at the decorator above it: does it have a type function?
grep -rn "@Field\|@Query\|@ResolveField\|@Mutation" src2Match what you see, then jump to its fix
What does the error look like?
Cannot determine a GraphQL output type for the "<name>". Make sure your class is decorated with an appropriate decorator.
Cause 1 of 2
A list field has no array type function
1Check
TypeScript reflects an array property as plain Array, so Nest cannot see the item type. The docs say that when the field is an array, you must indicate the array type in the type function.
grep -rn -B1 "\[\];" src --include=*.ts2Fix
Give @Field a type function that returns the item type in square brackets.
@ObjectType()
export class Author {
@Field(() => [Post])
posts: Post[];
}3Verify
The app starts and the schema file lists posts as a list.
npm run start:devConfirmed by docs.nestjs.com
Cause 2 of 2
A resolver method returns a type that cannot be inferred
1Check
The type function is required when the mapping between TypeScript and GraphQL is ambiguous. A resolver can return its value directly or as a promise, so state the GraphQL type on the decorator.
grep -rn "@Query()\|@Mutation()\|@ResolveField()" src2Fix
Pass the return type to the decorator. Wrap it in brackets for lists.
@Query(() => Author)
async author(@Args('id', { type: () => Int }) id: number) {
return this.authorsService.findOneById(id);
}
@Query(() => [Author])
authors() {
return this.authorsService.findAll();
}3Verify
Open the GraphQL endpoint and run the query: the type resolves.
curl -s http://localhost:3000/graphql -H "Content-Type: application/json" -d '{"query":"{ authors { id } }"}'Confirmed by docs.nestjs.com
How do I stop it coming back?
- Write the type function on every @Field, @Query and @Mutation by habit, even when it looks optional.
- Mark optional fields with { nullable: true }; fields are non-nullable by default.
- Commit the generated schema file (autoSchemaFile) so a changed type shows up in code review.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
Related fixes
Back end
TypeScript cannot find module '@nestjs/core' or its type declarations
error TS2307: Cannot find module '@nestjs/common' or its corresponding type declarations.
TS2307 means TypeScript searched node_modules and found neither the package nor its types.
3 causes, each with a fixBack end
Node.js in Docker Compose cannot reach MySQL on 127.0.0.1:3306
Error: connect ECONNREFUSED 127.0.0.1:3306
Inside a container, 127.0.0.1 is that container, not MySQL.
3 causes, each with a fixBack end
IntelliJ IDEA breakpoints are ignored in a NestJS app
No 'Debugger listening' line appears when the NestJS app starts
IntelliJ can only debug a NestJS app when the Node.js process runs with the inspector on and the IDE attaches to its port.
4 causes, each with a fixBack end
The nest command is not found in the terminal
bash: nest: command not found
The shell cannot find a nest executable on its PATH.
3 causes, each with a fixBack end
NestJS watch mode does not restart after I save a file
Changes show on the host but not in the running container
Watch mode only sees files that exist inside the process that runs it.
3 causes, each with a fixBack end
NestJS app will not start: Cannot find module '/app/dist/main'
Cannot find module 'dist/main'
Node cannot find dist/main.js because the build did not put it there.
4 causes, each with a fix