Skip to content
This repository was archived by the owner on Jul 25, 2024. It is now read-only.

Commit db5c8f7

Browse files
author
Josh Price
committed
Merge pull request #26 from aweiker/code-cleanup
Code cleanup
2 parents 901aee0 + 31b6062 commit db5c8f7

File tree

8 files changed

+151
-52
lines changed

8 files changed

+151
-52
lines changed

config/.credo.exs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# This file contains the configuration for Credo.
2+
#
3+
# If you find anything wrong or unclear in this file, please report an
4+
# issue on GitHub: https://github.com/rrrene/credo/issues
5+
%{
6+
#
7+
# You can have as many configs as you like in the `configs:` field.
8+
configs: [
9+
%{
10+
#
11+
# Run any config using `mix credo -C <name>`. If no config name is given
12+
# "default" is used.
13+
name: "default",
14+
#
15+
# these are the files included in the analysis
16+
files: %{
17+
#
18+
# you can give explicit globs or simply directories
19+
# in the latter case `**/*.{ex,exs}` will be used
20+
included: ["lib/", "src/", "web/", "apps/"],
21+
excluded: []
22+
},
23+
#
24+
# The `checks:` field contains all the checks that are run. You can
25+
# customize the parameters of any given check by adding a second element
26+
# to the tuple.
27+
#
28+
# There are two ways of deactivating a check:
29+
# 1. deleting the check from this list
30+
# 2. putting `false` as second element (to quickly "comment it out"):
31+
#
32+
# {Credo.Check.Consistency.ExceptionNames, false}
33+
#
34+
checks: [
35+
{Credo.Check.Consistency.ExceptionNames},
36+
{Credo.Check.Consistency.LineEndings},
37+
{Credo.Check.Consistency.SpaceAroundOperators},
38+
{Credo.Check.Consistency.SpaceInParentheses},
39+
{Credo.Check.Consistency.TabsOrSpaces},
40+
41+
# For some checks, like AliasUsage, you can only customize the priority
42+
# Priority values are: `low, normal, high, higher`
43+
{Credo.Check.Design.AliasUsage, priority: :low},
44+
# For others you can set parameters
45+
{Credo.Check.Design.DuplicatedCode, mass_threshold: 16, nodes_threshold: 2},
46+
47+
# You can also customize the exit_status of each check.
48+
# If you don't want TODO comments to cause `mix credo` to fail, just
49+
# set this value to 0 (zero).
50+
{Credo.Check.Design.TagTODO, false},
51+
{Credo.Check.Design.TagFIXME},
52+
53+
{Credo.Check.Readability.FunctionNames},
54+
{Credo.Check.Readability.MaxLineLength, priority: :low, max_length: 120},
55+
{Credo.Check.Readability.ModuleAttributeNames},
56+
{Credo.Check.Readability.ModuleDoc},
57+
{Credo.Check.Readability.ModuleNames},
58+
{Credo.Check.Readability.PredicateFunctionNames},
59+
{Credo.Check.Readability.TrailingBlankLine},
60+
{Credo.Check.Readability.TrailingWhiteSpace},
61+
{Credo.Check.Readability.VariableNames},
62+
63+
{Credo.Check.Refactor.ABCSize},
64+
{Credo.Check.Refactor.CaseTrivialMatches},
65+
{Credo.Check.Refactor.CondStatements},
66+
{Credo.Check.Refactor.FunctionArity},
67+
{Credo.Check.Refactor.MatchInCondition},
68+
{Credo.Check.Refactor.PipeChainStart},
69+
{Credo.Check.Refactor.CyclomaticComplexity},
70+
{Credo.Check.Refactor.NegatedConditionsInUnless},
71+
{Credo.Check.Refactor.NegatedConditionsWithElse},
72+
{Credo.Check.Refactor.Nesting},
73+
{Credo.Check.Refactor.UnlessWithElse},
74+
75+
{Credo.Check.Warning.IExPry},
76+
{Credo.Check.Warning.IoInspect},
77+
{Credo.Check.Warning.NameRedeclarationByAssignment},
78+
{Credo.Check.Warning.NameRedeclarationByCase},
79+
{Credo.Check.Warning.NameRedeclarationByDef},
80+
{Credo.Check.Warning.NameRedeclarationByFn},
81+
{Credo.Check.Warning.OperationOnSameValues},
82+
{Credo.Check.Warning.UnusedEnumOperation},
83+
{Credo.Check.Warning.UnusedKeywordOperation},
84+
{Credo.Check.Warning.UnusedListOperation},
85+
{Credo.Check.Warning.UnusedStringOperation},
86+
{Credo.Check.Warning.UnusedTupleOperation},
87+
{Credo.Check.Warning.OperationWithConstantResult},
88+
]
89+
}
90+
]
91+
}

config/dogma.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ config :dogma,
1414
# Override an existing rule configuration
1515
override: [
1616
%Rule.LineLength{max_length: 120},
17+
%Rule.FunctionArity{max: 5}
1718
]

