I wrote up the second post in a series I’m doing on using Phoenix and Absinthe to build GraphQL API’s. In this post I cover adding CRUD functionality with mutations. Please let me know if you have any questions. Happy to help in any way that I can!
Nice post! I was wondering though: do you ever played with the idea @chrismccord mentioned on 2015’s ElixirConf Keynote? More specifically what he talks between 25th and 27th minutes, the idea of using a Phoenix.View with a GraphQL as the data layer.
We’ll get this out soon. We actually have it driving the Phoenix CRUD scaffolding with very minimal edits (including queries and mutations). Just some work around serialization (eg, views probably don’t want serialized time structs they then have to deserialize) and some utilities for error feedback before we push this out there.
Here’s a sneak peek of working code. Note the 3-arity actions; Absinthe processes input using the associated document, hands off result to action for response. It is pretty cool to have your Absinthe schema handle the complexities of input validation and database operations and just focus on what to show them!
@graphql """
mutation ($id: ID!, $user: InputUser!) {
user: update_user(id: $id, input: $user) { ... UserFields }
}
"""
# Note: The "UserFields" fragment used above is defined by the View, as
# it's a view concern.
def update(conn, %{user: user}, []) do
conn
|> put_flash(:info, "User updated successfully.")
|> redirect(to: user_path(conn, :show, user))
end
def update(conn, %{user: user}, _errors) do
conn
|> put_flash(:error, "An error occurred.")
|> redirect(to: user_path(conn, :edit, user))
end
Unfortunately this fell behind some other features. I’ll report back here when I have a better idea of when we can get it out there – I’ll try to at least get some code up people can play with, even if it’s not released on Hex.
This would be great! Looking at this discussion, quite some people are building core, web and api applications, but using this it might be possible to have the api as the core and use it on the server in web