/ectograph

Ectograph is a set of utility functions for using Ecto in combination with GraphQL (joshprice/graphql-elixir)

Primary LanguageElixirMIT LicenseMIT

Ectograph

Ectograph is a set of utility functions for using Ecto in combination with GraphQL (specifically this graphql library for Elixir).

defp deps do
  [
    { :ecto, "~> 1.1.3" },
    { :graphql, "~> 0.1.2" }
  ]
end

Features

  • Map a Ecto.Type to a GraphQL.Type
  • Map a Ecto.Schema to a GraphQL.Type.ObjectType
  • Map a GraphQL.Type to a Ecto.Type
  • Provide extra GraphQL types, such as DateTime

How to use

Schemas
defmodule Schemas.Quote do
  use Ecto.Schema

  schema "quotes" do
    field :quote, :string
    field :author, :string

    timestamps
  end

end

Ectograph.Schema.cast_schema(Schemas.Quote, :ecto_to_graphql)
# %GraphQL.Type.ObjectType{ name: "quotes", fields: %{ quote: %{ type: ... }, ... }}
Types
Ectograph.Type.cast_type(:string, :ecto_to_graphql)
# %GraphQL.Type.String{}

Ectograph.Type.cast_type({ :array, :integer }, :ecto_to_graphql)
# %GraphQL.Type.List{ ofType: :integer }

Ectograph.Type.cast_type(%GraphQL.Type.String{}, :graphql_to_ecto)
# :string

Ectograph.Type.cast_type(%GraphQL.Type.List{ ofType: :integer }, :graphql_to_ecto)
# { :array, :integer }
Example

You can find a working example at https://github.com/icidasset/key_maps.
The crucial bit is located at lib/graphql/definitions.ex.

Installation

If available in Hex, the package can be installed as:

  1. Add ectograph to your list of dependencies in mix.exs:

    def deps do [{:ectograph, "~> 0.0.1"}] end

  2. Ensure ectograph is started before your application:

    def application do [applications: [:ectograph]] end

To do

Missing features:

  • Casting a GraphQL schema to an Ecto schema (not sure how to implement this)

Things I haven't tried yet:

  • Associations
  • Embedded schemas

Ecto types that still have to be implemented:

  • binary
  • binary_id
  • decimal
  • date
  • time
  • composite types