lib/graphql/plug.ex

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,37 +18,40 @@ defmodule GraphQL.Plug do
1818
"""
1919

2020
use Plug.Builder
21+
alias GraphQL.Schema
22+
alias GraphQL.Plug.{GraphiQL, Endpoint}
23+
alias Plug.Parsers
2124

2225
require Logger
2326

24-
plug Plug.Parsers,
27+
plug Parsers,
2528
parsers: [:graphql, :urlencoded, :multipart, :json],
2629
pass: ["*/*"],
2730
json_decoder: Poison
2831

2932
@type init :: %{
30-
schema: GraphQL.Schema.t,
33+
schema: Schema.t,
3134
root_value: ConfigurableValue.t,
3235
query: ConfigurableValue.t,
3336
allow_graphiql?: true | false
3437
}
3538

3639
@spec init(Map) :: init
3740
def init(opts) do
38-
graphiql = GraphQL.Plug.GraphiQL.init(opts)
39-
endpoint = GraphQL.Plug.Endpoint.init(opts)
41+
graphiql = GraphiQL.init(opts)
42+
endpoint = Endpoint.init(opts)
4043

41-
Keyword.merge(graphiql, endpoint)
42-
|> Enum.dedup
44+
opts = Keyword.merge(graphiql, endpoint)
45+
Enum.dedup(opts)
4346
end
4447

4548
def call(conn, opts) do
4649
conn = super(conn, opts)
4750

48-
conn = if GraphQL.Plug.GraphiQL.use_graphiql?(conn, opts) do
49-
GraphQL.Plug.GraphiQL.call(conn, opts)
51+
conn = if GraphiQL.use_graphiql?(conn, opts) do
52+
GraphiQL.call(conn, opts)
5053
else
51-
GraphQL.Plug.Endpoint.call(conn, opts)
54+
Endpoint.call(conn, opts)
5255
end
5356

5457
# TODO consider not logging instrospection queries

lib/graphql/plug/configurable_value.ex

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ defmodule GraphQL.Plug.ConfigurableValue do
99
an arity of 1 accepting a `Plug.Conn`.
1010
"""
1111

12-
@type t :: {module, atom} | (Plug.Conn.t -> Map) | Map | nil
13-
@spec evaluate(Plug.Conn.t, t, any) :: Map
12+
alias Plug.Conn
13+
14+
@type t :: {module, atom} | (Conn.t -> Map) | Map | nil
15+
@spec evaluate(Conn.t, t, any) :: Map
1416

1517
@error_msg "Configured function must only be arity of 1 that accepts a value of Plug.Conn"
1618

lib/graphql/plug/endpoint.ex

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,28 @@ defmodule GraphQL.Plug.Endpoint do
4141
end
4242

4343
def call(%Conn{method: m} = conn, opts) when m in ["GET", "POST"] do
44+
args = extract_arguments(conn, opts)
45+
handle_call(conn, args)
46+
end
47+
48+
def call(%Conn{method: _} = conn, _) do
49+
handle_error(conn, "GraphQL only supports GET and POST requests.")
50+
end
51+
52+
def extract_arguments(conn, opts) do
4453
query = Parameter.query(conn) ||
4554
ConfigurableValue.evaluate(conn, opts[:query], nil)
4655
variables = Parameter.variables(conn)
4756
operation_name = Parameter.operation_name(conn)
4857
root_value = ConfigurableValue.evaluate(conn, opts[:root_value], %{})
4958

50-
cond do
51-
query ->
52-
handle_call(conn, opts[:schema], root_value, query, variables, operation_name)
53-
true ->
54-
handle_error(conn, "Must provide query string.")
55-
end
56-
end
57-
58-
def call(%Conn{method: _} = conn, _) do
59-
handle_error(conn, "GraphQL only supports GET and POST requests.")
59+
%{
60+
query: query,
61+
variables: variables,
62+
operation_name: operation_name,
63+
root_value: root_value,
64+
schema: opts[:schema]
65+
}
6066
end
6167

6268
def handle_error(conn, message) do
@@ -66,14 +72,17 @@ defmodule GraphQL.Plug.Endpoint do
6672
|> send_resp(400, errors)
6773
end
6874

69-
def handle_call(conn, schema, root_value, query, variables, operation_name) do
75+
def handle_call(conn, %{query: nil}) do
76+
handle_error(conn, "Must provide query string.")
77+
end
78+
def handle_call(conn, args) do
7079
conn
7180
|> put_resp_content_type("application/json")
72-
|> execute(schema, root_value, query, variables, operation_name)
81+
|> execute(args)
7382
end
7483

75-
defp execute(conn, schema, root_value, query, variables, operation_name) do
76-
case GraphQL.execute(schema, query, root_value, variables, operation_name) do
84+
defp execute(conn, args) do
85+
case GraphQL.execute(args.schema, args.query, args.root_value, args.variables, args.operation_name) do
7786
{:ok, data} ->
7887
case Poison.encode(data) do
7988
{:ok, json} -> send_resp(conn, 200, json)

