Sha256: 4d06b670ffd2c2ab8ba712b31817a4dce24108d42751df6c2be9f41180473b6b

Contents?: true

Size: 1.61 KB

Versions: 2

Compression:

Stored size: 1.61 KB

Contents

# frozen_string_literal: true

module GraphQL
  module Stitching
    class PlannerOperation
      LANGUAGE_PRINTER = GraphQL::Language::Printer.new

      attr_reader :order, :location, :parent_type, :if_type, :operation_type, :path
      attr_accessor :after, :selections, :variables, :boundary

      def initialize(
        location:,
        parent_type:,
        order:,
        after: nil,
        operation_type: "query",
        selections: [],
        variables: [],
        path: [],
        if_type: nil,
        boundary: nil
      )
        @location = location
        @parent_type = parent_type
        @order = order
        @after = after
        @operation_type = operation_type
        @selections = selections
        @variables = variables
        @path = path
        @if_type = if_type
        @boundary = boundary
      end

      def selection_set
        op = GraphQL::Language::Nodes::OperationDefinition.new(selections: @selections)
        LANGUAGE_PRINTER.print(op).gsub!(/\s+/, " ").strip!
      end

      def variable_set
        @variables.each_with_object({}) do |(variable_name, value_type), memo|
          memo[variable_name] = LANGUAGE_PRINTER.print(value_type)
        end
      end

      def to_h
        data = {
          "order" => @order,
          "after" => @after,
          "location" => @location,
          "operation_type" => @operation_type,
          "selections" => selection_set,
          "variables" => variable_set,
          "path" => @path,
        }

        data["if_type"] = @if_type if @if_type
        data["boundary"] = @boundary if @boundary
        data
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
graphql-stitching-1.0.0 lib/graphql/stitching/planner_operation.rb
graphql-stitching-0.3.6 lib/graphql/stitching/planner_operation.rb