You can find code example for this chapter here.
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!
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.
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),
}
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
}
}