Skip to content
This repository has been archived by the owner on Sep 2, 2022. It is now read-only.

Should be possible to return {data: null} from resolvers without getting an error #558

Closed
nikolasburk opened this issue Sep 14, 2017 · 1 comment
Assignees
Milestone

Comments

@nikolasburk
Copy link
Member

nikolasburk commented Sep 14, 2017

What feature are you missing?

In a resolver function, it should be possible to return {data: null} without having to also provide an error.

How could this feature look like in detail? Tradeoffs?

Considering the equivalent for the standard user query that returns the logged in user or null. If you want to build that for a custom auth type using a resolver, you would set up the schema extension e.g. like this:

type AuthenticatedEmailUserPayload {
  id: String!
  email: String!
}

extend type Query {
  authenticatedEmailUser: AuthenticatedEmailUserPayload
}

authenticatedEmailUser is optional, so it should be allowed to just return null:

module.exports = function authenticatedEmailUser(event) {

  if (!event.context.auth || !event.context.auth.nodeId) {
    return { data: null }
  }

  ...
}

However, with this implementation the server also returns an error for non-logged-in users:

{
  "data": {
    "authenticatedEmailUser": null
  },
  "errors": [
    {
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "authenticatedEmailUser"
      ],
      "code": 5006,
      "message": "The return value should include a 'data' field of type object",
      "requestId": "eu-west-1:simple:cj7k7742xq5ha0118jq8vigrf"
    }
  ]
}
@do4gr
Copy link
Member

do4gr commented Sep 30, 2017

As part of the rework of the resolver function return types it is now possible to return {data: null} for resolver payload types that are optional ( PayloadType and [PayloadType!]). Returning just an empty response {} will also soon be possible.

@do4gr do4gr closed this as completed Sep 30, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants