It would be nice if our blog posts had a
posted_at time. This would
be something we could both send as part of our CreatePost mutation,
and also retrieve in a query.
Here we have a small conundrum. While GraphQL strings have an obvious counterpart in elixir strings, time in Elixir is often represented by something like a Timex struct. We could handle this in our resolvers by manually serializing or deserializing the time data. Fortunately however GraphQL provides a better way via allowing us to build additional Scalar types.
Let’s define our time type:
Our post should now look like:
And our mutation in the schema should look like:
posted_at is passed as an argument, the parse function we
defined in our
:time type will be called and it will automatically
arrive in our resolver as a
Timex.DateTime struct! Similarly, when
we return the
posted_at field the
Timex.DateTime struct will be
serialized back to a string for easy JSON representation.
Next: Read "Complex Arguments"