Consider this NestJS GraphQL resolver method:
@Query(() => String)
getGreeting() {
return `Hello, NestJS!`;
}What will be the response when this query is called?
@Query(() => String)
getGreeting() {
return `Hello, NestJS!`;
}Look at the return value of the method and its type.
The resolver method returns the string "Hello, NestJS!" directly. The GraphQL query will respond with this string.
Given this resolver snippet:
@Resolver()
export class CounterResolver {
private count = 0;
@Query(() => Number)
getCount() {
return this.count;
}
@Mutation(() => Number)
increment() {
this.count++;
return this.count;
}
}If the increment mutation is called twice, what will getCount return?
@Resolver() export class CounterResolver { private count = 0; @Query(() => Number) getCount() { return this.count; } @Mutation(() => Number) increment() { this.count++; return this.count; } }
Think about how many times the count is increased and what the initial value is.
The count starts at 0. Each call to increment adds 1. After two calls, count is 2, so getCount returns 2.
Which of the following resolver methods correctly accepts an argument id of type string and returns it?
Remember the NestJS decorator to extract GraphQL arguments.
In NestJS GraphQL, @Args('id') is used to get the argument named 'id'. Other options use incorrect decorators or syntax.
Consider this resolver code snippet:
@Query(() => String)
async fetchData() {
const data = await this.service.getData();
return data.value;
}If this.service.getData() returns null, what error will occur?
@Query(() => String) async fetchData() { const data = await this.service.getData(); return data.value; }
What happens if you try to access a property on null?
Accessing data.value when data is null causes a TypeError because null has no properties.
In NestJS GraphQL, when a query requests multiple fields resolved by different resolver methods, how does NestJS execute these resolvers?
Think about how GraphQL resolves fields and nested fields.
GraphQL executes resolvers for fields at the same level in parallel. Nested fields wait for their parent field's resolver to complete before executing.