Convert Figma logo to code with AI

absinthe-graphql logoabsinthe

The GraphQL toolkit for Elixir

4,264
525
4,264
64

Top Related Projects

21,201

Peace of mind from prototype to production

6,132

A toolkit for data mapping and language integrated query.

1,380

An Elixir implementation of gRPC

2,841

Compose web applications with functions

24,265

Elixir is a dynamic, functional language for building scalable and maintainable applications

Quick Overview

Absinthe is a GraphQL toolkit for Elixir, providing a robust and flexible implementation of the GraphQL specification. It allows developers to build scalable and efficient GraphQL APIs in Elixir, with support for real-time subscriptions, custom schema definitions, and integration with popular Elixir web frameworks.

Pros

  • Seamless integration with Elixir and Phoenix ecosystem
  • Excellent performance and scalability due to Elixir's concurrency model
  • Comprehensive feature set, including subscriptions and custom directives
  • Active community and regular updates

Cons

  • Steeper learning curve for developers new to Elixir or functional programming
  • Limited resources and tutorials compared to more mainstream GraphQL implementations
  • Some advanced features may require additional plugins or extensions

Code Examples

  1. Defining a simple GraphQL schema:
defmodule MyAppWeb.Schema do
  use Absinthe.Schema

  query do
    field :hello, :string do
      resolve fn _, _, _ -> {:ok, "Hello, world!"} end
    end
  end
end
  1. Creating a resolver for a more complex type:
defmodule MyAppWeb.Resolvers.User do
  def list_users(_parent, _args, _resolution) do
    {:ok, MyApp.Accounts.list_users()}
  end

  def get_user(_parent, %{id: user_id}, _resolution) do
    case MyApp.Accounts.get_user(user_id) do
      nil -> {:error, "User not found"}
      user -> {:ok, user}
    end
  end
end
  1. Setting up a subscription:
defmodule MyAppWeb.Schema do
  use Absinthe.Schema

  # ... other schema definitions

  subscription do
    field :user_created, :user do
      config fn _, _ ->
        {:ok, topic: "users"}
      end
    end
  end
end

Getting Started

To start using Absinthe in your Elixir project:

  1. Add Absinthe to your mix.exs dependencies:
def deps do
  [
    {:absinthe, "~> 1.7"},
    {:absinthe_plug, "~> 1.5"}
  ]
end
  1. Run mix deps.get to install the dependencies.

  2. Create a schema file (e.g., lib/my_app_web/schema.ex) and define your GraphQL schema:

defmodule MyAppWeb.Schema do
  use Absinthe.Schema

  query do
    # Define your queries here
  end

  mutation do
    # Define your mutations here
  end
end
  1. Configure Absinthe in your Phoenix router (lib/my_app_web/router.ex):
defmodule MyAppWeb.Router do
  use MyAppWeb, :router

  forward "/api", Absinthe.Plug,
    schema: MyAppWeb.Schema

  forward "/graphiql", Absinthe.Plug.GraphiQL,
    schema: MyAppWeb.Schema
end

Now you can start building your GraphQL API with Absinthe!

Competitor Comparisons

21,201

Peace of mind from prototype to production

Pros of Phoenix

  • Full-featured web framework with built-in ORM, routing, and templating
  • Excellent performance and scalability for real-time applications
  • Large and active community with extensive documentation and resources

Cons of Phoenix

  • Steeper learning curve for developers new to Elixir or functional programming
  • May be overkill for simple API-only applications or microservices
  • Less flexible for non-standard architectures or custom data layers

Code Comparison

Phoenix (Router):

defmodule MyAppWeb.Router do
  use MyAppWeb, :router
  
  scope "/api", MyAppWeb do
    pipe_through :api
    resources "/users", UserController, only: [:index, :show]
  end
end

Absinthe (Schema):

defmodule MyAppWeb.Schema do
  use Absinthe.Schema
  
  query do
    field :users, list_of(:user) do
      resolve &MyApp.Resolvers.User.list_users/3
    end
  end
end

While Phoenix is a comprehensive web framework, Absinthe is specifically designed for building GraphQL APIs in Elixir. Phoenix provides a traditional REST-style routing system, whereas Absinthe uses a schema-based approach for defining GraphQL types and resolvers. Both are powerful tools, but they serve different purposes within the Elixir ecosystem.

6,132

A toolkit for data mapping and language integrated query.

Pros of Ecto

  • More comprehensive database toolkit, offering query building, migrations, and schema definitions
  • Supports multiple databases, including PostgreSQL, MySQL, and SQLite
  • Provides powerful associations and preloading capabilities for complex data relationships

Cons of Ecto

  • Steeper learning curve due to its broader scope and feature set
  • May be overkill for simple applications or those not requiring a full ORM
  • Less focused on GraphQL-specific functionality

Code Comparison

Ecto query example:

query = from u in User,
  where: u.age > 18,
  select: u.name

Repo.all(query)

Absinthe resolver example:

resolve fn _, _, _ ->
  users = Repo.all(from u in User, where: u.age > 18, select: u.name)
  {:ok, users}
end

While Ecto is a powerful database toolkit for Elixir applications, Absinthe focuses specifically on GraphQL implementation. Ecto excels in database interactions and complex queries, whereas Absinthe shines in creating GraphQL APIs. The choice between them depends on the project's requirements, with Ecto being more suitable for general database operations and Absinthe for GraphQL-centric applications.

1,380

An Elixir implementation of gRPC

Pros of grpc

  • Better performance for high-volume, low-latency communication
  • Strong typing and contract-first approach
  • Built-in support for streaming data

Cons of grpc

  • Steeper learning curve compared to GraphQL
  • Less flexibility in querying data
  • More complex setup and configuration

