Graphql new type falls back to JsonString

Hello, I got a new type defined as:
defmodule ContactInfo do
use Ash.Type.NewType,
subtype_of: :map,
constraints: [
fields: [
vat: [
type: :string,
allow_nil?: false,
constraints: [
min_length: 11,
max_length: 11
]
],
name: [
type: :string,
allow_nil?: false
],
main_contact: [
type: :string,
allow_nil?: false
]
]
]

def graphql_type(_), do: :contact_info
end
defmodule ContactInfo do
use Ash.Type.NewType,
subtype_of: :map,
constraints: [
fields: [
vat: [
type: :string,
allow_nil?: false,
constraints: [
min_length: 11,
max_length: 11
]
],
name: [
type: :string,
allow_nil?: false
],
main_contact: [
type: :string,
allow_nil?: false
]
]
]

def graphql_type(_), do: :contact_info
end
I would have expected for the grapqhl generated type to have each field defined, instead the ContactInfo type is globally defined as JsonString. Is this intended behaviour, or am I missing something?
8 Replies
ZachDaniel
ZachDaniel•2mo ago
Sounds wrong to me. How are you using this type in your resources?
Oliver
Oliver•2mo ago
input type missing? they are separate
er.califfo
er.califfoOP•2mo ago
this is how it is used in the resource:
defmodule Bodyshop do
use Ash.Resource,
otp_app: :bodyshop,
domain: Bodyshop.Catalog,
data_layer: AshPostgres.DataLayer,
extensions: [AshGraphql.Resource]

postgres do
table "bodyshops"
repo Bodyshop.Repo
end

graphql do
type :bodyshop
end

actions do
defaults [:read, :update, :destroy]

create :insert do
accept [:name, :services, :contact_info, :location]
end
end

attributes do
uuid_primary_key :id

attribute :name, :string do
public? true
allow_nil? false
end

attribute :contact_info, :contact_info do
public? true
allow_nil? false
end

timestamps(public?: true)
end
end
defmodule Bodyshop do
use Ash.Resource,
otp_app: :bodyshop,
domain: Bodyshop.Catalog,
data_layer: AshPostgres.DataLayer,
extensions: [AshGraphql.Resource]

postgres do
table "bodyshops"
repo Bodyshop.Repo
end

graphql do
type :bodyshop
end

actions do
defaults [:read, :update, :destroy]

create :insert do
accept [:name, :services, :contact_info, :location]
end
end

attributes do
uuid_primary_key :id

attribute :name, :string do
public? true
allow_nil? false
end

attribute :contact_info, :contact_info do
public? true
allow_nil? false
end

timestamps(public?: true)
end
end
Oliver
Oliver•2mo ago
you can use a newtype like that iirc, use an embed instead
ZachDaniel
ZachDaniel•2mo ago
A newtype should work that way just fine
Oliver
Oliver•2mo ago
in graphql? there might be a bug then, because I yanked some stuff to embeds not that long ago cause I also couldnt get newtype to work properly as an attr for a map I just assumed it wasnt meant to work 🤷
er.califfo
er.califfoOP•2mo ago
from the docs it sounds like I should be able to have visibility on the inner fields, but maybe I interpreted it wrong https://hexdocs.pm/ash_graphql/use-maps-with-graphql.html
Straffern
Straffern•4w ago
I also ran into this issue. What worked was doing as @Oliver Mulelid-Tynes said and use an embedded resource.

Did you find this page helpful?