lib/graphql/plug/graphiql.ex

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ defmodule GraphQL.Plug.GraphiQL do
1616
import Plug.Conn
1717
alias Plug.Conn
1818
alias GraphQL.Plug.Endpoint
19-
alias GraphQL.Plug.ConfigurableValue
20-
alias GraphQL.Plug.Parameter
2119

2220
@behaviour Plug
2321

@@ -47,23 +45,16 @@ defmodule GraphQL.Plug.GraphiQL do
4745
def init(opts) do
4846
allow_graphiql? = Keyword.get(opts, :allow_graphiql?, true)
4947

50-
GraphQL.Plug.Endpoint.init(opts) ++ [allow_graphiql?: allow_graphiql?]
48+
Endpoint.init(opts) ++ [allow_graphiql?: allow_graphiql?]
5149
end
5250

5351
def call(%Conn{method: m} = conn, opts) when m in ["GET", "POST"] do
54-
query = Parameter.query(conn) ||
55-
ConfigurableValue.evaluate(conn, opts[:query], nil)
56-
variables = Parameter.variables(conn)
57-
operation_name = Parameter.operation_name(conn)
58-
root_value = ConfigurableValue.evaluate(conn, opts[:root_value], %{})
59-
60-
cond do
61-
use_graphiql?(conn, opts) ->
62-
handle_graphiql_call(conn, opts[:schema], root_value, query, variables, operation_name)
63-
query ->
64-
Endpoint.handle_call(conn, opts[:schema], root_value, query, variables, operation_name)
65-
true ->
66-
Endpoint.handle_error(conn, "Must provide query string.")
52+
args = Endpoint.extract_arguments(conn, opts)
53+
54+
if use_graphiql?(conn, opts) do
55+
handle_call(conn, args)
56+
else
57+
Endpoint.handle_call(conn, args)
6758
end
6859
end
6960

@@ -77,12 +68,12 @@ defmodule GraphQL.Plug.GraphiQL do
7768
|> String.replace(~r/'/, "\\'")
7869
end
7970

80-
defp handle_graphiql_call(conn, schema, root_value, query, variables, operation_name) do
71+
defp handle_call(conn, args) do
8172
# TODO construct a simple query from the schema (ie `schema.query.fields[0].fields[0..5]`)
82-
query = query || @graphiql_instructions <> "\n{\n\tfield\n}\n"
73+
query = args.query || @graphiql_instructions <> "\n{\n\tfield\n}\n"
8374

84-
{_, data} = GraphQL.execute(schema, query, root_value, variables, operation_name)
85-
{:ok, variables} = Poison.encode(variables, pretty: true)
75+
{_, data} = GraphQL.execute(args.schema, query, args.root_value, args.variables, args.operation_name)
76+
{:ok, variables} = Poison.encode(args.variables, pretty: true)
8677
{:ok, result} = Poison.encode(data, pretty: true)
8778

8879
graphiql = graphiql_html(@graphiql_version, escape_string(query), escape_string(variables), escape_string(result))

lib/graphql/plug/parameter.ex

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,23 @@ defmodule GraphQL.Plug.Parameter do
44
value from `Plug.Conn`
55
"""
66

7-
@spec operation_name(Plug.Conn.t) :: String.t
7+
alias Plug.Conn
8+
9+
@spec operation_name(Conn.t) :: String.t
810
def operation_name(conn) do
911
conn
1012
|> operation_name_params
1113
|> cleanup_string
1214
end
1315

14-
@spec query(Plug.Conn.t) :: String.t | nil
16+
@spec query(Conn.t) :: String.t | nil
1517
def query(conn) do
1618
conn.params
1719
|> Map.get("query")
1820
|> cleanup_string
1921
end
2022

21-
@spec variables(Plug.Conn.t) :: Map
23+
@spec variables(Conn.t) :: Map
2224
def variables(conn) do
2325
decode_variables(Map.get(conn.params, "variables", %{}))
2426
end
@@ -32,9 +34,9 @@ defmodule GraphQL.Plug.Parameter do
3234
Map.get(conn.params, "operation_name")
3335
end
3436

35-
defp decode_variables(variables) when is_binary(variables) do
36-
case Poison.decode(variables) do
37-
{:ok, variables} -> variables
37+
defp decode_variables(values) when is_binary(values) do
38+
case Poison.decode(values) do
39+
{:ok, values} -> values
3840
{:error, _} -> %{} # express-graphql ignores these errors currently
3941
end
4042
end

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ defmodule GraphQL.Plug.Mixfile do
2828
{:dogma, "~> 0.1", only: :dev},
2929
{:earmark, "~> 0.1", only: :dev},
3030
{:ex_doc, "~> 0.11", only: :dev},
31-
{:mix_test_watch, only: :dev},
31+
{:mix_test_watch, "~> 0.2", only: :dev},
3232
{:cowboy, "~> 1.0"},
3333
{:plug, "~> 0.14 or ~> 1.0"},
3434
{:poison, "~> 1.5 or ~> 2.0", override: true},

0 commit comments

Comments
 (0)