class GraphqlRails::Router::SchemaBuilder

builds GraphQL::Schema based on previously defined grahiti data

Attributes

group[R]
mutations[R]
queries[R]
raw_actions[R]

Public Class Methods

new(queries:, mutations:, raw_actions:, group: nil) click to toggle source
# File lib/graphql_rails/router/schema_builder.rb, line 11
def initialize(queries:, mutations:, raw_actions:, group: nil)
  @queries = queries
  @mutations = mutations
  @raw_actions = raw_actions
  @group = group
end

Private Class Methods

inspect() click to toggle source
# File lib/graphql_rails/router/schema_builder.rb, line 56
def self.inspect
  "#{GraphQL::Schema::Object}(#{graphql_name})"
end

Public Instance Methods

call() click to toggle source
# File lib/graphql_rails/router/schema_builder.rb, line 18
def call
  query_type = build_group_type('Query', queries)
  mutation_type = build_group_type('Mutation', mutations)
  raw = raw_actions

  Class.new(GraphQL::Schema) do
    connections.add(
      GraphqlRails::Decorator::RelationDecorator,
      GraphQL::Pagination::ActiveRecordRelationConnection
    )
    cursor_encoder(Router::PlainCursorEncoder)
    raw.each { |action| send(action[:name], *action[:args], &action[:block]) }

    query(query_type) if query_type
    mutation(mutation_type) if mutation_type
  end
end

Private Instance Methods

build_group_type(type_name, routes) click to toggle source
# File lib/graphql_rails/router/schema_builder.rb, line 40
def build_group_type(type_name, routes)
  group_name = group
  group_routes = routes.select { |route| route.show_in_group?(group_name) }
  return if group_routes.empty?

  build_type(type_name, group_routes)
end
build_type(type_name, group_routes) click to toggle source
# File lib/graphql_rails/router/schema_builder.rb, line 48
def build_type(type_name, group_routes)
  Class.new(GraphQL::Schema::Object) do
    graphql_name(type_name)

    group_routes.each do |route|
      field(*route.name, **route.field_options)
    end

    def self.inspect
      "#{GraphQL::Schema::Object}(#{graphql_name})"
    end
  end
end