nestjs/nest · error · NotFoundException

${id}

Error message

${id}

What it means

Same NotFoundException pattern as the Apollo sample, but in the Mercurius-backed GraphQL app at sample/33-graphql-mercurius/src/recipes/recipes.resolver.ts:24. The `recipe(id)` @Query resolver calls RecipesService.findOneById(id) and throws NotFoundException(id) — bare id as message — when nothing is found. Mercurius serialises Nest exceptions into the GraphQL response extensions/errors surface.

Source

Thrown at sample/33-graphql-mercurius/src/recipes/recipes.resolver.ts:24

  Query,
  Resolver,
  Subscription,
} from '@nestjs/graphql';
import { PubSub } from 'mercurius';
import { NewRecipeInput } from './dto/new-recipe.input';
import { RecipesArgs } from './dto/recipes.args';
import { Recipe } from './models/recipe.model';
import { RecipesService } from './recipes.service';

@Resolver(of => Recipe)
export class RecipesResolver {
  constructor(private readonly recipesService: RecipesService) {}

  @Query(returns => Recipe)
  async recipe(@Args('id') id: string): Promise<Recipe> {
    const recipe = await this.recipesService.findOneById(id);
    if (!recipe) {
      throw new NotFoundException(id);
    }
    return recipe;
  }

  @Query(returns => [Recipe])
  recipes(@Args() recipesArgs: RecipesArgs): Promise<Recipe[]> {
    return this.recipesService.findAll(recipesArgs);
  }

  @Mutation(returns => Recipe)
  async addRecipe(
    @Args('newRecipeData') newRecipeData: NewRecipeInput,
    @Context('pubsub') pubSub: PubSub,
  ): Promise<Recipe> {
    const recipe = await this.recipesService.create(newRecipeData);
    pubSub.publish({ topic: 'recipeAdded', payload: { recipeAdded: recipe } });
    return recipe;
  }

View on GitHub (pinned to 6ec0e2783d)

Solutions

  1. Seed at least one recipe via the addRecipe mutation before querying by id.
  2. Use the recipes() list query to discover valid ids first.
  3. Return null (matching a nullable schema return type) instead of throwing, for graceful partial responses.
  4. Pass a descriptive message to NotFoundException for easier client-side debugging.

Example fix

// before
const recipe = await this.recipesService.findOneById(id);
if (!recipe) {
  throw new NotFoundException(id);
}

// after
const recipe = await this.recipesService.findOneById(id);
if (!recipe) {
  throw new NotFoundException(`Recipe ${id} not found`);
}
Defensive patterns

Strategy: validation

Validate before calling

// Seed at least one recipe, or list before reading by id.
await client.mutate({ mutation: ADD_RECIPE, variables: { newRecipeData: { ... } } });
const { data } = await client.query({ query: RECIPES });
const id = data.recipes[0]?.id; // known-good id

Type guard

function isRecipe(v: unknown): v is Recipe {
  return typeof v === 'object' && v !== null
    && typeof (v as any).id === 'string'
    && typeof (v as any).title === 'string';
}

Try / catch

// Mercurius client (mercurius-integration-testing or fetch)
try {
  const res = await client.query(RECIPE, { id });
} catch (e) {
  if (/not found/i.test(e?.message ?? '') || e?.errors?.[0]?.extensions?.code === 'NOT_FOUND') {
    // show empty-state UI
  }
}

Prevention

When it happens

Trigger: GraphQL query `recipe(id: "<nonexistent>")` against the Mercurius endpoint; findOneById returns null/undefined → resolver throws. Common when the in-memory recipes array is empty or was reset.

Common situations: Sample store seeded via addRecipe mutation only — a cold server with no mutations yet means every lookup misses; id generated by one process but queried after a restart; Mercurius subscription/publish path references a recipe that was removed.

Related errors


AI-assisted analysis of nestjs/nest@6ec0e2783d (2026-08-03). Data as JSON: /data/errors/aa058de5dbf43c7d.json. Report an issue: GitHub.