ns.bruno

ns.bruno

How to create function to return all common fields in absinthe

I’m trying to minimize repetitions in field definitions in a Type, input and query.
With this I don’t know how to do it, so I ask for help from the community.

Below is the code I’m trying to do, but without success.

defmodule MyApp.UserType do
  use Absinthe.Schema.Notation
  import Absinthe.Schema.Notation

  @descricao_type "User registration that can access the system."

  @desc @descricao_type
  object :user_types do
    MyApp.UserType.comuns_fields()

    field :password_hash,
          non_null(:string),
          description: "User-defined and already encrypted password."
  end

  # São todos os fields/campos que devem estar presentes no momento da inserção/atualização
  @desc @descricao_type
  input_object :user_input do
    MyApp.UserType.comuns_fields()

    field :password, non_null(:string),
      description: "Password entered by the user to be registered."
  end

  defmacro comuns_fields() do
    quote do
      field :email,
            non_null(:string),
            description: "Email address that will be used to log into the system."

      field :enable,
            :boolean,
            description: "Mark if user is active or not. By default it is active."
    end
  end
end

I’m getting the following error message:

== Compilation error in file lib/on_sistemas/schema/types/administracao/sistema/usuario_types.ex ==
** (CompileError) lib/my_app/types/user_types.ex:16: undefined function comuns_fields/0 (there is no such import)
    (absinthe 1.7.0) expanding macro: Absinthe.Schema.Notation.input_object/2
    lib/my_app/types/user_types.ex:16: OnSistemas.Graphql.Schema.Types.Administracao.Sistema.UsuarioType (module)

Marked As Solved

maartenvanvliet

maartenvanvliet

Absinthe has a built-in method to reuse fields.

  object :user_fields do
      field :email,
            non_null(:string),
            description: "Email address that will be used to log into the system."

      field :enable,
            :boolean,
            description: "Mark if user is active or not. By default it is active."
  end

  input_object :user_input do
    field :password, non_null(:string),
      description: "Password entered by the user to be registered."

    import_fields :user_fields
  end

Where Next?

Popular in Questions Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> somethi...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
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
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
dotdotdotPaul
Okay, I’m having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I’m sure I’...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
New

Other popular topics Top

AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
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
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39523 209
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
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
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

We're in Beta

About us Mission Statement