Sha256: 3714c6818144eae2e1c2cc47ec74fbedf16d646be76e06d655927a37e8b3bb5a

Contents?: true

Size: 1.7 KB

Versions: 10

Compression:

Stored size: 1.7 KB

Contents

# frozen_string_literal: true

require 'json'

require 'active_support/core_ext/string/inflections'
require 'graphql/client'

namespace :graphql do
  namespace :schema do
    desc "Downloads and saves the GraphQL schema (options: SERVICE=service_name AUTHORIZATION='token ...')"
    task update: :environment do
      service = if ENV['SERVICE']
                  ENV['SERVICE']
                else
                  services = Artemis.config_for_graphql(Rails.application).keys

                  if services.size == 1
                    services.first
                  else
                    raise "Please specify a service name (available services: #{services.join(", ")}): rake graphql:schema:update SERVICE=service"
                  end
                end

      headers          = ENV['AUTHORIZATION'] ? { Authorization: ENV['AUTHORIZATION'] } : {}
      service_class    = service.to_s.camelize.constantize
      schema_path      = service_class.endpoint.schema_path
      schema           = service_class.connection
                           .execute(
                             document: GraphQL::Client::IntrospectionDocument,
                             operation_name: "IntrospectionQuery",
                             variables: {},
                             context: { headers: headers }
                           ).to_h

      if schema['errors'].nil? || schema['errors'].empty?
        FileUtils.mkdir_p(File.dirname(schema_path))
        File.open(schema_path, 'w') do |file|
          file.write(JSON.pretty_generate(schema))
        end

        puts "saved schema to: #{schema_path.gsub("#{Dir.pwd}/", '')}"
      else
        raise "received error from server: #{schema}\n\n"
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
artemis-1.1.0 lib/tasks/artemis.rake
artemis-1.0.2 lib/tasks/artemis.rake
artemis-1.0.0 lib/tasks/artemis.rake
artemis-0.9.0 lib/tasks/artemis.rake
artemis-0.8.0 lib/tasks/artemis.rake
artemis-0.7.0 lib/tasks/artemis.rake
artemis-0.6.0 lib/tasks/artemis.rake
artemis-0.5.2 lib/tasks/artemis.rake
artemis-0.5.1 lib/tasks/artemis.rake
artemis-0.5.0 lib/tasks/artemis.rake