graphql-relay 0.5.0 → 0.5.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/graphql/relay/base_connection.rb +2 -2
- data/lib/graphql/relay/connection_field.rb +1 -1
- data/lib/graphql/relay/global_node_identification.rb +10 -2
- data/lib/graphql/relay/monkey_patches/base_type.rb +8 -1
- data/lib/graphql/relay/version.rb +1 -1
- data/spec/graphql/relay/global_node_identification_spec.rb +31 -1
- data/spec/support/star_wars_schema.rb +4 -4
- 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: 00f40fa848587c7ac0a9c6ad65500b3b22df5d6a
|
4
|
+
data.tar.gz: ebeaca7d3907982c6018080a7912a8586a648f32
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4a4c357825ab7bc6b60b9f1d32f673b1ddd1fbb40b4ce1831323937b5a3af7f6ace82536795aff425f5938d37a586f2333cc922875fdb2db471a362e4f84330d
|
7
|
+
data.tar.gz: 9df15d45a7005ec04d36f4352e4bd2bca0fdefbb5410291929d3d1a36adc0bb9cb93cf8b8a36ff8e69699d9e4094736e2acd4d113f052c9175d5cd03b9dc7707
|
@@ -2,8 +2,8 @@ module GraphQL
|
|
2
2
|
module Relay
|
3
3
|
# Subclasses must implement:
|
4
4
|
# - {#cursor_from_node}, which returns an opaque cursor for the given item
|
5
|
-
# - {#
|
6
|
-
# - {#
|
5
|
+
# - {#sliced_nodes}, which slices by `before` & `after`
|
6
|
+
# - {#paged_nodes}, which applies `first` & `last` limits
|
7
7
|
#
|
8
8
|
# In a subclass, you have access to
|
9
9
|
# - {#object}, the object which the connection will wrap
|
@@ -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 = DEFAULT_ARGUMENTS.merge(underlying_field.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)
|
@@ -6,6 +6,11 @@ module GraphQL
|
|
6
6
|
# GlobalIdField depends on that, since it calls class methods
|
7
7
|
# which delegate to the singleton instance.
|
8
8
|
class GlobalNodeIdentification
|
9
|
+
class << self
|
10
|
+
attr_accessor :id_separator
|
11
|
+
end
|
12
|
+
self.id_separator = "-"
|
13
|
+
|
9
14
|
include GraphQL::DefinitionHelpers::DefinedByConfig
|
10
15
|
defined_by_config :object_from_id_proc, :type_from_object_proc
|
11
16
|
attr_accessor :object_from_id_proc, :type_from_object_proc
|
@@ -54,13 +59,16 @@ module GraphQL
|
|
54
59
|
# Create a global ID for type-name & ID
|
55
60
|
# (This is an opaque transform)
|
56
61
|
def to_global_id(type_name, id)
|
57
|
-
|
62
|
+
if type_name.include?(self.class.id_separator) || id.include?(self.class.id_separator)
|
63
|
+
raise "to_global_id(#{type_name}, #{id}) contains reserved characters `#{self.class.id_separator}`"
|
64
|
+
end
|
65
|
+
Base64.strict_encode64([type_name, id].join(self.class.id_separator))
|
58
66
|
end
|
59
67
|
|
60
68
|
# Get type-name & ID from global ID
|
61
69
|
# (This reverts the opaque transform)
|
62
70
|
def from_global_id(global_id)
|
63
|
-
Base64.decode64(global_id).split(
|
71
|
+
Base64.decode64(global_id).split(self.class.id_separator)
|
64
72
|
end
|
65
73
|
|
66
74
|
# Use the provided config to
|
@@ -1,9 +1,16 @@
|
|
1
1
|
class GraphQL::BaseType
|
2
2
|
def connection_type
|
3
|
-
@connection_type ||=
|
3
|
+
@connection_type ||= define_connection
|
4
4
|
end
|
5
5
|
|
6
6
|
def edge_type
|
7
7
|
@edge_type ||= GraphQL::Relay::Edge.create_type(self)
|
8
8
|
end
|
9
|
+
|
10
|
+
def define_connection(&block)
|
11
|
+
if !@connection_type.nil?
|
12
|
+
raise("#{name}'s connection type was already defined, can't redefine it!")
|
13
|
+
end
|
14
|
+
@connection_type = GraphQL::Relay::BaseConnection.create_type(self, &block)
|
15
|
+
end
|
9
16
|
end
|
@@ -38,6 +38,20 @@ describe GraphQL::Relay::GlobalNodeIdentification do
|
|
38
38
|
end
|
39
39
|
end
|
40
40
|
|
41
|
+
after do
|
42
|
+
# Set the id_separator back to it's default after each spec, since some of
|
43
|
+
# them change it at runtime
|
44
|
+
GraphQL::Relay::GlobalNodeIdentification.id_separator = "-"
|
45
|
+
end
|
46
|
+
|
47
|
+
describe 'id_separator' do
|
48
|
+
it "allows you to change it at runtime" do
|
49
|
+
GraphQL::Relay::GlobalNodeIdentification.id_separator = "-zomg-"
|
50
|
+
|
51
|
+
assert_equal("-zomg-", GraphQL::Relay::GlobalNodeIdentification.id_separator)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
41
55
|
describe 'to_global_id / from_global_id ' do
|
42
56
|
it 'Converts typename and ID to and from ID' do
|
43
57
|
global_id = node_identification.to_global_id("SomeType", "123")
|
@@ -45,12 +59,28 @@ describe GraphQL::Relay::GlobalNodeIdentification do
|
|
45
59
|
assert_equal("SomeType", type_name)
|
46
60
|
assert_equal("123", id)
|
47
61
|
end
|
62
|
+
|
63
|
+
it "allows you to change the id_separator" do
|
64
|
+
GraphQL::Relay::GlobalNodeIdentification.id_separator = "---"
|
65
|
+
|
66
|
+
global_id = node_identification.to_global_id("Type-With-UUID", "250cda0e-a89d-41cf-99e1-2872d89f1100")
|
67
|
+
type_name, id = node_identification.from_global_id(global_id)
|
68
|
+
assert_equal("Type-With-UUID", type_name)
|
69
|
+
assert_equal("250cda0e-a89d-41cf-99e1-2872d89f1100", id)
|
70
|
+
end
|
71
|
+
|
72
|
+
it "raises an error if you try and use a reserved character in the ID" do
|
73
|
+
err = assert_raises(RuntimeError) {
|
74
|
+
node_identification.to_global_id("Best-Thing", "234")
|
75
|
+
}
|
76
|
+
assert_includes err.message, "to_global_id(Best-Thing, 234) contains reserved characters `-`"
|
77
|
+
end
|
48
78
|
end
|
49
79
|
|
50
80
|
describe "type_from_object" do
|
51
81
|
describe "when the return value is not a BaseType" do
|
52
82
|
it "raises an error " do
|
53
|
-
err = assert_raises {
|
83
|
+
err = assert_raises(RuntimeError) {
|
54
84
|
GraphQL::Relay::GlobalNodeIdentification.instance.type_from_object(:test_error)
|
55
85
|
}
|
56
86
|
assert_includes err.message, "not_a_type (Symbol)"
|
@@ -39,7 +39,7 @@ end
|
|
39
39
|
|
40
40
|
# Define a connection which will wrap an ActiveRecord::Relation.
|
41
41
|
# We use an optional block to add fields to the connection type:
|
42
|
-
|
42
|
+
BaseType.define_connection do
|
43
43
|
field :totalCount do
|
44
44
|
type types.Int
|
45
45
|
resolve -> (obj, args, ctx) { obj.object.count }
|
@@ -65,7 +65,7 @@ Faction = GraphQL::ObjectType.define do
|
|
65
65
|
# You can define arguments here and use them in the connection
|
66
66
|
argument :nameIncludes, types.String
|
67
67
|
end
|
68
|
-
connection :bases,
|
68
|
+
connection :bases, BaseType.connection_type do
|
69
69
|
# Resolve field should return an Array, the Connection
|
70
70
|
# will do the rest!
|
71
71
|
resolve -> (obj, args, ctx) {
|
@@ -78,8 +78,8 @@ Faction = GraphQL::ObjectType.define do
|
|
78
78
|
argument :nameIncludes, types.String
|
79
79
|
end
|
80
80
|
|
81
|
-
connection :basesClone,
|
82
|
-
connection :basesByName,
|
81
|
+
connection :basesClone, BaseType.connection_type
|
82
|
+
connection :basesByName, BaseType.connection_type, property: :bases do
|
83
83
|
argument :order, types.String, default_value: "name"
|
84
84
|
end
|
85
85
|
end
|
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.5.
|
4
|
+
version: 0.5.1
|
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
|
11
|
+
date: 2015-12-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: graphql
|