Skip to content

node and nodes scalar type configuration #5381

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 22 additions & 22 deletions lib/graphql/types/relay/has_node_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,36 @@
module GraphQL
module Types
module Relay
# Include this module to your root Query type to get a Relay-compliant `node(id: ID!): Node` field that uses the schema's `object_from_id` hook.
module HasNodeField
def self.included(child_class)
child_class.field(**field_options, &field_block)
add_node_field(child_class, id_type: GraphQL::Types::ID)
end

class << self
def field_options
{
name: "node",
type: GraphQL::Types::Relay::Node,
null: true,
description: "Fetches an object given its ID.",
relay_node_field: true,
}
def self.[](id_type:)
Module.new do
define_singleton_method(:included) do |child_class|
GraphQL::Types::Relay::HasNodeField.add_node_field(child_class, id_type: id_type)
end
end
end

def field_block
Proc.new {
argument :id, "ID!",
description: "ID of the object."
def self.add_node_field(child_class, id_type:)
child_class.field(
name: "node",
type: GraphQL::Types::Relay::Node,
null: true,
description: "Fetches an object given its ID.",
relay_node_field: true
) do
argument :id, id_type, description: "ID of the object."

def resolve(obj, args, ctx)
ctx.schema.object_from_id(args[:id], ctx)
end
def resolve(_obj, args, ctx)
ctx.schema.object_from_id(args[:id], ctx)
end

def resolve_field(obj, args, ctx)
resolve(obj, args, ctx)
end
}
def resolve_field(obj, args, ctx)
resolve(obj, args, ctx)
end
end
end
end
Expand Down
44 changes: 22 additions & 22 deletions lib/graphql/types/relay/has_nodes_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,36 @@
module GraphQL
module Types
module Relay
# Include this module to your root Query type to get a Relay-style `nodes(id: ID!): [Node]` field that uses the schema's `object_from_id` hook.
module HasNodesField
def self.included(child_class)
child_class.field(**field_options, &field_block)
add_nodes_field(child_class, id_type: GraphQL::Types::ID)
end

class << self
def field_options
{
name: "nodes",
type: [GraphQL::Types::Relay::Node, null: true],
null: false,
description: "Fetches a list of objects given a list of IDs.",
relay_nodes_field: true,
}
def self.[](id_type:)
Module.new do
define_singleton_method(:included) do |child_class|
GraphQL::Types::Relay::HasNodesField.add_nodes_field(child_class, id_type: id_type)
end
end
end

def field_block
Proc.new {
argument :ids, "[ID!]!",
description: "IDs of the objects."
def self.add_nodes_field(child_class, id_type:)
child_class.field(
name: "nodes",
type: [GraphQL::Types::Relay::Node, null: true],
null: false,
description: "Fetches a list of objects given a list of IDs.",
relay_nodes_field: true
) do
argument :ids, [id_type], description: "IDs of the objects."

def resolve(obj, args, ctx)
args[:ids].map { |id| ctx.schema.object_from_id(id, ctx) }
end
def resolve(_obj, args, ctx)
args[:ids].map { |id| ctx.schema.object_from_id(id, ctx) }
end

def resolve_field(obj, args, ctx)
resolve(obj, args, ctx)
end
}
def resolve_field(obj, args, ctx)
resolve(obj, args, ctx)
end
end
end
end
Expand Down
Loading