Class: Stellate::SchemaSyncing

Inherits:
Object
  • Object
show all
Defined in:
lib/stellate.rb

Overview

Use this plugin in your GraphQL::Schema to automatically sync your GraphQL schema with your Stellate service.

Class Method Summary collapse

Class Method Details

.use(schema, **kwargs) ⇒ Object

The first argument is the GraphQLSchema class that this plugin is used on. It accepts the following names arguments:

  • ‘callback` (`Method`): If passed, this will be called with a lambda as argument. Calling the passed lambda will sync the schema to Stellate via HTTP. This is useful if you want to move this request into a non- blocking process (e.g. using Sidekiq) rather than the default behavior of performing a blocking HTTP request which can increase overall response times of your API.



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/stellate.rb', line 124

def self.use(schema, **kwargs)
  unless schema.stellate_service_name.is_a?(String)
    puts 'Missing service name in order to sync schema to Stellate'
    return
  end

  unless schema.stellate_token.is_a?(String)
    puts 'Missing token in order to sync schema to Stellate'
    return
  end

  introspection = JSON.parse(schema.to_json)['data']

  stellate_request = {
    'url' => "https://#{schema.stellate_service_name}.stellate.sh/schema",
    'headers' => {
      'Content-Type' => 'application/json',
      'Stellate-Schema-Token' => schema.stellate_token
    },
    'body' => { schema: introspection }.to_json
  }

  callback = kwargs[:callback]
  # The former check handles methods, the latter handles lambdas
  if callback.is_a?(Method) || callback.respond_to?(:call)
    callback.call(stellate_request)
  # This handles symbols that contain methods
  elsif callback.is_a?(Symbol) && method(callback).is_a?(Method)
    method(callback).call(stellate_request)
  else
    run_stellate_request(stellate_request)
  end
end