Sha256: a17c6f5e38cfd24019167ab9a85ed5e2318975ea2593a1d194accab1006a2582

Contents?: true

Size: 1.31 KB

Versions: 1

Compression:

Stored size: 1.31 KB

Contents

module GraphQLDocs
  class Parser
    attr_reader :schema, :processed_schema

    def initialize(response, options)
      @options = options
      @schema = JSON.parse(response)['data']
      @processed_schema = @schema.dup['__schema']
    end

    def parse
      # sort the types
      @processed_schema['types'] = @processed_schema['types'].sort_by { |key, _| key['name'] }

      # fetch the connections
      @processed_schema['types'].each do |object|
        next if object['fields'].nil?
        object['connections'] = object['fields'].select { |f| next if f.is_a?(Array); connection?(f) }
      end

      # fetch the kinds of items
      type_kinds = @processed_schema['types'].map { |h| h['kind'] }.uniq
      type_kinds.each do |kind|
        @processed_schema["#{kind.downcase}_types"] = @processed_schema['types'].select { |t| t['kind'] == kind }
      end
      @processed_schema['mutation_types'] = @processed_schema['object_types'].select do |t|
        t['name'] == 'Mutation'
      end.first['fields']
      # TODO: should the 'types' key be deleted now?

      @processed_schema
    end

    private

    def connection?(hash)
      if hash['type']['ofType'] && hash['type']['ofType']['name'] && hash['type']['ofType']['name'].end_with?('Connection')
        true
      else
        false
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
graphql-docs-0.1.1 lib/graphql-docs/parser.rb