StrawHat.GraphQL v0.2.0 StrawHat.GraphQL.MetadataResolver View Source

It is been used on metadata object type of the graph, it is StrawHat.Error.ErrorMetadata.t/0 mapping. It converts the key and value to the proper values for the GraphQL.

Link to this section Summary

Functions

Converts the key from the map using Absinthe Adapter

Converts the value from the map using Absinthe Adapter IF AND ONLY IF the value is field_name

Link to this section Functions

Link to this function key(map, args, resolution) View Source
key(%{key: String.t()}, any(), %{adapter: Absinthe.Adapter.t()}) ::
  {:ok, String.t()}

Converts the key from the map using Absinthe Adapter.

Link to this function value(map, args, resolution) View Source
value(%{key: String.t(), value: String.t()}, any(), %{
  adapter: Absinthe.Adapter.t()
}) :: {:ok, String.t()}
value(%{value: String.t()}, any(), any()) :: {:ok, String.t()}

Converts the value from the map using Absinthe Adapter IF AND ONLY IF the value is field_name.

field_name is RESERVED ON THE METADATA. field_name is used for map Ecto.Changeset field name on the changeset.

The following example shows the field password_confirmation failing so the value of field_name on the metadata will be field_name: :password_confirmation.

changeset = User.changeset(%UserSchema{}, %{password_confirmation: "123qwe"})
{:error, changeset} = Repo.insert(changeset)
changeset.errors #=> [password_confirmation: {"is invalid", []}]

The reason behind is that Absinthe converts the args names or field names on the input_objects using the setup Absinthe Adapter so normally the clients want the coorelation between those names and the field name to be the same.

For Example

arg :password_confirmation, non_null(:string)

# or

input_object :account_input do
  field :password_confirmation, non_null(:string)
end

The actual name that the clients have to use depends of the Absinthe Adapter used, this is important because the following example could be different if you changed the Absinthe Adapter setup. The default adapter will actually expect passwordConfirmation from the client and it will convert it into password_confirmation. So we need to convert it back into passwordConfirmation for the key of that metadata. Check StrawHat.Error.ChangesetParser so you could understand more how we use field_name from Ecto.Changeset.