class GraphQL::Types::Relay::BaseConnection
@see Relay::BaseEdge for edge types
end
edge_type(Types::PostEdge)
class Types::PostConnection < Types::BaseConnection
end
node_type(Types::Post)
class Types::PostEdge < Types::BaseEdge
# Then extend them for the object in your app
class Types::BaseConnection < GraphQL::Types::Relay::BaseConnection; end
class Types::BaseEdge < GraphQL::Types::Relay::BaseEdge; end
# Make a couple of base classes:
end
class Types::Post < BaseObject
# Given some object in your app …
@example Implementation a connection and edge
so you can extend your own ‘BaseObject` instead of `GraphQL::Schema::Object`.
You may wish to copy this code into your own base class,
for Relay classes in your own app.
Use this to implement Relay connections, or take it as inspiration
def accessible?(ctx)
def accessible?(ctx) node_type.accessible?(ctx) end
def authorized?(obj, ctx)
def authorized?(obj, ctx) true # Let nodes be filtered out end
def define_nodes_field(nullable = true)
def define_nodes_field(nullable = true) type = nullable ? [@node_type, null: true] : [@node_type] field :nodes, type, null: nullable, description: "A list of nodes.", connection: false end
def edge_type(edge_type_class, edge_class: GraphQL::Relay::Edge, node_type: edge_type_class.node_type, nodes_field: true, node_nullable: true)
class name to set defaults. You can call it again in the class definition
It's called when you subclass this base connection, trying to use the
- description
- `nodes` field
- `edges` field
This method will use the inputs to create:
Configure this connection to return `edges` and `nodes` based on `edge_type_class`.
def edge_type(edge_type_class, edge_class: GraphQL::Relay::Edge, node_type: edge_type_class.node_type, nodes_field: true, node_nullable: true) # 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], null: true, description: "A list of edges.", edge_class: edge_class define_nodes_field(node_nullable) if nodes_field description("The connection type for #{node_type_name}.") end
def edges
def edges if @object.is_a?(GraphQL::Pagination::Connection) @object.edges elsif context.interpreter? context.schema.after_lazy(object.edge_nodes) do |nodes| nodes.map { |n| self.class.edge_class.new(n, object) } end else # This is done by edges_instrumentation @object.edge_nodes end end
def nodes
By default this calls through to the ConnectionWrapper's edge nodes method,
def nodes @object.edge_nodes end
def nodes_field
def nodes_field define_nodes_field end
def scope_items(items, context)
def scope_items(items, context) node_type.scope_items(items, context) end
def visible?(ctx)
def visible?(ctx) node_type.visible?(ctx) end