Skip to content

Commit

Permalink
added option to make a edge non-nullable too
Browse files Browse the repository at this point in the history
  • Loading branch information
gopeter committed Mar 5, 2021
1 parent a149661 commit 2b2e877
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
19 changes: 15 additions & 4 deletions lib/graphql/types/relay/connection_behaviors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ def self.included(child_class)
child_class.extend(ClassMethods)
child_class.extend(Relay::DefaultRelay)
child_class.default_relay(true)
child_class.edges_nullable(true)
child_class.node_nullable(true)
child_class.edges_nullable(true)
child_class.edge_nullable(true)
add_page_info_field(child_class)
end

Expand All @@ -33,15 +34,15 @@ module ClassMethods
# It's called when you subclass this base connection, trying to use the
# class name to set defaults. You can call it again in the class definition
# to override the default (or provide a value, if the default lookup failed).
def edge_type(edge_type_class, edge_class: GraphQL::Relay::Edge, node_type: edge_type_class.node_type, nodes_field: true, node_nullable: self.node_nullable, edges_nullable: self.edges_nullable)
def edge_type(edge_type_class, edge_class: GraphQL::Relay::Edge, node_type: edge_type_class.node_type, nodes_field: true, node_nullable: self.node_nullable, edges_nullable: self.edges_nullable, edge_nullable: self.edge_nullable)
# Set this connection's graphql name
node_type_name = node_type.graphql_name

@node_type = node_type
@edge_type = edge_type_class
@edge_class = edge_class

field :edges, [edge_type_class, null: true],
field :edges, [edge_type_class, null: edge_nullable],
null: edges_nullable,
description: "A list of edges.",
legacy_edge_class: edge_class, # This is used by the old runtime only, for EdgesInstrumentation
Expand Down Expand Up @@ -92,7 +93,17 @@ def edges_nullable(new_value = nil)
else
@edges_nullable ||= new_value
end
end
end

# Set the default `edge_nullable` for this class and its child classes. (Defaults to `true`.)
# Use `edge_nullable(false)` in your base class to make non-null `edge` fields.
def edge_nullable(new_value = nil)
if new_value.nil?
@edge_nullable || superclass.edge_nullable
else
@edge_nullable ||= new_value
end
end

private

Expand Down
9 changes: 8 additions & 1 deletion spec/graphql/types/relay/base_connection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class NodeEdgeType < GraphQL::Types::Relay::BaseEdge
end

class NonNullableNodeEdgeConnectionType < GraphQL::Types::Relay::BaseConnection
edge_type(NodeEdgeType, node_nullable: false, edges_nullable: false)
edge_type(NodeEdgeType, node_nullable: false, edges_nullable: false, edge_nullable: false)
end

class Query < GraphQL::Schema::Object
Expand All @@ -39,6 +39,13 @@ class Schema < GraphQL::Schema
assert_equal "NON_NULL",edges_field["type"]["kind"]
end

it "edge_nullable option is works" do
res = NonNullAbleNodeDummy::Schema.execute(GraphQL::Introspection::INTROSPECTION_QUERY)
connection_type = res["data"]["__schema"]["types"].find { |t| t["name"] == "NonNullableNodeEdgeConnection" }
edges_field = connection_type["fields"].find { |f| f["name"] == "edges" }
assert_equal "NON_NULL",edges_field["type"]["ofType"]["ofType"]["kind"]
end

it "never treats nodes like a connection" do
type = Class.new(GraphQL::Schema::Object) do
graphql_name "MissedConnection"
Expand Down

0 comments on commit 2b2e877

Please sign in to comment.