ash-project / ash_graphql

The extension for building GraphQL APIs with Ash
https://hexdocs.pm/ash_graphql
MIT License
73 stars 49 forks source link

Type deduplication quirks #163

Closed rbino closed 5 months ago

rbino commented 5 months ago

I've run across a couple of issues regarding type deduplication.

Types are deduplicated only in the first? domain

Here uniq_by/2 gets run only in the first? branch, but not for the other one.

A duplicate type can happen if, e.g., a NewType is used as output in a calculation and argument in a mutation. In this case it appears twice here.

This should be an easy fix, either deduplicating it in global_maps or, probably better, in AshGraphql, but it would actually be probably superseded by a fix to the following issue

Types are not deduplicated across domains

I've run into this while reusing a generic type like

defmodule Edgehog.Localization.LocalizedAttribute do
  use Ash.Type.NewType,
    subtype_of: :map,
    constraints: [
      fields: [
        language_tag: [
          type: :string,
          allow_nil?: false
        ],
        value: [
          type: :string,
          allow_nil?: false
        ]
      ]
    ]
  use AshGraphql.Type

  @impl true
  def graphql_type(_), do: :localized_attribute

  @impl true
  def graphql_input_type(_), do: :localized_attribute_input
end

This is something which is supposed to be used across domains, but each domain was generating the GraphQL type for the map. We should probably do a dedup pass at the end instead of doing it per-domain. I'm not an expert on GraphQL blueprints, but if there's the need I can try to tackle this if I get some pointers about where to put my hands.