Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Jason over Poison #193

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def deps do
[
...,
{:absinthe_plug, "~> 1.4.0"},
{:poison, "~> 3.1.0"}
{:jason, "~> 1.1.0"}
]
end
```
Expand All @@ -47,7 +47,7 @@ Basic Usage:
plug Plug.Parsers,
parsers: [:urlencoded, :multipart, :json, Absinthe.Plug.Parser],
pass: ["*/*"],
json_decoder: Poison
json_decoder: Jason

plug Absinthe.Plug,
schema: MyAppWeb.Schema
Expand All @@ -60,7 +60,7 @@ router like:
plug Plug.Parsers,
parsers: [:urlencoded, :multipart, :json, Absinthe.Plug.Parser],
pass: ["*/*"],
json_decoder: Poison
json_decoder: Jason

forward "/api",
to: Absinthe.Plug,
Expand Down
10 changes: 5 additions & 5 deletions lib/absinthe/plug.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ defmodule Absinthe.Plug do
plug Plug.Parsers,
parsers: [:urlencoded, :multipart, :json, Absinthe.Plug.Parser],
pass: ["*/*"],
json_decoder: Poison
json_decoder: Jason

plug Absinthe.Plug,
schema: MyAppWeb.Schema
Expand All @@ -20,7 +20,7 @@ defmodule Absinthe.Plug do
plug Plug.Parsers,
parsers: [:urlencoded, :multipart, :json, Absinthe.Plug.Parser],
pass: ["*/*"],
json_decoder: Poison
json_decoder: Jason

forward "/api",
to: Absinthe.Plug,
Expand Down Expand Up @@ -132,7 +132,7 @@ defmodule Absinthe.Plug do
- `:adapter` -- (Optional) Absinthe adapter to use (default: `Absinthe.Adapter.LanguageConventions`).
- `:context` -- (Optional) Initial value for the Absinthe context, available to resolvers. (default: `%{}`).
- `:no_query_message` -- (Optional) Message to return to the client if no query is provided (default: "No query document supplied").
- `:json_codec` -- (Optional) A `module` or `{module, Keyword.t}` dictating which JSON codec should be used (default: `Poison`). The codec module should implement `encode!/2` (e.g., `module.encode!(body, opts)`).
- `:json_codec` -- (Optional) A `module` or `{module, Keyword.t}` dictating which JSON codec should be used (default: `Jason`). The codec module should implement `encode!/2` (e.g., `module.encode!(body, opts)`).
- `:pipeline` -- (Optional) `{module, atom}` reference to a 2-arity function that will be called to generate the processing pipeline. (default: `{Absinthe.Plug, :default_pipeline}`).
- `:document_providers` -- (Optional) A `{module, atom}` reference to a 1-arity function that will be called to determine the document providers that will be used to process the request. (default: `{Absinthe.Plug, :default_document_providers}`, which configures `Absinthe.Plug.DocumentProvider.Default` as the lone document provider). A simple list of document providers can also be given. See `Absinthe.Plug.DocumentProvider` for more information about document providers, their role in procesing requests, and how you can define and configure your own.
- `:schema` -- (Required, if not handled by Mix.Config) The Absinthe schema to use. If a module name is not provided, `Application.get_env(:absinthe, :schema)` will be attempt to find one.
Expand All @@ -142,7 +142,7 @@ defmodule Absinthe.Plug do
- `:log_level` -- (Optional) Set the logger level for Absinthe Logger. Defaults to `:debug`.
- `:analyze_complexity` -- (Optional) Set whether to calculate the complexity of incoming GraphQL queries.
- `:max_complexity` -- (Optional) Set the maximum allowed complexity of the GraphQL query. If a document’s calculated complexity exceeds the maximum, resolution will be skipped and an error will be returned in the result detailing the calculated and maximum complexities.

"""
@type opts :: [
schema: module,
Expand Down Expand Up @@ -177,7 +177,7 @@ defmodule Absinthe.Plug do
pipeline = Keyword.get(opts, :pipeline, {__MODULE__, :default_pipeline})
document_providers = Keyword.get(opts, :document_providers, {__MODULE__, :default_document_providers})

json_codec = case Keyword.get(opts, :json_codec, Poison) do
json_codec = case Keyword.get(opts, :json_codec, Jason) do
module when is_atom(module) -> %{module: module, opts: []}
other -> other
end
Expand Down
2 changes: 1 addition & 1 deletion lib/absinthe/plug/document_provider/compiled.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ defmodule Absinthe.Plug.DocumentProvider.Compiled do
parsing the file and inverting the key/value pairs.

provide File.read!("/path/to/extracted_queries.json")
|> Poison.decode!
|> Jason.decode!
|> Map.new(fn {k, v} -> {v, k} end)

By default, the request parameter that will be used to lookup documents is
Expand Down
2 changes: 1 addition & 1 deletion lib/absinthe/plug/parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ defmodule Absinthe.Plug.Parser do
plug Plug.Parsers,
parsers: [:urlencoded, :multipart, :json, Absinthe.Plug.Parser],
pass: ["*/*"],
json_decoder: Poison
json_decoder: Jason

plug Absinthe.Plug,
schema: MyAppWeb.Schema
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ defmodule Absinthe.Plug.Mixfile do
[
{:plug, "~> 1.3.2 or ~> 1.4"},
{:absinthe, "~> 1.4.11"},
{:poison, ">= 0.0.0", only: [:dev, :test]},
{:jason, ">= 0.0.0", only: [:dev, :test]},
{:ex_doc, "~> 0.18.0", only: :dev}
]
end
Expand Down
2 changes: 1 addition & 1 deletion mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"absinthe": {:hex, :absinthe, "1.4.13", "81eb2ff41f1b62cd6e992955f62c22c042d1079b7936c27f5f7c2c806b8fc436", [:mix], [{:dataloader, "~> 1.0.0", [hex: :dataloader, repo: "hexpm", optional: true]}, {:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm"},
"earmark": {:hex, :earmark, "1.2.5", "4d21980d5d2862a2e13ec3c49ad9ad783ffc7ca5769cf6ff891a4553fbaae761", [:mix], [], "hexpm"},
"ex_doc": {:hex, :ex_doc, "0.18.3", "f4b0e4a2ec6f333dccf761838a4b253d75e11f714b85ae271c9ae361367897b7", [:mix], [{:earmark, "~> 1.1", [hex: :earmark, repo: "hexpm", optional: false]}], "hexpm"},
"jason": {:hex, :jason, "1.1.2", "b03dedea67a99223a2eaf9f1264ce37154564de899fd3d8b9a21b1a6fd64afe7", [:mix], [{:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm"},
"mime": {:hex, :mime, "1.2.0", "78adaa84832b3680de06f88f0997e3ead3b451a440d183d688085be2d709b534", [:mix], [], "hexpm"},
"plug": {:hex, :plug, "1.5.0", "224b25b4039bedc1eac149fb52ed456770b9678bbf0349cdd810460e1e09195b", [:mix], [{:cowboy, "~> 1.0.1 or ~> 1.1 or ~> 2.1", [hex: :cowboy, repo: "hexpm", optional: true]}, {:mime, "~> 1.0", [hex: :mime, repo: "hexpm", optional: false]}], "hexpm"},
"poison": {:hex, :poison, "3.1.0", "d9eb636610e096f86f25d9a46f35a9facac35609a7591b3be3326e99a0484665", [:mix], [], "hexpm"},
}
6 changes: 3 additions & 3 deletions test/lib/absinthe/graphiql_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ defmodule Absinthe.Plug.GraphiQLTest do
header_json = [
%{"name" => "Authorization", "value" => "Basic Zm9vOmJhcg=="},
%{"name" => "X-CSRF-Token", "value" => "foobarbaz"}
] |> Poison.encode!(pretty: true)
] |> Jason.encode!(pretty: true)

assert body |> String.contains?("defaultHeaders: " <> header_json)
end
Expand All @@ -54,7 +54,7 @@ defmodule Absinthe.Plug.GraphiQLTest do

header_json = [
%{"name" => "Authorization", "value" => "Bearer Zm9vOmJhcg=="}
] |> Poison.encode!(pretty: true)
] |> Jason.encode!(pretty: true)

assert body |> String.contains?("defaultHeaders: " <> header_json)
end
Expand Down Expand Up @@ -170,7 +170,7 @@ defmodule Absinthe.Plug.GraphiQLTest do
opts = Plug.Parsers.init(
parsers: [:urlencoded, :multipart, :json],
pass: ["*/*"],
json_decoder: Poison
json_decoder: Jason
)
Plug.Parsers.call(conn, opts)
end
Expand Down
2 changes: 1 addition & 1 deletion test/lib/absinthe/plug/document_provider/compiled_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ defmodule Absinthe.Plug.DocumentProvider.CompiledTest do
@fixture Path.join([File.cwd!, "test/support/fixtures/extracted_queries.json"])

provide File.read!(@fixture)
|> Poison.decode!
|> Jason.decode!
|> Map.new(fn {k, v} -> {v, k} end)

end
Expand Down
62 changes: 10 additions & 52 deletions test/lib/absinthe/plug/transport_batching_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -116,25 +116,11 @@ defmodule Absinthe.Plug.TransportBatchingTest do
@fragment_query """
[{
"id": "1",
"query": "query Q {
item(id: \\"foo\\") {
...Named
}
}
fragment Named on Item {
name
}",
"query": "query Q { item(id: \\"foo\\") { ...Named } } fragment Named on Item { name }",
"variables": {}
}, {
"id": "2",
"query": "query P {
item(id: \\"bar\\") {
...Named
}
}
fragment Named on Item {
name
}",
"query": "query P { item(id: \\"bar\\") { ...Named } } fragment Named on Item { name }",
"variables": {}
}]
"""
Expand All @@ -153,33 +139,19 @@ defmodule Absinthe.Plug.TransportBatchingTest do
@fragment_query_with_undefined_field """
[{
"id": "1",
"query": "query Q {
item(id: \\"foo\\") {
...Named
}
}
fragment Named on Item {
name
}",
"query": "query Q { item(id: \\"foo\\") { ...Named } } fragment Named on Item { name }",
"variables": {}
}, {
"id": "2",
"query": "query P {
item(id: \\"foo\\") {
...Named
}
}
fragment Named on Item {
namep
}",
"query": "query P { item(id: \\"foo\\") { ...Named } } fragment Named on Item { namep }",
"variables": {}
}]
"""
@fragment_query_with_undefined_field_result [
%{"id" => "1", "payload" => %{"data" => %{"item" => %{"name" => "Foo"}}}},
%{"id" => "2", "payload" => %{"errors" => [
%{
"locations" => [%{"column" => 0, "line" => 7}],
"locations" => [%{"column" => 0, "line" => 1}],
"message" => "Cannot query field \"namep\" on type \"Item\". Did you mean \"name\"?"
}
]}}
Expand All @@ -199,25 +171,11 @@ defmodule Absinthe.Plug.TransportBatchingTest do
@fragment_query_with_undefined_variable """
[{
"id": "1",
"query": "query Q {
item(id: \\"foo\\") {
...Named
}
}
fragment Named on Item {
name
}",
"query": "query Q { item(id: \\"foo\\") { ...Named } } fragment Named on Item { name }",
"variables": {}
}, {
"id": "2",
"query": "query P($id: ID!) {
item(id: $id) {
...Named
}
}
fragment Named on Item {
name
}",
"query": "query P($id: ID!) { item(id: $id) { ...Named } } fragment Named on Item { name }",
"variables": {"idx": "foo"}
}]
"""
Expand All @@ -226,7 +184,7 @@ defmodule Absinthe.Plug.TransportBatchingTest do
"payload" => %{"data" => %{"item" => %{"name" => "Foo"}}}},
%{"id" => "2",
"payload" => %{"errors" => [%{"locations" => [%{"column" => 0,
"line" => 2}],
"line" => 1}],
"message" => "In argument \"id\": Expected type \"ID!\", found null."}, %{"locations" => [%{"column" => 0, "line" => 1}], "message" => "Variable \"id\": Expected non-null, found null."}]}}]

test "can include fragments with undefined variable" do
Expand Down Expand Up @@ -350,7 +308,7 @@ defmodule Absinthe.Plug.TransportBatchingTest do
query: "query Upload($file: Upload) {uploadTest(fileA: $file)}",
variables: %{"file" => "a"},
},
] |> Poison.encode!
] |> Jason.encode!

test "single batched query in relay-network-layer format works with variables and uploads" do
opts = Absinthe.Plug.init(schema: TestSchema)
Expand Down Expand Up @@ -405,7 +363,7 @@ defmodule Absinthe.Plug.TransportBatchingTest do
opts = Map.put(opts, :before_send, {__MODULE__, :test_before_send})
%{resp_body: body} = conn = Absinthe.Plug.call(conn, opts)

case Poison.decode(body) do
case Jason.decode(body) do
{:ok, parsed} -> %{conn | resp_body: parsed}
_ -> conn
end
Expand Down
24 changes: 12 additions & 12 deletions test/lib/absinthe/plug_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ defmodule Absinthe.PlugTest do
test "content-type application/json works" do
opts = Absinthe.Plug.init(schema: TestSchema)

assert %{status: 200, resp_body: resp_body} = conn(:post, "/", Poison.encode!(%{query: @query}))
assert %{status: 200, resp_body: resp_body} = conn(:post, "/", Jason.encode!(%{query: @query}))
|> put_req_header("content-type", "application/json")
|> plug_parser
|> Absinthe.Plug.call(opts)
Expand All @@ -88,7 +88,7 @@ defmodule Absinthe.PlugTest do
test "content-type application/json works with variables" do
opts = Absinthe.Plug.init(schema: TestSchema)

assert %{status: 200, resp_body: resp_body} = conn(:post, "/", Poison.encode!(%{query: @variable_query, variables: %{id: "foo"}}))
assert %{status: 200, resp_body: resp_body} = conn(:post, "/", Jason.encode!(%{query: @variable_query, variables: %{id: "foo"}}))
|> put_req_header("content-type", "application/json")
|> plug_parser
|> Absinthe.Plug.call(opts)
Expand All @@ -99,7 +99,7 @@ defmodule Absinthe.PlugTest do
test "content-type application/json works with empty operation name" do
opts = Absinthe.Plug.init(schema: TestSchema)

assert %{status: 200, resp_body: resp_body} = conn(:post, "/", Poison.encode!(%{query: @query, operationName: ""}))
assert %{status: 200, resp_body: resp_body} = conn(:post, "/", Jason.encode!(%{query: @query, operationName: ""}))
|> put_req_header("content-type", "application/json")
|> plug_parser
|> Absinthe.Plug.call(opts)
Expand All @@ -124,7 +124,7 @@ defmodule Absinthe.PlugTest do
|> Absinthe.Plug.call(opts)

message = "Can only perform a mutation from a POST request"
assert %{"errors" => [%{"message" => ^message}]} = resp_body |> Poison.decode!
assert %{"errors" => [%{"message" => ^message}]} = resp_body |> Jason.decode!
end

@query """
Expand All @@ -144,7 +144,7 @@ defmodule Absinthe.PlugTest do
|> Absinthe.Plug.call(opts)

message = opts[:no_query_message]
assert %{"errors" => [%{"message" => ^message}]} = resp_body |> Poison.decode!
assert %{"errors" => [%{"message" => ^message}]} = resp_body |> Jason.decode!
end

test "document with error returns validation errors" do
Expand All @@ -155,7 +155,7 @@ defmodule Absinthe.PlugTest do
|> plug_parser
|> Absinthe.Plug.call(opts)

assert %{"errors" => [%{"message" => _}]} = resp_body |> Poison.decode!
assert %{"errors" => [%{"message" => _}]} = resp_body |> Jason.decode!
end

@complex_query """
Expand All @@ -172,7 +172,7 @@ defmodule Absinthe.PlugTest do
|> plug_parser
|> Absinthe.Plug.call(opts)

assert %{"errors" => [%{"message" => "Field complex is too complex" <> _} | _]} = resp_body |> Poison.decode!
assert %{"errors" => [%{"message" => "Field complex is too complex" <> _} | _]} = resp_body |> Jason.decode!
end

@query """
Expand All @@ -187,8 +187,8 @@ defmodule Absinthe.PlugTest do

double_encoded_query =
%{query: @query}
|> Poison.encode!()
|> Poison.encode!()
|> Jason.encode!()
|> Jason.encode!()

assert %{status: 400} = conn(:post, "/", double_encoded_query)
|> put_req_header("content-type", "application/json")
Expand Down Expand Up @@ -258,7 +258,7 @@ defmodule Absinthe.PlugTest do

query = "{field_on_root_value}"

assert %{status: 200, resp_body: resp_body} = conn(:post, "/", Poison.encode!(%{query: query, operationName: ""}))
assert %{status: 200, resp_body: resp_body} = conn(:post, "/", Jason.encode!(%{query: query, operationName: ""}))
|> put_req_header("content-type", "application/json")
|> plug_parser
|> Absinthe.Plug.put_options(root_value: %{field_on_root_value: "foo"})
Expand Down Expand Up @@ -304,7 +304,7 @@ defmodule Absinthe.PlugTest do
"""

upload = %Plug.Upload{}
variables = Poison.encode!(%{auth: "foo"})
variables = Jason.encode!(%{auth: "foo"})
assert %{status: 200, resp_body: resp_body} = conn(:post, "/", %{"query" => query, "a" => upload, "b" => upload, "variables" => variables})
|> put_req_header("content-type", "multipart/form-data")
|> call(opts)
Expand Down Expand Up @@ -398,7 +398,7 @@ defmodule Absinthe.PlugTest do
events =
state.chunks
|> String.split
|> Enum.map(&Poison.decode!/1)
|> Enum.map(&Jason.decode!/1)

assert length(events) == 2
assert Enum.member?(events, %{"data" => %{"update" => "FOO"}})
Expand Down
4 changes: 2 additions & 2 deletions test/support/test_case.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ defmodule Absinthe.Plug.TestCase do
conn
|> plug_parser
|> Absinthe.Plug.call(opts)
|> Map.update!(:resp_body, &Poison.decode!/1)
|> Map.update!(:resp_body, &Jason.decode!/1)
end

def plug_parser(conn) do
opts = Plug.Parsers.init(
parsers: [:urlencoded, :multipart, :json, Absinthe.Plug.Parser],
json_decoder: Poison
json_decoder: Jason
)
Plug.Parsers.call(conn, opts)
end
Expand Down