0

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.

3
  • 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. Commented Feb 5, 2018 at 6:51
  • 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 Commented Feb 6, 2018 at 1:42
  • 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. Commented Feb 6, 2018 at 4:58

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

0

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
    ```

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.