pierreabreup

pierreabreup

Reuse object fields as input_object fields in Absinthe

I have a absinthe object type like that:

defmodule ContentProxyGraphql.Schema.ObjectTypes.Content do
  use Absinthe.Schema.Notation
  use Absinthe.Relay.Schema, :modern

  import_types Absinthe.Type.Custom

  object :content do
    field :type_of, non_null(:string)
    field :name, non_null(:string)
    field :date_created, non_null(:datetime)
    field :date_modified, non_null(:datetime)
    field :identifier, non_null(:identifier)
    field :publisher, :publisher
  end

  object :identifier do
    field :type_of, non_null(:string)
    field :property_id,  non_null(:string)
    field :value, non_null(:string)
  end

  object :publisher do
    field :type_of, non_null(:string)
    field :name, non_null(:string)
  end

end

In my schema.ex, I have the query

query do


    field :contents, list_of(:content) do
      resolve &Resolvers.Resources.Content.list/3
    end

  end

Now I’m creating the mutation

mutation do 
  field :create_content, type: :content do
    arg :typeOf, non_null(:string)
    arg :name, non_null(:string)
    arg :dateCreated, non_null(:datetime)
    arg :dateModified, non_null(:datetime)

    resolve &Resolvers.Resources.Content.create/3
  end
end

In my mutation I intend to add arg :identifier and arg :publisher. According to absinthe documentation (Complex Arguments — absinthe v1.11.0) I have to create a input_object :indentifier and a input_object :publisher with their fields, which are the same of respective object :identifier and object :publisher.

Is there a way to reuse object fields inside input_object? I’m trying it to avoid code duplication, in other words, I would like to avoid

input_object :identifier do 
    field :type_of, non_null(:string)
    field :property_id,  non_null(:string)
    field :value, non_null(:string)
end

@benwilson512 FYI

First Post!

dylan-chong

dylan-chong

there is a import_fields function now so you can do inheritance on types. not a perfect solution but way better than not having it at all

Last Post!

alexandru-calinoiu

alexandru-calinoiu

Not sure if you ever got around testing it, but it actually works to use import_fields

Where Next?

Popular in Questions Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

Other popular topics Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31586 112
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44265 214
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New

We're in Beta

About us Mission Statement