How to Handle Input Types in TypeGraphQL for Mutations? #1751
Stormer0528
started this conversation in
General
Replies: 1 comment
-
To handle input types in TypeGraphQL, you can define a separate InputType class that serves as the structure for the mutation arguments. Here's an example for creating a new user with a mutation: import "reflect-metadata";
import { ObjectType, Field, Int, InputType, Mutation, Arg, Resolver } from "type-graphql";
@ObjectType()
class User {
@Field(() => Int)
id: number;
@Field()
name: string;
@Field()
email: string;
}
let mockUsers = [
{ id: 1, name: "John Doe", email: "john@example.com" },
{ id: 2, name: "Jane Smith", email: "jane@example.com" },
];
@InputType()
class UserInput {
@Field()
name: string;
@Field()
email: string;
}
@Resolver()
class UserResolver {
@Mutation(() => User)
addUser(@Arg("data") data: UserInput): User {
const newUser = { id: mockUsers.length + 1, ...data };
mockUsers.push(newUser);
return newUser;
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I'm working on a GraphQL API using TypeGraphQL and want to create a mutation to add a new user. I understand I need to pass arguments like name and email to the mutation. How can I properly define and use input types for mutations in TypeGraphQL?
Beta Was this translation helpful? Give feedback.
All reactions