DEV Community

loading...

Discussion on: Introduction to GraphQL

Collapse
n1ru4l profile image
Laurin Quast

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

@sachee did a great talk about this: youtube.com/watch?v=GYBhHUGR1ZY

Collapse
ashraful profile image
Ashraful Islam Author

Thank u so much for nice explanation