Coder Social home page Coder Social logo

thetamind / ash_json_api Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ash-project/ash_json_api

0.0 1.0 0.0 1.43 MB

A JSON:API extension for the Ash Framework

Home Page: https://hexdocs.pm/ash_json_api

License: MIT License

Elixir 100.00%

ash_json_api's Introduction

AshJsonApi

Elixir CI License: MIT Coverage Status Hex version badge

AshJsonApi allows you to take resources created with Ash and build complete JSON:API compliant endpoints with just a few lines of code.

This is what AshJsonApi does:

  1. Route Creation: AshJsonApi defines routes and actions in your app based on resource configurations
  2. Deserialization: When an incoming HTTP request hits a AshJsonApi defined route/action, AshJsonApi will parse it from /api/users?filter[admin]=true into a call to ash
  3. Query Execution: AshJsonApi then executes the parsed Ash Action (this is the integration point between AshJsonApi and Ash Core, where Ash Actions are defined)
  4. Serialization: AshJsonApi then serializes the result of the Ash Action into JSON API objects.
  5. Response: AshJsonApi then sends this JSON back to the client
  6. Schema Generation: AshJsonApi generates a machine-readable JSON Schema of your entire API and a route/action that can serve it

As you can see, Ash takes care of all of the data related work for a request (CRUD, Sorting, Filtering, Pagination, Side Loading, and Authorization) while AshJsonApi more or less replaces controllers and serializers.

The beauty of putting all of that data functionality into a non-web layer (Ash) is that it can be used in many contexts. A JSON:API is one context - but there are others such as GraphQL or just using an Ash Resource from other code within an Application. The decoupling of the web from data layers is why AshJsonApi is it's own hex package, as opposed to just a module within Ash.

Usage

Assume you have already built a resource using Ash such as this Post resource:

defmodule Post do
  use Ash.Resource,
    data_layer: Ash.DataLayer.Postgres

  # If no actions are defined, simple defaults are implemented called `:read`, `:create`, `:update`, `:destroy`

  attributes do
    attribute :name, :string
  end

  relationships do
    belongs_to :author, Author
  end
end

As you can see, the resource takes care of interacting with the database, setting up attributes and relationships, as well as specifying actions (CRUD) that can be performed on the resource. What is now needed is to add a configuration for how this resource will interact with JSON:API

defmodule Post do
  use Ash.Resource,
    data_layer: AshPostgres,
    extensions: [AshJsonApi.Resource]

  ...

  json_api do
    routes do
      base "/posts"
      # Add a `GET /posts/:id` route, that calls into the :read action called :read
      get :read
      # Add a `GET /posts` route, that calls into the :read action called :read 
      index :read
    end

  end

  ...

Then, update your API with the API extension and configuration:

  defmodule MyApp.Api do
    use Ash.Api, extensions: [AshJsonApi.Api]

    json_api do
      ...
    end

  end

See the hex documentation for more.

See AshJsonApi.Api and AshJsonApi.Resource for the DSL documentation.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.