How to Set Up a Simple Query in TypeGraphQL? #1750
Answered
by
christian0429
avocado1109
asked this question in
Q&A
-
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.
Answered by
christian0429
Oct 4, 2024
Replies: 2 comments
-
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: "[email protected]" },
{ id: 2, name: "Jane Smith", email: "[email protected]" },
];
@Resolver()
class UserResolver {
@Query(() => [User])
users() {
return mockUsers;
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
avocado1109
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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: