Class: FragmentClient

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/fragment_client.rb,
lib/fragment_client/graphql_ast.rb,
lib/fragment_client/typed_entries.rb,
lib/fragment_client/typed_ledger_entry.rb

Overview

A client for Fragment

Defined Under Namespace

Modules: Entries, GraphqlAst, TypedEntries Classes: AuthenticationError, Configuration, NetworkError, ResponseError, Token, TokenExpiredError, TypedLedgerEntry

Constant Summary collapse

DEFAULT_OAUTH_URL =
'https://auth.fragment.dev/oauth2/token'
DEFAULT_OAUTH_SCOPE =
'https://api.fragment.dev/*'
WRAPPED_OPERATIONS =

Operations with a hand-written wrapper below, which the dynamic definer must not shadow -- a singleton method beats an instance method. Listed explicitly rather than tested with method_defined?, which would also match clone, freeze and everything else on Object.

T.let(%w[add_ledger_entries].freeze, T::Array[String])

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_id, client_secret, extra_queries_filenames: nil, api_url: nil, oauth_url: DEFAULT_OAUTH_URL, oauth_scope: DEFAULT_OAUTH_SCOPE) ⇒ FragmentClient

Returns a new instance of FragmentClient.



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/fragment_client.rb', line 162

def initialize(client_id, client_secret, extra_queries_filenames: nil, api_url: nil,
               oauth_url: DEFAULT_OAUTH_URL, oauth_scope: DEFAULT_OAUTH_SCOPE)
  # Both are nilable, so nil means the default rather than an assertion failure.
  @oauth_scope = T.let(oauth_scope || DEFAULT_OAUTH_SCOPE, String)
  @oauth_url = T.let(parse_oauth_url(oauth_url || DEFAULT_OAUTH_URL), URI::HTTP)
  @client_id = T.let(client_id, String)
  @client_secret = T.let(client_secret, String)

  execute = api_url ? FragmentGraphQl::CustomHTTP.new(URI.parse(api_url).to_s) : FragmentGraphQl::HTTP
  @execute = T.let(execute, GraphQL::Client::HTTP)

  @client = T.let(
    FragmentGraphQl::CustomClient.new(
      schema: FragmentGraphQl::FragmentSchema,
      execute: @execute
    ),
    FragmentGraphQl::CustomClient
  )
  @token = T.let(create_token, Token)

  define_method_from_queries(FragmentGraphQl::FragmentQueries)
  return if extra_queries_filenames.nil?

  extra_queries_filenames.each { |filename| define_method_from_queries(self.class.load_queries(filename)) }
end

Class Method Details

.configurationObject



373
374
375
# File 'lib/fragment_client.rb', line 373

def configuration
  @configuration ||= Configuration.new
end

.configure(&blk) ⇒ Object



378
379
380
# File 'lib/fragment_client.rb', line 378

def configure(&blk)
  blk.call(configuration)
end

.load_queries(*paths) ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
# File 'lib/fragment_client.rb', line 196

def self.load_queries(*paths)
  queries = paths.map do |path|
    source = File.read(path)
    parsed = T.let(FragmentGraphQl.parse_queries(path), T.untyped)
    FragmentGraphQl.record_operations(parsed)
    FragmentGraphQl.record_document(source)
    TypedEntries.load(path)
    parsed
  end
  queries.length == 1 ? queries.first : queries
end

Instance Method Details

#add_ledger_entries(variables) ⇒ Object



235
236
237
238
239
240
241
242
# File 'lib/fragment_client.rb', line 235

def add_ledger_entries(variables)
  query(
    FragmentGraphQl.operation(:AddLedgerEntries),
    variables.to_h do |name, value|
      [name, name.to_s == 'entries' ? TypedEntries.to_entry_inputs(value) : value]
    end
  )
end

#query(query, variables) ⇒ Object



251
252
253
254
# File 'lib/fragment_client.rb', line 251

def query(query, variables)
  refresh_token_if_needed
  @client.query(query, variables: variables, context: { access_token: @token.token })
end