You are viewing the preview version of this book
Click here for the full version.

None

Try it yourself

You can find code example for this chapter here.

(Official docs)

The None data source is a special type that does not interface with any external service. It's only usage is that it allows adding a mapping template to any field, even if all the information needed for that field is already present. Thus its primary use case is to convert the data.

Its structure is simple. The request mapping template needs to return a JSON with a version and a payload:

{
  "version": "2018-05-29",
  "payload": <anything>
}

Then the response mapping template gets the value in the payload as the ctx.result. Since the payload is simply copied without any modifications, the response just returns it:

return ctx.result;

This leaves only one place to define how the resolver should work: the value of the payload. Here, the template has access to all the usual values: ctx.identity, ctx.args, and ctx.source.

Let's see a few examples where the None data source is usually helpful!

Converting dates

The first use-case I needed to use the None data source was to convert between time formats. AWS provides some extra scalars, among them the AWSDateTime. This behaves like a String, but it ensures that the value conforms to ISO8601, such as 2022-08-24T08:22:43.932Z.

For example, a GraphQL schema might use the AWSDateTime for a field:

type User {
  id: ID!
  last_modified: AWSDateTime!
}

On the other hand, it's generally a best practice to store dates in a database as timestamps.

Timestamp stored in the database

A resolver for the User.last_modified field can use the None data source to convert the timestamp to ISO8601 format.

return {
  version: "2018-05-29",
  payload: util.time.epochMilliSecondsToISO8601(ctx.source.last_modified),
}
Source object

Note that ctx.source is what the parent resolver, in this case the Query.User returned. See more in the Resolver arguments chapter.

With this conversion, a query that defines this field:

query MyQuery {
  user(id: "user1") {
    last_modified
  }
}

There is more, but you've reached the end of this preview
Read this and all other chapters in full and get lifetime access to:
  • all future updates
  • full web-based access
  • PDF and Epub versions