Skip to content

Latest commit

 

History

History
163 lines (128 loc) · 2.14 KB

File metadata and controls

163 lines (128 loc) · 2.14 KB

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)",