module GraphQR::QueryField

This extension adds the query_field method. A helper to create simple queries faster and easier

To use this extension, add extend Graphql::QueryField on your QueryType

Public Instance Methods

query_field(field_name, active_record_class, type_class:, scope_class: nil, **kwargs, &block) click to toggle source

The query_field method is a helper to create fields and resolver without effort.

Arguments

field_name (required): the GraphQL query name

active_record_class (required): the model ActiveRecord class. It can be represented as an array if you want it to return a collection

type_class (required): The GraphQL type class

scope_class: A specific InputType that contains the possible scopes that can be applied to your collection. Similar to the has_scope gem. This argument is required for collection fields.

Examples

query_type :user, User, type_class: UserType
query_type :users, [User], type_class: UserType, scope_class: UserScopeInput

Collention fields

Collection fields are always paginated using the configured paginator Its resolver will look for the index? method on the model Policy. It'll have the optional filter argument with scope_class type

Single fields

Single fields have the required id argument to find the exact record searched. Its resolver will look for the show? method on the model Policy.

rubocop:disable Metrics/ParameterLists

# File lib/graphqr/query_field.rb, line 46
def query_field(field_name, active_record_class, type_class:, scope_class: nil, **kwargs, &block)
  is_collection = active_record_class.is_a? Array
  if is_collection
    active_record_class = active_record_class.first
    resolver = collection_resolver(active_record_class, type_class, scope_class)
  else
    resolver = resource_resolver(active_record_class, type_class)
  end

  field(field_name, paginate: is_collection, resolver: resolver, **kwargs, &block)
end