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_filenames: 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.



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/fragment_client.rb', line 82

def initialize(client_id, client_secret, extra_queries_filenames: 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_filenames.nil?

  extra_queries_filenames.each do |filename|
    queries = @client.parse(
      File.read(filename)
    )
    define_method_from_queries(queries)
  end
end

Instance Method Details

#query(query, variables) ⇒ Object



107
108
109
110
111
# File 'lib/fragment_client.rb', line 107

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