Sha256: 7f5f0d3a0a7caa251968f21569195c3ccaca6ae3180872db599bf45ffc850de0

Contents?: true

Size: 1.13 KB

Versions: 4

Compression:

Stored size: 1.13 KB

Contents

# frozen_string_literal: true

module SolidusGraphqlApi
  class Schema < GraphQL::Schema
    query Types::Query
    mutation Types::Mutation

    use ::BatchLoader::GraphQL

    rescue_from CanCan::AccessDenied do |exception|
      raise GraphQL::ExecutionError, exception.message
    end

    rescue_from ActiveRecord::RecordNotFound do
      raise GraphQL::ExecutionError, I18n.t(:'activerecord.exceptions.not_found')
    end

    # Relay Object Identification:
    class << self
      # Return a string UUID for object
      def id_from_object(object, _type_definition, _query_ctx)
        GraphQL::Schema::UniqueWithinType.encode(object.class.name, object.id)
      end

      # Given a string UUID, find the object
      def object_from_id(id, _query_ctx)
        class_name, item_id = GraphQL::Schema::UniqueWithinType.decode(id)

        ::Object.const_get(class_name).find(item_id)
      end

      # Object Resolution
      def resolve_type(_type_definition, object, _query_ctx)
        class_name = object.is_a?(Spree::PaymentMethod) ? "PaymentMethod" : object.class.name.demodulize

        Types.const_get(class_name)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
solidus_graphql_api-0.3.1 lib/solidus_graphql_api/schema.rb
solidus_graphql_api-0.3.0 lib/solidus_graphql_api/schema.rb
solidus_graphql_api-0.2.0 lib/solidus_graphql_api/schema.rb
solidus_graphql_api-0.1.0 lib/solidus_graphql_api/schema.rb