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

None

(Official docs)

(Example code)

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:

$util.toJson($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.

{
  "version": "2018-05-29",
  "payload": $util.toJson($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
  }
}

Will get the result in the expected format:

{
  "data": {
    "user": {
      "last_modified": "2022-08-24T08:22:43.932Z",
    }
  }
}
Using resolver arguments

The resolver also has access to any arguments the query defines. For example, clients might want to define the date format:

type User {
  id: ID!
  last_login(format: String!): String!
}

In this case, the resolver mapping template can use the $ctx.args.format to convert the $ctx.source.last_login:

{
  "version": "2018-05-29",
  "payload": $util.toJson(
    $util.time.epochMilliSecondsToFormatted($ctx.source.last_login, $ctx.args.format)
  )
}

This way, the query contains the format:

query MyQuery {
  user(id: "user1") {
    last_login(format: "MM/dd/YYYY HH:mm")
  }
}

And the response is converted to that:

{
  "data": {
    "user": {
      "last_login": "08/24/2022 08:23",
    }
  }
}

Derived data

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