Code Comparison

Absinthe (GraphQL) schema definition:

object :user do
  field :id, :id
  field :name, :string
  field :email, :string
end

grpc protocol definition:

message User {
  int32 id = 1;
  string name = 2;
  string email = 3;
}

While Absinthe uses Elixir-native syntax for schema definition, grpc relies on Protocol Buffers for service and message definitions. Absinthe offers a more declarative approach to defining types and relationships, while grpc focuses on strict message structures and RPC-style communication.

Absinthe excels in flexibility and ease of use for web APIs, making it ideal for client-driven data fetching. grpc, on the other hand, shines in scenarios requiring high-performance, bi-directional streaming, and strong typing, particularly in microservices architectures.

Choose Absinthe for GraphQL-based APIs with flexible querying, or opt for grpc when dealing with high-throughput, low-latency communication between services.

2,841

Compose web applications with functions

Pros of Plug

  • More general-purpose and flexible for building web applications
  • Simpler and easier to learn for beginners
  • Lightweight and fast, with minimal overhead

Cons of Plug

  • Lacks built-in GraphQL support
  • Requires more manual setup for complex API scenarios
  • Less specialized tooling for API development

Code Comparison

Plug example:

defmodule MyApp.Router do
  use Plug.Router
  plug :match
  plug :dispatch

  get "/hello" do
    send_resp(conn, 200, "Hello, World!")
  end
end

Absinthe example:

defmodule MyApp.Schema do
  use Absinthe.Schema

  query do
    field :hello, :string do
      resolve fn _, _, _ -> {:ok, "Hello, World!"} end
    end
  end
end

Plug is a general-purpose web application framework for Elixir, while Absinthe is specifically designed for building GraphQL APIs. Plug offers more flexibility and simplicity for various web applications, making it easier for beginners to get started. However, Absinthe provides specialized tools and abstractions for GraphQL, making it more suitable for complex API development scenarios. The code examples demonstrate the difference in approach, with Plug using a routing-based system and Absinthe utilizing a schema-based structure for defining GraphQL queries.

24,265

Elixir is a dynamic, functional language for building scalable and maintainable applications

Pros of Elixir

  • General-purpose programming language with a broader scope and application range
  • Built-in concurrency support through the Erlang VM (BEAM)
  • Extensive standard library and ecosystem for various development needs

Cons of Elixir

  • Steeper learning curve for developers new to functional programming
  • Smaller community compared to mainstream languages like Python or JavaScript
  • Limited tooling and IDE support compared to more established languages

Code Comparison

Elixir (basic HTTP request):

defmodule HttpClient do
  def get(url) do
    HTTPoison.get!(url).body
  end
end

Absinthe (GraphQL schema definition):

defmodule MyAppWeb.Schema do
  use Absinthe.Schema

  query do
    field :hello, :string do
      resolve fn _, _, _ -> {:ok, "Hello, world!"} end
    end
  end
end

While Elixir is a general-purpose programming language, Absinthe is a specialized GraphQL toolkit for Elixir. Elixir provides the foundation for building various applications, including web services, while Absinthe focuses on implementing GraphQL APIs within Elixir projects. Elixir offers more flexibility and broader use cases, whereas Absinthe excels in GraphQL-specific functionality and integration with Elixir-based web frameworks like Phoenix.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Absinthe

Build Status Version Hex Docs Download License Last Updated

GraphQL implementation for Elixir.

Goals:

  • Complete implementation of the GraphQL Working Draft.
  • An idiomatic, readable, and comfortable API for Elixir developers
  • Extensibility based on small parts that do one thing well.
  • Detailed error messages and documentation.
  • A focus on robustness and production-level performance.

Please see the website at https://absinthe-graphql.org.

Why Use Absinthe?

Absinthe goes far beyond GraphQL specification basics.

Easy-to-Read, Fast-to-Run Schemas

Absinthe schemas are defined using easy-to-read macros that build and verify their structure at compile-time, preventing runtime errors and increasing performance.

Pluggability

The entire query processing pipeline is configurable. Add, swap out, or remove the parser, individual validations, or resolution logic at will, even on a per-document basis.

Advanced Resolution

Absinthe includes a number of advanced resolution features, to include:

  • Asynchronous field resolution
  • Batched field resolution (addressing N+1 query problems)
  • A resolution plugin system supporting further extensibility

Safety

  • Complexity analysis and configurable limiting
  • Support for precompiled documents/preventing custom documents

Idiomatic Documents, Idiomatic Code

Write your schemas in idiomatic Elixir snake_case notation. Absinthe can transparently translate to camelCase notation for your API clients.

Or, define your own translation schema by writing a simple adapter.

Frontend Support

We care about support for third-party frameworks, both on the back and front end.

So far, we include specialized support for Phoenix and Plug on the backend, and Relay on the frontend.

Of course we work out of the box with other frontend frameworks and GraphQL clients, too.

Installation

Install from Hex.pm:

def deps do
  [{:absinthe, "~> 1.7.0"}]
end

Note: Absinthe requires Elixir 1.10 or higher.

Upgrading

See CHANGELOG for upgrade steps between versions.

Documentation

Mix Tasks

Absinthe includes a number of useful Mix tasks for extracting schema metadata.

Run mix help in your project and look for tasks starting with absinthe.

Related Projects

See the GitHub organization.

Community

The project is under constant improvement by a growing list of contributors, and your feedback is important. Please join us in Slack (#absinthe-graphql under the Elixir Slack account) or the Elixir Forum (tagged absinthe).

Please remember that all interactions in our official spaces follow our Code of Conduct.

Contribution

Please follow contribution guide.

License

See LICENSE.md.