Skip to content

Commit 77828ae

Browse files
committed
fix(GlobalId) use normal fields for global id fields
1 parent 1e6af3d commit 77828ae

File tree

6 files changed

+45
-23
lines changed

6 files changed

+45
-23
lines changed

lib/graphql/define/assign_global_id_field.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ module Define
33
module AssignGlobalIdField
44
def self.call(type_defn, field_name)
55
type_defn.name || raise("You must define the type's name before creating a GlobalIdField")
6-
GraphQL::Define::AssignObjectField.call(type_defn, field_name, field: GraphQL::Relay::GlobalIdField.new(type_defn.name))
6+
resolve = GraphQL::Relay::GlobalIdResolve.new(type_name: type_defn.name, property: field_name)
7+
GraphQL::Define::AssignObjectField.call(type_defn, field_name, type: GraphQL::ID_TYPE.to_non_null_type, resolve: resolve)
78
end
89
end
910
end

lib/graphql/relay.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
require 'graphql/relay/base_connection'
88
require 'graphql/relay/array_connection'
99
require 'graphql/relay/relation_connection'
10-
require 'graphql/relay/global_id_field'
10+
require 'graphql/relay/global_id_resolve'
1111
require 'graphql/relay/mutation'
1212
require 'graphql/relay/connection_field'
1313
require 'graphql/relay/connection_type'

lib/graphql/relay/global_id_field.rb

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module GraphQL
2+
module Relay
3+
class GlobalIdResolve
4+
def initialize(type_name:, property:)
5+
@property = property
6+
@type_name = type_name
7+
end
8+
9+
def call(obj, args, ctx)
10+
id_value = obj.public_send(@property)
11+
ctx.query.schema.node_identification.to_global_id(@type_name, id_value)
12+
end
13+
end
14+
end
15+
end

spec/graphql/analysis/query_complexity_spec.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,30 @@
187187
end
188188
end
189189

190+
describe "relay types" do
191+
let(:query) { GraphQL::Query.new(StarWarsSchema, query_string) }
192+
let(:query_string) {%|
193+
{
194+
rebels {
195+
ships {
196+
edges {
197+
node {
198+
id
199+
}
200+
}
201+
pageInfo {
202+
hasNextPage
203+
}
204+
}
205+
}
206+
}
207+
|}
208+
209+
it "gets the complexity" do
210+
reduce_result
211+
assert_equal 7, complexities.last
212+
end
213+
end
190214

191215
describe "custom complexities" do
192216
let(:query) { GraphQL::Query.new(complexity_schema, query_string) }

spec/support/star_wars_schema.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@
3030
Ship = GraphQL::ObjectType.define do
3131
name "Ship"
3232
interfaces [NodeIdentification.interface]
33-
# Explict alternative to `global_id_field` helper:
34-
field :id, field: GraphQL::Relay::GlobalIdField.new("Ship")
33+
global_id_field :id
3534
field :name, types.String
3635
end
3736

@@ -83,7 +82,8 @@ def upcased_parent_name
8382
Faction = GraphQL::ObjectType.define do
8483
name "Faction"
8584
interfaces [NodeIdentification.interface]
86-
field :id, field: GraphQL::Relay::GlobalIdField.new("Faction")
85+
86+
field :id, !types.ID, resolve: GraphQL::Relay::GlobalIdResolve.new(type_name: "Faction", property: :id)
8787
field :name, types.String
8888
connection :ships, Ship.connection_type do
8989
resolve -> (obj, args, ctx) {

0 commit comments

Comments
 (0)