i'm working on creating mutations for a project i'm working on but stuck on the resolver returns, i won't to handle the errors while i'm at it as well by sending back messages for notifications to make it user friendly, but let's say my mutation is UPDATE_USER and it should also return the updated user instance, how can i set my mutation to double returns if possible?
that's how it looks
export const UPDATE_USER = {
type: UserType,
args: {
user_id: {type: GraphQLString}!,
first_name: {type: GraphQLString}!,
last_name: {type: GraphQLString},
but i want something like this incase there's no user like the update failed for some reason
export const UPDATE_USER = {
type: UserType|| MessageType,
args: {
user_id: {type: GraphQLString}!,
first_name: {type: GraphQLString}!,
last_name: {type: GraphQLString},
but i can't find a way to make it work
In GraphQL, you can't directly return multiple types from a single field, but you can create a wrapper type that can contain either the user or the error message. This is often called a "union type" or an "interface type".
Here's how you can define a union type for this purpose:
Then, you can use this
UserResulttype in yourUPDATE_USERmutation:In the resolver, you can specify which type you are returning by setting the
__typenamefield. The client can then use inline fragments to handle the different types:This way, you can return either a
UserTypeor aMessageTypefrom yourUPDATE_USERmutation, and the client can handle both cases.