graphql-relay 0.4.5 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/graphql/relay/connection_field.rb +1 -1
- data/lib/graphql/relay/global_node_identification.rb +8 -2
- data/lib/graphql/relay/mutation.rb +1 -1
- data/lib/graphql/relay/relation_connection.rb +4 -2
- data/lib/graphql/relay/version.rb +1 -1
- data/spec/graphql/relay/global_node_identification_spec.rb +20 -2
- data/spec/graphql/relay/relation_connection_spec.rb +33 -11
- data/spec/support/star_wars_schema.rb +8 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a7bcced67ab9b1c147555d570968aab04fa4d695
|
4
|
+
data.tar.gz: 585864fd46e046a9b2566e0392732634da32e215
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7048333381510bd9a4af3232e3c794cde527541ae7d6760e9bc4d46ce19773d74b905585651704138ee35be5ab58420b22c0784dc51225cbf8db53a3bb6cb163
|
7
|
+
data.tar.gz: 7c47839af39a5e4916496ef15259d7d9ba1b3098e3fa606d63cbe1c307fbec42ed027904709bf5e0241b4843cf78a01a26de887ee55d1d64327f3aaf3c9916ee
|
@@ -29,7 +29,7 @@ module GraphQL
|
|
29
29
|
# @param [GraphQL::Field] A field which returns items to be wrapped as a connection
|
30
30
|
# @return [GraphQL::Field] A field which serves a connections
|
31
31
|
def self.create(underlying_field)
|
32
|
-
underlying_field.arguments = underlying_field.arguments.
|
32
|
+
underlying_field.arguments = underlying_field.arguments.reverse_merge(DEFAULT_ARGUMENTS)
|
33
33
|
# TODO: make a public API on GraphQL::Field to expose this proc
|
34
34
|
original_resolve = underlying_field.instance_variable_get(:@resolve_proc)
|
35
35
|
underlying_field.resolve = get_connection_resolve(underlying_field.name, original_resolve)
|
@@ -11,7 +11,7 @@ module GraphQL
|
|
11
11
|
attr_accessor :object_from_id_proc, :type_from_object_proc
|
12
12
|
|
13
13
|
class << self
|
14
|
-
|
14
|
+
attr_accessor :instance
|
15
15
|
def new(*args, &block)
|
16
16
|
@instance = super
|
17
17
|
end
|
@@ -66,7 +66,13 @@ module GraphQL
|
|
66
66
|
# Use the provided config to
|
67
67
|
# get a type for a given object
|
68
68
|
def type_from_object(object)
|
69
|
-
@type_from_object_proc.call(object)
|
69
|
+
type_result = @type_from_object_proc.call(object)
|
70
|
+
if !type_result.is_a?(GraphQL::BaseType)
|
71
|
+
type_str = "#{type_result} (#{type_result.class.name})"
|
72
|
+
raise "type_from_object(#{object}) returned #{type_str}, but it should return a GraphQL type"
|
73
|
+
else
|
74
|
+
type_result
|
75
|
+
end
|
70
76
|
end
|
71
77
|
|
72
78
|
# Use the provided config to
|
@@ -1,6 +1,8 @@
|
|
1
1
|
module GraphQL
|
2
2
|
module Relay
|
3
3
|
class RelationConnection < BaseConnection
|
4
|
+
DEFAULT_ORDER = "id"
|
5
|
+
|
4
6
|
def cursor_from_node(item)
|
5
7
|
order_value = item.public_send(order_name)
|
6
8
|
cursor_parts = [order, order_value]
|
@@ -8,7 +10,7 @@ module GraphQL
|
|
8
10
|
end
|
9
11
|
|
10
12
|
def order
|
11
|
-
@order ||= (super ||
|
13
|
+
@order ||= (super || DEFAULT_ORDER)
|
12
14
|
end
|
13
15
|
|
14
16
|
private
|
@@ -64,7 +66,7 @@ module GraphQL
|
|
64
66
|
def order_direction
|
65
67
|
@order_direction ||= order.start_with?("-") ? :desc : :asc
|
66
68
|
end
|
67
|
-
|
69
|
+
|
68
70
|
def table_name
|
69
71
|
@table_name ||= object.table.table_name
|
70
72
|
end
|
@@ -47,12 +47,30 @@ describe GraphQL::Relay::GlobalNodeIdentification do
|
|
47
47
|
end
|
48
48
|
end
|
49
49
|
|
50
|
+
describe "type_from_object" do
|
51
|
+
describe "when the return value is not a BaseType" do
|
52
|
+
it "raises an error " do
|
53
|
+
err = assert_raises {
|
54
|
+
GraphQL::Relay::GlobalNodeIdentification.instance.type_from_object(:test_error)
|
55
|
+
}
|
56
|
+
assert_includes err.message, "not_a_type (Symbol)"
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
50
61
|
describe 'making a second instance' do
|
62
|
+
before do
|
63
|
+
@first_instance = GraphQL::Relay::GlobalNodeIdentification.instance
|
64
|
+
end
|
65
|
+
|
66
|
+
after do
|
67
|
+
GraphQL::Relay::GlobalNodeIdentification.instance = @first_instance
|
68
|
+
end
|
69
|
+
|
51
70
|
it 'overrides the first instance' do
|
52
|
-
first_instance = GraphQL::Relay::GlobalNodeIdentification.instance
|
53
71
|
GraphQL::Relay::GlobalNodeIdentification.define {}
|
54
72
|
second_instance = GraphQL::Relay::GlobalNodeIdentification.instance
|
55
|
-
refute_equal(first_instance, second_instance)
|
73
|
+
refute_equal(@first_instance, second_instance)
|
56
74
|
end
|
57
75
|
end
|
58
76
|
end
|
@@ -65,17 +65,6 @@ describe GraphQL::Relay::RelationConnection do
|
|
65
65
|
assert_equal(["Death Star", "Shield Generator"], get_names(result))
|
66
66
|
end
|
67
67
|
|
68
|
-
it 'paginates with order' do
|
69
|
-
result = query(query_string, "first" => 2, "order" => "name")
|
70
|
-
assert_equal(["Death Star", "Headquarters"], get_names(result))
|
71
|
-
|
72
|
-
# After the last result, find the next 2:
|
73
|
-
last_cursor = get_last_cursor(result)
|
74
|
-
|
75
|
-
result = query(query_string, "after" => last_cursor, "first" => 2, "order" => "name")
|
76
|
-
assert_equal(["Shield Generator"], get_names(result))
|
77
|
-
end
|
78
|
-
|
79
68
|
it 'paginates with reverse order' do
|
80
69
|
result = query(query_string, "first" => 2, "order" => "-name")
|
81
70
|
assert_equal(["Shield Generator", "Headquarters"], get_names(result))
|
@@ -126,4 +115,37 @@ describe GraphQL::Relay::RelationConnection do
|
|
126
115
|
assert_equal(3, bases.length)
|
127
116
|
end
|
128
117
|
end
|
118
|
+
|
119
|
+
describe "overriding default order" do
|
120
|
+
let(:query_string) {%|
|
121
|
+
query getBases {
|
122
|
+
empire {
|
123
|
+
basesByName { ... basesFields }
|
124
|
+
bases { ... basesFields }
|
125
|
+
}
|
126
|
+
}
|
127
|
+
fragment basesFields on BaseConnection {
|
128
|
+
edges {
|
129
|
+
node {
|
130
|
+
name
|
131
|
+
}
|
132
|
+
}
|
133
|
+
}
|
134
|
+
|}
|
135
|
+
|
136
|
+
def get_names(result, field_name)
|
137
|
+
bases = result["data"]["empire"][field_name]["edges"]
|
138
|
+
base_names = bases.map { |b| b["node"]["name"] }
|
139
|
+
end
|
140
|
+
|
141
|
+
it "applies the default value" do
|
142
|
+
result = query(query_string)
|
143
|
+
|
144
|
+
bases_by_id = ["Death Star", "Shield Generator", "Headquarters"]
|
145
|
+
bases_by_name = ["Death Star", "Headquarters", "Shield Generator"]
|
146
|
+
|
147
|
+
assert_equal(bases_by_id, get_names(result, "bases"))
|
148
|
+
assert_equal(bases_by_name, get_names(result, "basesByName"))
|
149
|
+
end
|
150
|
+
end
|
129
151
|
end
|
@@ -13,7 +13,11 @@ NodeIdentification = GraphQL::Relay::GlobalNodeIdentification.define do
|
|
13
13
|
end
|
14
14
|
|
15
15
|
type_from_object -> (object) do
|
16
|
-
|
16
|
+
if object == :test_error
|
17
|
+
:not_a_type
|
18
|
+
else
|
19
|
+
STAR_WARS_DATA["Faction"].values.include?(object) ? Faction : Ship
|
20
|
+
end
|
17
21
|
end
|
18
22
|
end
|
19
23
|
|
@@ -75,6 +79,9 @@ Faction = GraphQL::ObjectType.define do
|
|
75
79
|
end
|
76
80
|
|
77
81
|
connection :basesClone, BaseConnection
|
82
|
+
connection :basesByName, BaseConnection, property: :bases do
|
83
|
+
argument :order, types.String, default_value: "name"
|
84
|
+
end
|
78
85
|
end
|
79
86
|
|
80
87
|
# Define a mutation. It will also:
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: graphql-relay
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Robert Mosolgo
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-11-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: graphql
|