Class: FragmentClient

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

Overview

A client for Fragment

Defined Under Namespace

Classes: Token

Instance Method Summary collapse

Constructor Details

#initialize(client_id, client_secret, extra_queries_filename: nil, api_url: nil, oauth_url: 'https://auth.fragment.dev/oauth2/token', oauth_scope: 'https://api.fragment.dev/*') ⇒ FragmentClient

Returns a new instance of FragmentClient.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/fragment_client.rb', line 51

def initialize(client_id, client_secret, extra_queries_filename: nil, api_url: nil,
               oauth_url: 'https://auth.fragment.dev/oauth2/token', oauth_scope: 'https://api.fragment.dev/*')
  @oauth_scope = T.let(oauth_scope, String)
  @oauth_url = T.let(URI.parse(oauth_url), URI)
  @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(GraphQL::Client.new(schema: FragmentGraphQl::FragmentSchema, execute: @execute), GraphQL::Client)
  @token = T.let(create_token, Token)

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

  queries = @client.parse(
    File.read(extra_queries_filename)
  )
  define_method_from_queries(queries)
end

Instance Method Details

#query(query, variables) ⇒ Object



74
75
76
77
78
79
# File 'lib/fragment_client.rb', line 74

def query(query, variables)
  expiry_time_skew = 120
  @token = create_token if Time.now > @token.expires_at - expiry_time_skew
  puts query
  @client.query(query, variables: variables, context: { access_token: @token.token })
end