{"id":"aa058de5dbf43c7d","repo":"nestjs/nest","slug":"id-aa058d","errorCode":null,"errorMessage":"${id}","messagePattern":"\\$\\{id\\}","errorType":"http","errorClass":"NotFoundException","httpStatus":404,"severity":"error","filePath":"sample/33-graphql-mercurius/src/recipes/recipes.resolver.ts","lineNumber":24,"sourceCode":"  Query,\n  Resolver,\n  Subscription,\n} from '@nestjs/graphql';\nimport { PubSub } from 'mercurius';\nimport { NewRecipeInput } from './dto/new-recipe.input';\nimport { RecipesArgs } from './dto/recipes.args';\nimport { Recipe } from './models/recipe.model';\nimport { RecipesService } from './recipes.service';\n\n@Resolver(of => Recipe)\nexport class RecipesResolver {\n  constructor(private readonly recipesService: RecipesService) {}\n\n  @Query(returns => Recipe)\n  async recipe(@Args('id') id: string): Promise<Recipe> {\n    const recipe = await this.recipesService.findOneById(id);\n    if (!recipe) {\n      throw new NotFoundException(id);\n    }\n    return recipe;\n  }\n\n  @Query(returns => [Recipe])\n  recipes(@Args() recipesArgs: RecipesArgs): Promise<Recipe[]> {\n    return this.recipesService.findAll(recipesArgs);\n  }\n\n  @Mutation(returns => Recipe)\n  async addRecipe(\n    @Args('newRecipeData') newRecipeData: NewRecipeInput,\n    @Context('pubsub') pubSub: PubSub,\n  ): Promise<Recipe> {\n    const recipe = await this.recipesService.create(newRecipeData);\n    pubSub.publish({ topic: 'recipeAdded', payload: { recipeAdded: recipe } });\n    return recipe;\n  }","sourceCodeStart":6,"sourceCodeEnd":42,"githubUrl":"https://github.com/nestjs/nest/blob/6ec0e2783d15290732447f304d8549b591b9749e/sample/33-graphql-mercurius/src/recipes/recipes.resolver.ts#L6-L42","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Seed at least one recipe via the addRecipe mutation before querying by id.","Use the recipes() list query to discover valid ids first.","Return null (matching a nullable schema return type) instead of throwing, for graceful partial responses.","Pass a descriptive message to NotFoundException for easier client-side debugging."],"exampleFix":"// before\nconst recipe = await this.recipesService.findOneById(id);\nif (!recipe) {\n  throw new NotFoundException(id);\n}\n\n// after\nconst recipe = await this.recipesService.findOneById(id);\nif (!recipe) {\n  throw new NotFoundException(`Recipe ${id} not found`);\n}","handlingStrategy":"validation","validationCode":"// Seed at least one recipe, or list before reading by id.\nawait client.mutate({ mutation: ADD_RECIPE, variables: { newRecipeData: { ... } } });\nconst { data } = await client.query({ query: RECIPES });\nconst id = data.recipes[0]?.id; // known-good id\n","typeGuard":"function isRecipe(v: unknown): v is Recipe {\n  return typeof v === 'object' && v !== null\n    && typeof (v as any).id === 'string'\n    && typeof (v as any).title === 'string';\n}","tryCatchPattern":"// Mercurius client (mercurius-integration-testing or fetch)\ntry {\n  const res = await client.query(RECIPE, { id });\n} catch (e) {\n  if (/not found/i.test(e?.message ?? '') || e?.errors?.[0]?.extensions?.code === 'NOT_FOUND') {\n    // show empty-state UI\n  }\n}","preventionTips":["Run an addRecipe mutation before querying by id on a cold server.","Return null for nullable schema fields instead of throwing for expected absence.","Add a top-level `recipes` list query so clients can discover valid ids.","Keep subscription topic and recipe lifecycle in sync so removed recipes stop being referenced."],"tags":["graphql","mercurius","nestjs","not-found","resolver"],"analyzedSha":"6ec0e2783d15290732447f304d8549b591b9749e","analyzedAt":"2026-08-03T17:42:23.673Z","schemaVersion":2}