Skip to content

How to Set Up a Simple Query in TypeGraphQL? #1750

Answered by christian0429
avocado1109 asked this question in Q&A
Discussion options

You must be logged in to vote

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;
  }
}

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by avocado1109
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants