0
votes

I have an Elixir API that can use Graphiql to send queries to my database, all the crud calls are working fine as you can see.

field :users, list_of(:user) do
        resolve &Graphical.UserResolver.all/2
    end

this returns all the users from a database. Now obviously this isn't ideal if your using a front end, you don't want to manually type out the query. How would i go about implementing functions that call these fields in the schema file? How do I write say this query

users{
  id,
  name,
  email
}

in the Elixir itself, so that I can call it from a front end to return all the data in JSON format. I am quite unsure where and how to write the queries so that they are passed to the schema and then Absinthe and Ecto take care of the rest.

2
If I understand your question correctly, you just tell Graphql what you want and it will handle all the data you need. If you need to filter the result you can add arg to your field use it in the resolver function. - vahid abdi
yes so I can manually do that. But how do i encapsulate the queries and mutations into functions that i can then call that function and it will run the query, without typing out the query through a gui - Errol Hassall
write an api and then write a function and put the logic inside it that you want to implement and then call that api from frontend .it will get the records from database base on the function you wrote and populate it on the frontend. - script

2 Answers

1
votes

Can do something Query:

users(scope: "some"){}

And in the resolver

defmodule Graphical.UserResolver do

  def all(_root,  %{:scope => "some"}, _info) do
    users = Graphical.Repo.all(from user in Graphical.User,
       select: { user.name, user.email, user.id }
     ) 
    {:ok, users}
  end

  def all(_root, _args, _info) do
    users = Graphical.Repo.all(Graphical.User)
    {:ok, users}
  end

end
0
votes

When you query Absinthe from the FE you will get a JSON object in return. If you want to have a field return a JSON object you will have to create a custom scalar and use it as the field type. e.g.

defmodule ApiWeb.Schema.Types.JSON do
  @moduledoc """
  The Json scalar type allows arbitrary JSON values to be passed in and out.
  Requires `{ :jason, "~> 1.1" }` package: https://github.com/michalmuskala/jason
  """
  use Absinthe.Schema.Notation

  scalar :json, name: "Json" do
    description("""
    The `Json` scalar type represents arbitrary json string data, represented as UTF-8
    character sequences. The Json type is most often used to represent a free-form
    human-readable json string.
    """)

    serialize(&encode/1)
    parse(&decode/1)
  end

  @spec decode(Absinthe.Blueprint.Input.String.t()) :: {:ok, term()} | :error
  @spec decode(Absinthe.Blueprint.Input.Null.t()) :: {:ok, nil}
  defp decode(%Absinthe.Blueprint.Input.String{value: value}) do
    case Jason.decode(value) do
      {:ok, result} -> {:ok, result}
      _ -> :error
    end
  end

  defp decode(%Absinthe.Blueprint.Input.Null{}) do
    {:ok, nil}
  end

  defp decode(_) do
    :error
  end

  defp encode(value), do: value
end

and then

    @desc "Create an auth token"
    field :create_auth_token, :token do
      arg(:app_id, non_null(:string))
      arg(:sub, non_null(:string))
      arg(:claims, :json)
      resolve(&Resolvers.Token.create_auth_token/3)
    end
    ```