Absinthe – working with Date.Range?

I am working with date ranges quite a lot and I was wondering what is the easiest way to convert an incoming JavaScript/GraphQL input such as { first: date1, last: date2 } into Elixir’s Date.Range struct.

I see an example of a custom scalar type here:

scalar :datetime, name: "DateTime" do
  serialize &DateTime.to_iso8601/1
  parse &parse_datetime/1

But it’s easy to serialize an incoming string.

However in the case of a date range, I receive a JS object, right? I’m not sure how to deal with this.

Should I avoid creating a custom type for it maybe?

Seems like a Custom type can only work with incoming primitives, such as a string.

When my type is represented by several properties (such as “first date” and “last date” to express a date range) in a JS object, Absinthe will show an error “Unknown field”.

@benwilson512 can this be seen as a limitation?

It is an intentional limitation to date. Scalars in the GraphQL spec have a certain syntax. Input objects in the spec also have a certain syntax. I haven’t seen any affordance to date in the spec to allow a schema to reinterpret the input object syntax to mean a scalar.

1 Like