Sha256: 19436d802fcec3fb41e3d65f41ef84382dda52029cbc1407fbfc65d111bd60d4

Contents?: true

Size: 1.31 KB

Versions: 1

Compression:

Stored size: 1.31 KB

Contents

# frozen_string_literal: true

module GraphQL
  module Client
    class Base
      attr_reader :adapter, :config, :schema

      def initialize(schema, config: nil, adapter: nil, &block)
        @config = config || Config.new
        @schema = load_schema(schema)
        @adapter = adapter || Adapters::HTTPAdapter.new(@config)

        instance_eval(&block) if block_given?
      end

      def build_query
        query = Query::Document.new(@schema)

        if block_given?
          yield query
        else
          query
        end
      end

      def configure
        yield @config
      end

      def query(query, operation_name: nil, variables: {})
        response = adapter.request(query.to_query, operation_name: operation_name, variables: variables)
        GraphObject.new(data: response.data, query: query)
      end

      def raw_query(query_string, operation_name: nil, variables: {})
        response = adapter.request(query_string, operation_name: operation_name, variables: variables)
        ResponseObject.new(response.data)
      end

      private

      def load_schema(schema)
        case schema
        when Pathname
          schema_string = JSON.parse(File.read(schema))
          GraphQLSchema.new(schema_string)
        else
          GraphQLSchema.new(schema)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
graphql_client-0.3.3 lib/graphql_client/base.rb