graphql-elixir / plug_graphql

Plug (Phoenix) integration for GraphQL Elixir
Other
126 stars 7 forks source link

Handle content-type: application/graphql #10

Closed Bockit closed 8 years ago

Bockit commented 8 years ago

What does this PR do?

Handles request which send the Content-Type: application/graphql header and the raw request body is the query.

How should this be manually tested?

  1. Check this branch out
  2. Add it as a path dep to a something using plug_graphql and has a schema
  3. Send a post request with the Content-Type: application/graphql header and the query in the request body.

E.g. (postman, headers tab sets the content-type):

screen shot 2016-01-16 at 12 34 55 pm

Any background context you want to provide?

I tried doing pattern matching with an in guard clause:

def call(%Conn{method: m, req_headers: headers} = conn, schema) when {"content-type", "application/graphql"} in headers do
    case read_body(conn) do
      {:err, reason} -> handle_error(conn, reason)
      {:ok, query}   ->
        conn = Map.put(conn, :params, %{"query" => query})
        call(conn, schema)
      end
  end

But you get this error:

== Compilation error on file lib/graphql/plug/endpoint.ex ==
** (ArgumentError) invalid args for operator in, it expects a compile time list or range on the right side when used in guard expressions, got: headers

So this works, but it may be time to rethink how the pattern matching works here a little bit.

Bockit commented 8 years ago

Thanks for the feedback. Have pushed the changes. I was trying to avoid having to handle checking the query wasn't empty in a second place, but perhaps too much an abuse of pattern matching.

Still have to test.

Bockit commented 8 years ago

@joshprice: Added the tests for the application/graphql handler.

joshprice commented 8 years ago

Looks good, thanks!