Class: GraphitiGql::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/graphiti_gql/schema.rb,
lib/graphiti_gql/schema/util.rb,
lib/graphiti_gql/schema/query.rb,
lib/graphiti_gql/response_shim.rb,
lib/graphiti_gql/schema/registry.rb,
lib/graphiti_gql/schema/connection.rb,
lib/graphiti_gql/schema/fields/show.rb,
lib/graphiti_gql/schema/fields/index.rb,
lib/graphiti_gql/schema/fields/stats.rb,
lib/graphiti_gql/schema/fields/to_one.rb,
lib/graphiti_gql/schema/resource_type.rb,
lib/graphiti_gql/schema/fields/to_many.rb,
lib/graphiti_gql/schema/list_arguments.rb,
lib/graphiti_gql/schema/fields/attribute.rb,
lib/graphiti_gql/schema/polymorphic_belongs_to_interface.rb

Defined Under Namespace

Modules: Fields Classes: Connection, DatetimeRange, FloatRange, IntegerRange, ListArguments, PolymorphicBelongsToInterface, PreciseDatetime, PreciseDatetimeRange, Query, Registry, RelayConnectionExtension, ResourceType, ResponseShim, StringRange, ToManyConnection, Util

Constant Summary collapse

GQL_TYPE_MAP =
{
  integer_id: String,
  string: String,
  uuid: String,
  gid: GraphQL::Types::ID,
  integer: Integer,
  big_integer: GraphQL::Types::BigInt,
  float: Float,
  boolean: GraphQL::Types::Boolean,
  date: GraphQL::Types::ISO8601Date,
  datetime: GraphQL::Types::ISO8601DateTime,
  precise_datetime: PreciseDatetime,
  hash: GraphQL::Types::JSON,
  string_range: StringRange,
  integer_range: IntegerRange,
  float_range: FloatRange,
  datetime_range: DatetimeRange,
  precise_datetime_range: PreciseDatetimeRange,
  array: [GraphQL::Types::JSON],
  array_of_strings: [String],
  array_of_integers: [Integer],
  array_of_floats: [Float],
  array_of_dates: [GraphQL::Types::ISO8601Date],
  array_of_datetimes: [GraphQL::Types::ISO8601DateTime],
  array_of_precise_datetimes: [PreciseDatetime],
  array_of_string_ranges: [StringRange],
  array_of_integer_ranges: [IntegerRange],
  array_of_float_ranges: [FloatRange],
  array_of_datetime_ranges: [DatetimeRange],
  array_of_precise_datetime_ranges: [PreciseDatetimeRange]
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resources) ⇒ Schema

Returns a new instance of Schema.



101
102
103
# File 'lib/graphiti_gql/schema.rb', line 101

def initialize(resources)
  @resources = resources
end

Class Method Details

.base_objectObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/graphiti_gql/schema.rb', line 75

def self.base_object
  return @base_object if @base_object
  klass = Class.new(GraphQL::Schema::Object)

  # TODO make this config maybe
  if defined?(ActionView)
    klass.send(:include, ActionView::Helpers::TranslationHelper)
    klass.class_eval do
      def initialize(*)
        super
        @virtual_path = "."
      end
    end
  end
  @base_object = klass
  klass
end


97
98
99
# File 'lib/graphiti_gql/schema.rb', line 97

def self.print
  GraphQL::Schema::Printer.print_schema(GraphitiGql.schema)
end

.registryObject



93
94
95
# File 'lib/graphiti_gql/schema.rb', line 93

def self.registry
  Registry.instance
end

.resource_types!Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/graphiti_gql/schema.rb', line 105

def self.resource_types!
  resources = Graphiti.resources.reject(&:abstract_class?)
  resources.each { |resource| ResourceType.new(resource).build }
  # add relationships
  registry.resource_types.each do |registered|
    resource, type = registered[:resource], registered[:type]
    ResourceType.add_relationships(resource, type)
  end
  # add_value_objects
  registry.resource_types(value_objects: false).each do |registered|
    resource, type = registered[:resource], registered[:type]
    ResourceType.add_value_objects(resource, type)
  end
end

Instance Method Details

#generateObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/graphiti_gql/schema.rb', line 120

def generate
  klass = Class.new(::GraphitiGql.config.base_schema || ::GraphQL::Schema)
  return if klass.query
  # TODO inherit from klass.query if exists
  klass.query(Query.new(@resources).build)
  klass.use(GraphQL::Batch)
  klass.connections.add(ResponseShim, Connection)
  klass.connections.add(Array, ToManyConnection)
  klass.orphan_types [GraphQL::Types::JSON]
  klass.rescue_from(Exception) do |err, obj, args, ctx, field|
    if GraphitiGql.config.error_handling
      handler = GraphitiGql.config.exception_handler
      handler.new(err, obj, args, ctx, field).handle
    else
      raise err
    end
  end
  klass
end