Object type for mutation argument - GraphQL, Absinthe

I have a field I need to accept that is in the shape of a map, but I’m not sure how to tell Absinthe/GraphQL that it should expect a map as the argument? Here is what I’m working with:

field :create_prediction, :prediction do
  arg(:question_id, non_null(:id))
  arg(:team_id, non_null(:id))
  arg(:prediction_params, non_null(:map))
end

ERROR:

:map is not defined in your schema.

The prediction_params field should be able to accept this: %{player_points: 10} but I’m not sure how to make that possible with Absinthe. Does anybody know how to handle this problem? I wasn’t able to find what I needed from the docs.

There aren’t any map fields or args. You’ll need to define an input object with the precise fields that will be accepted.

It can be a little bit of a pain, but the trade off of typed inputs is worth it.

5 Likes

You have to create a input_object ( https://hexdocs.pm/absinthe/complex-arguments.html#content )
Ex.:

input_object :predicition do
  field :player_points, :integer
end

field :create_prediction, :prediction do
  arg(:question_id, non_null(:id))
  arg(:team_id, non_null(:id))
  arg(:prediction_params, non_null(:predicition))
end