Instead of using the errors array of your response you can also model your schema to represent expected errors. That way you will know which error you need to handle while also having full type safety.
One great approach for achieving that is by using unions:
type User {
id: ID!
name: String!
}
type NotFoundError {
reason: String!
}
union UserResult = User | NotFoundError
type Query {
user(id: ID!): UserResult!
}
Sample Query:
query {
user(id: "1") {
type: __typename
... on User {
id
name
}
... on NotFoundError {
reason
}
}
}
Instead of using the errors array of your response you can also model your schema to represent expected errors. That way you will know which error you need to handle while also having full type safety.
One great approach for achieving that is by using unions:
Sample Query:
@sachee did a great talk about this: youtube.com/watch?v=GYBhHUGR1ZY
Thank u so much for nice explanation