zf

zenflows testing
git clone https://s.sonu.ch/~srfsh/zf.git
Log | Files | Refs | Submodules | README | LICENSE

parser.ex (976B)


      1 defmodule Absinthe.Plug.Parser do
      2   @moduledoc """
      3   Extracts the GraphQL request body.
      4 
      5   For use with `Plug.Parsers`, as in the example below.
      6 
      7   ## Examples
      8 
      9   Should be used with `Plug.Parsers`, before `Absinthe.Plug`:
     10 
     11       plug Plug.Parsers,
     12         parsers: [:urlencoded, :multipart, :json, Absinthe.Plug.Parser],
     13         pass: ["*/*"],
     14         json_decoder: Jason
     15 
     16       plug Absinthe.Plug,
     17         schema: MyAppWeb.Schema
     18   """
     19 
     20   @behaviour Plug.Parsers
     21 
     22   @doc false
     23   def init(opts), do: opts
     24 
     25   @doc false
     26   def parse(conn, "application", "graphql", _headers, opts) do
     27     case Plug.Conn.read_body(conn, opts) do
     28       {:ok, body, conn} ->
     29         {:ok, %{"query" => body}, conn}
     30 
     31       {:more, _data, conn} ->
     32         {:error, :too_large, conn}
     33 
     34       {:error, :timeout} ->
     35         raise Plug.TimeoutError
     36 
     37       {:error, _} ->
     38         raise Plug.BadRequestError
     39     end
     40   end
     41 
     42   def parse(conn, _type, _subtype, _headers, _opts) do
     43     {:next, conn}
     44   end
     45 end