0
0
NestJSframework~10 mins

Queries and mutations in NestJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Queries and mutations
Client sends GraphQL request
NestJS GraphQL Resolver
Query Handler
Fetch data
Return response to client
The client sends a GraphQL request which NestJS routes to either a query or mutation handler. Queries fetch data, mutations change data, then responses return to the client.
Execution Sample
NestJS
import { Resolver, Query, Mutation, Args } from '@nestjs/graphql';

@Resolver()
export class SampleResolver {
  @Query(() => String)
  hello() { return 'Hello World'; }

  @Mutation(() => String)
  updateMessage(@Args('newMsg') newMsg: string) { return newMsg; }
}
Defines a simple NestJS GraphQL resolver with a query returning a greeting and a mutation updating a message.
Execution Table
StepRequest TypeResolver MethodActionResult
1QueryhelloCalled hello()Returns 'Hello World'
2MutationupdateMessageCalled updateMessage with 'Hi!'Returns 'Hi!'
3QueryhelloCalled hello()Returns 'Hello World'
4MutationupdateMessageCalled updateMessage with 'Bye!'Returns 'Bye!'
5End-No more requestsExecution stops
💡 No more client requests; server waits for next GraphQL operation.
Variable Tracker
VariableStartAfter Step 2After Step 4Final
newMsgundefined'Hi!''Bye!''Bye!'
Key Moments - 2 Insights
Why does the query 'hello' always return the same result?
Because the 'hello' query method returns a fixed string without changing any state, as shown in execution_table rows 1 and 3.
How does the mutation 'updateMessage' change the output?
The mutation takes an argument 'newMsg' and returns it, changing the output each time it's called, as seen in execution_table rows 2 and 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what does the 'hello' query return at step 3?
A'Hello World'
B'Hi!'
C'Bye!'
Dundefined
💡 Hint
Check the 'Result' column for step 3 in the execution_table.
At which step does the mutation receive the argument 'Hi!'?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Action' column for when 'updateMessage' is called with 'Hi!' in the execution_table.
If the mutation 'updateMessage' always returned 'Hello', how would the variable 'newMsg' change?
AIt would be undefined
BIt would alternate between 'Hi!' and 'Bye!'
CIt would always be 'Hello'
DIt would cause an error
💡 Hint
Refer to variable_tracker to see how 'newMsg' changes with mutation calls.
Concept Snapshot
NestJS GraphQL uses resolvers with @Query for fetching data and @Mutation for changing data.
Queries do not change state; mutations can accept arguments to update data.
Each resolver method returns data sent back to the client.
Use @Args to get input values for mutations.
Queries and mutations are separate methods but both return responses.
Full Transcript
In NestJS GraphQL, clients send requests that are either queries or mutations. Queries fetch data without changing anything, while mutations modify data. The resolver class uses decorators @Query and @Mutation to mark methods for these operations. For example, a 'hello' query returns a fixed greeting string. A mutation called 'updateMessage' takes an argument and returns it, simulating data change. The execution table shows step-by-step calls and results. Variables like 'newMsg' track mutation inputs. Understanding this flow helps beginners see how GraphQL operations work in NestJS.