I had a look at the article you linked, but I seem to be struggling to understand how I return "this or that" from a mutation. My example is as follows:
I'd like to log a user in, and if the user doesn't exist, return a "Custom Error". In my head it would look something like this:
login(
password: String!
email: String!
): User || CustomError
Essentially, we return a User or a CustomError - these are both types defined in the schema. However, I don't know if this is possible (cant get it working at least!).
Tying the return of login down to a "LoginResponse" object feels wrong, because i'll need to have a "-InsertmutationNameHere-Response" object for every mutation. It seems better to have a system of "if we have data, return it or return CustomError" - if thats possible!
Thanks very much for the swift reply!
I had a look at the article you linked, but I seem to be struggling to understand how I return "this or that" from a mutation. My example is as follows:
I'd like to log a user in, and if the user doesn't exist, return a "Custom Error". In my head it would look something like this:
Essentially, we return a User or a CustomError - these are both types defined in the schema. However, I don't know if this is possible (cant get it working at least!).
Tying the return of login down to a "LoginResponse" object feels wrong, because i'll need to have a "-InsertmutationNameHere-Response" object for every mutation. It seems better to have a system of "if we have data, return it or return CustomError" - if thats possible!
Oh gotcha! Look into union types. Unions allow you to return this or that and are often used with Error types.
Your example would probably look like this in the schema:
The docs describe unions in more detail: graphql.org/learn/schema/#union-types
And Sasha Solomon has written about how she's used this pattern at Medium and Twitter: medium.com/@sachee/200-ok-error-ha...
Hey Eve, sorry for not replying. I got this working using your initial idea - returning a custom type :)