Module: GraphQL::Client::DocumentTypes

Defined in:
lib/graphql/client/document_types.rb

Overview

Internal: Use schema to detect definition and field types.

Defined Under Namespace

Classes: AnalyzeTypesVisitor

Class Method Summary collapse

Class Method Details

.analyze_types(schema, document) ⇒ Object

Internal: Detect all types used in a given document

schema - A GraphQL::Schema document - A GraphQL::Language::Nodes::Document to scan

Returns a Hash to GraphQL::Type objects.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/graphql/client/document_types.rb', line 45

def self.analyze_types(schema, document)
  unless schema.is_a?(GraphQL::Schema) || (schema.is_a?(Class) && schema < GraphQL::Schema)
    raise TypeError, "expected schema to be a GraphQL::Schema, but was #{schema.class}"
  end

  unless document.is_a?(GraphQL::Language::Nodes::Document)
    raise TypeError, "expected schema to be a GraphQL::Language::Nodes::Document, but was #{document.class}"
  end

  visitor = AnalyzeTypesVisitor.new(document, schema: schema)
  visitor.visit
  visitor.fields
rescue StandardError => err
  if err.is_a?(TypeError)
    raise
  end
  # FIXME: TypeStack my crash on invalid documents
  visitor.fields
end