Skip to content

minirepos/js-express-apollo-error-handling

Repository files navigation

Express with Apollo Server and Error Handling

Basic public query

JSON body, with Content-Type: application/json header:

{
  "query": "{ helloPublic }"
}

In Postman GraphQL mode or in GraphiQL:

Query:

{ helloPublic }

Response:

{
  "data": {
    "helloPublic": "Hello public"
  }
}

Query with variables

JSON body, with Content-Type: application/json header:

{
  "query": "query($num: Int!) { helloVariable(someNumber: $num) }",
  "variables": {
    "num": 3
  }
}

In Postman GraphQL mode or in GraphiQL:

Query:

query($num: Int!) { helloVariable(num: $someNumber) }

Variables:

{
  "num": 3
}

Reponse:

{
  "data": {
    "helloVariable": "Your number was 3"
  }
}

Private query

In Postman GraphQL mode or in GraphiQL:

{ helloPrivate }
  "errors": [
    {
      "message": "Not authenticated",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "helloPrivate"
      ],
      "extensions": {
        "code": "UNAUTHENTICATED",
        "exception": {
          "stacktrace": [
            "AuthenticationError: Not authenticated",

In Postman GraphQL mode or in GraphiQL with the dummy user: whatever header, which is used to simulate an authenticated user:

{
  "data": {
    "helloPrivate": "Hello private"
  }
}

Bad Input Handling

In Postman GraphQL mode or in GraphiQL:

query ($num: Int!) { helloBadInput(lessThanFive: $num) }

With variables:

{
  "num": 3
}

Response:

{
  "data": {
    "helloBadInput": true
  }
}

With variables:

{
  "num": 10
}

Response:

{
  "errors": [
    {
      "message": "Argument lessThanFive must be less than 5 (received 10)",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "helloBadInput"
      ],
      "extensions": {
        "code": "BAD_USER_INPUT",
        "exception": {
          "stacktrace": [
            "UserInputError: Argument lessThanFive must be less than 5 (received 10)",

About

Express with Apollo Server and Error Handling

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published