How to Set Up a Simple Query in TypeGraphQL? #1750
-
I'm new to TypeGraphQL and trying to set up a simple query to fetch user data from a mock array of users. I'm not sure how to structure the query or the resolver properly. Can someone provide a basic example of how to create a query in TypeGraphQL that fetches a list of users? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
https://github.com/MichalLytek/type-graphql/blob/master/examples/simple-usage/recipe.resolver.ts |
Beta Was this translation helpful? Give feedback.
-
Setting up a simple query in TypeGraphQL is straightforward. Here's an example where we define a User type and a query to return a list of users from a mock array: import "reflect-metadata";
import { ObjectType, Field, Int, Query, Resolver } from "type-graphql";
@ObjectType()
class User {
@Field(() => Int)
id: number;
@Field()
name: string;
@Field()
email: string;
}
const mockUsers = [
{ id: 1, name: "John Doe", email: "john@example.com" },
{ id: 2, name: "Jane Smith", email: "jane@example.com" },
];
@Resolver()
class UserResolver {
@Query(() => [User])
users() {
return mockUsers;
}
} |
Beta Was this translation helpful? Give feedback.
Setting up a simple query in TypeGraphQL is straightforward. Here's an example where we define a User type and a query to return a list of users from a mock array: