Skip to content

Commit 44e6912

Browse files
author
Josh Price
authored
Fix Elixir 1.3 warnings (#21)
* Fix Elixir 1.3 warnings * Made some other small refactorings for clarity Tests are still passing but not sure if every case was covered. * Depipeline single function calls * Add a note about `after` keyword * Refactoring for clarity
1 parent 6d1b724 commit 44e6912

File tree

3 files changed

+56
-78
lines changed

3 files changed

+56
-78
lines changed

lib/graphql/relay/connection/ecto.ex

+31-41
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,25 @@ if Code.ensure_loaded?(Ecto) do
1111

1212
def resolve(query, %{repo: repo} = args) do
1313
before = cursor_to_offset(args[:before])
14+
# `after` is a keyword http://elixir-lang.org/docs/master/elixir/Kernel.SpecialForms.html#try/1
1415
a_after = cursor_to_offset(args[:after])
1516
first = args[:first]
1617
last = args[:last]
1718
limit = Enum.min([first, last, connection_count(repo, query)])
1819

19-
if a_after do
20-
query = from things in query, where: things.id > ^a_after
20+
query = if a_after do
21+
from things in query, where: things.id > ^a_after
22+
else
23+
query
2124
end
2225

23-
if before do
24-
query = from things in query, where: things.id < ^before
26+
query = if before do
27+
from things in query, where: things.id < ^before
28+
else
29+
query
2530
end
2631

27-
# Calculate has_next_page/has_prev_page before order_by to avoid group_by
28-
# requirement
32+
# Calculate has_next_page/has_prev_page before order_by to avoid group_by requirement
2933
has_next_page = case first do
3034
nil -> false
3135
_ ->
@@ -44,16 +48,16 @@ if Code.ensure_loaded?(Ecto) do
4448
repo.one(has_prev_records_query) > last
4549
end
4650

47-
if first do
48-
query = from things in query, order_by: [asc: things.id], limit: ^limit
51+
query = if first do
52+
from things in query, order_by: [asc: things.id], limit: ^limit
4953
else
50-
has_next_page = false
54+
query
5155
end
5256

53-
if last do
54-
query = from things in query, order_by: [desc: things.id], limit: ^limit
57+
query = if last do
58+
from things in query, order_by: [desc: things.id], limit: ^limit
5559
else
56-
has_prev_page = false
60+
query
5761
end
5862

5963
records = repo.all(query)
@@ -66,10 +70,8 @@ if Code.ensure_loaded?(Ecto) do
6670
end)
6771

6872
edges = case last do
69-
nil ->
70-
edges
71-
_ ->
72-
Enum.reverse(edges)
73+
nil -> edges
74+
_ -> Enum.reverse(edges)
7375
end
7476

7577
first_edge = List.first(edges)
@@ -78,34 +80,22 @@ if Code.ensure_loaded?(Ecto) do
7880
%{
7981
edges: edges,
8082
pageInfo: %{
81-
startCursor: first_edge && Map.get(first_edge, :cursor) || nil,
82-
endCursor: last_edge && Map.get(last_edge, :cursor) || nil,
83+
startCursor: first_edge && Map.get(first_edge, :cursor),
84+
endCursor: last_edge && Map.get(last_edge, :cursor),
8385
hasPreviousPage: has_prev_page,
8486
hasNextPage: has_next_page
8587
}
8688
}
8789
end
8890

89-
def get_offset_with_default(cursor, default_offset) do
90-
unless cursor do
91-
default_offset
92-
else
93-
offset = cursor_to_offset(cursor)
94-
offset || default_offset
95-
end
96-
end
97-
91+
def cursor_to_offset(nil), do: nil
9892
def cursor_to_offset(cursor) do
99-
case cursor do
100-
nil -> nil
101-
_ ->
102-
case Base.decode64(cursor) do
103-
{:ok, decoded_cursor} ->
104-
{int, _} = Integer.parse(String.slice(decoded_cursor, String.length(@prefix)..String.length(decoded_cursor)))
105-
int
106-
:error ->
107-
nil
108-
end
93+
case Base.decode64(cursor) do
94+
{:ok, decoded_cursor} ->
95+
{int, _} = Integer.parse(String.slice(decoded_cursor, String.length(@prefix)..String.length(decoded_cursor)))
96+
int
97+
:error ->
98+
nil
10999
end
110100
end
111101

@@ -121,20 +111,20 @@ if Code.ensure_loaded?(Ecto) do
121111

122112
defp make_query_countable(query) do
123113
query
124-
|> remove_select
125-
|> remove_order
114+
|> remove_select
115+
|> remove_order
126116
end
127117

128118
# Remove select if it exists so that we avoid `only one select
129119
# expression is allowed in query` Ecto exception
130120
defp remove_select(query) do
131-
query |> Ecto.Query.exclude(:select)
121+
Ecto.Query.exclude(query, :select)
132122
end
133123

134124
# Remove order by if it exists so that we avoid `field X in "order_by"
135125
# does not exist in the model source in query`
136126
defp remove_order(query) do
137-
query |> Ecto.Query.exclude(:order_by)
127+
Ecto.Query.exclude(query, :order_by)
138128
end
139129
end
140130
end

lib/graphql/relay/connection/list.ex

+23-30
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
defmodule GraphQL.Relay.Connection.List do
22
@prefix "arrayconnection:"
33

4-
def resolve(data) do
5-
resolve(data, %{})
6-
end
4+
def resolve(data), do: resolve(data, %{})
75
def resolve(data, args) do
86
resolve_slice(data, args, %{
97
slice_start: 0,
@@ -13,34 +11,40 @@ defmodule GraphQL.Relay.Connection.List do
1311

1412
def resolve_slice(records, args, meta) do
1513
before = args[:before]
14+
# `after` is a keyword http://elixir-lang.org/docs/master/elixir/Kernel.SpecialForms.html#try/1
1615
a_after = args[:after]
1716
first = args[:first]
1817
last = args[:last]
1918
slice_start = meta[:slice_start] || 0
2019
list_length = meta[:list_length] || length(records)
2120
slice_end = slice_start + length(records)
22-
before_offset = get_offset_with_default(before, list_length)
23-
after_offset = get_offset_with_default(a_after, -1)
21+
before_offset = cursor_to_offset(before, list_length)
22+
after_offset = cursor_to_offset(a_after, -1)
2423
start_offset = Enum.max([slice_start - 1, after_offset, -1]) + 1
2524
end_offset = Enum.min([slice_end, before_offset, list_length])
2625

27-
if first do
28-
end_offset = Enum.min([end_offset, start_offset + first])
26+
end_offset = if first do
27+
Enum.min([end_offset, start_offset + first])
28+
else
29+
end_offset
2930
end
3031

31-
if last do
32-
start_offset = Enum.max([start_offset, end_offset - last])
32+
start_offset = if last do
33+
Enum.max([start_offset, end_offset - last])
34+
else
35+
start_offset
3336
end
3437

3538
from_slice = Enum.max([start_offset - slice_start, 0])
3639
to_slice = length(records) - (slice_end - end_offset) - 1
3740
slice = case first do
3841
0 -> []
39-
_ ->
40-
Enum.slice(records, from_slice..to_slice)
42+
_ -> Enum.slice(records, from_slice..to_slice)
4143
end
4244

43-
{edges, _count} = Enum.map_reduce(slice, 0, fn(record, acc) -> {%{ cursor: offset_to_cursor(start_offset+acc), node: record }, acc + 1} end)
45+
{edges, _count} = Enum.map_reduce(slice, 0, fn(record, acc) ->
46+
{%{cursor: offset_to_cursor(start_offset+acc), node: record}, acc + 1}
47+
end)
4448

4549
first_edge = List.first(edges)
4650
last_edge = List.last(edges)
@@ -50,42 +54,31 @@ defmodule GraphQL.Relay.Connection.List do
5054
%{
5155
edges: edges,
5256
pageInfo: %{
53-
startCursor: first_edge && Map.get(first_edge, :cursor) || nil,
54-
endCursor: last_edge && Map.get(last_edge, :cursor) || nil,
57+
startCursor: first_edge && Map.get(first_edge, :cursor),
58+
endCursor: last_edge && Map.get(last_edge, :cursor),
5559
hasPreviousPage: last && (start_offset > lower_bound) || false,
5660
hasNextPage: first && (end_offset < upper_bound) || false
5761
}
5862
}
5963
end
6064

61-
def get_offset_with_default(cursor, default_offset) do
62-
unless cursor do
63-
default_offset
64-
else
65-
offset = cursor_to_offset(cursor)
66-
offset || default_offset
67-
end
68-
end
69-
70-
def cursor_to_offset(cursor) do
65+
def cursor_to_offset(nil, default), do: default
66+
def cursor_to_offset(cursor, default) do
7167
case Base.decode64(cursor) do
7268
{:ok, decoded_cursor} ->
7369
{int, _} = Integer.parse(String.slice(decoded_cursor, String.length(@prefix)..String.length(decoded_cursor)))
7470
int
7571
:error ->
76-
nil
72+
default
7773
end
7874
end
7975

8076
def cursor_for_object_in_connection(data, object) do
8177
offset = Enum.find_index(data, fn(obj) -> object == obj end)
82-
unless offset do
83-
nil
84-
else
85-
offset_to_cursor(offset)
86-
end
78+
offset_to_cursor(offset)
8779
end
8880

81+
def offset_to_cursor(nil), do: nil
8982
def offset_to_cursor(offset) do
9083
Base.encode64("#{@prefix}#{offset}")
9184
end

lib/graphql_relay.ex

+2-7
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,8 @@ defmodule GraphQL.Relay do
66
"""
77

88
@spec resolve_maybe_thunk(fun | map) :: %{}
9-
def resolve_maybe_thunk(thing_or_thunk) do
10-
if Kernel.is_function(thing_or_thunk) do
11-
thing_or_thunk.()
12-
else
13-
thing_or_thunk
14-
end
15-
end
9+
def resolve_maybe_thunk(t) when is_function(t), do: t.()
10+
def resolve_maybe_thunk(t), do: t
1611

1712
def generate_schema_json! do
1813
Logger.debug "Updating GraphQL schema.json"

0 commit comments

Comments
 (0)