Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performing mutations #25

Open
churcho opened this issue Mar 18, 2021 · 1 comment
Open

Performing mutations #25

churcho opened this issue Mar 18, 2021 · 1 comment

Comments

@churcho
Copy link

churcho commented Mar 18, 2021

Does this client offer the ability to perform mutations on an endpoint? Looked thru the doc and can't seem to find any.

@Menkir
Copy link

Menkir commented Apr 1, 2022

Idk if its up-to-date but maybe it helps some other guys.
The Client does not support mutations natively. But in the end mutations are http requests as well as queries. So, in order to have a Client that behaves for mutation operations analogous to the underlying api you need to inject the other methods like delete, delete_by, put, put_by and so on and implement the handle callbacks in the client.
Example a Behvaiour that can be applied to any client:

defmodule ClientExtension do
  @type return :: :ok | {:ok, any()} | {:error, any()}
  @callback delete_by(term :: atom(), variables :: map()) :: return()
  @callback post_by(term :: atom(), variables :: map()) :: return()
  @callback put_by(term :: atom(), variables :: map()) :: return()

  defmacro __using__(_args) do
    quote do
      @behaviour unquote(__MODULE__)

      @impl true
      def put_by(term, variables), do: handle(:put_by, term, variables)

      @impl true
      def post_by(term, variables), do: handle(:post_by, term, variables)

      @impl true
      def delete_by(term, variables), do: handle(:delete_by, term, variables)

    end
  end
end

The Context should also simply map its function to client().post_by etc. You can also achieving that by writing some Context-Macro that maps these functions implicitly.

I don't know why the team didn't include the mutation operations as part of the standard api.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants