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: AuthenticationError, Configuration, NetworkError, ResponseError, Token, TokenExpiredError

Class Method Summary collapse

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.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/fragment_client.rb', line 87

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(
    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 do |filename|
    queries = @client.parse(
      File.read(filename)
    )
    define_method_from_queries(queries)
  end
end

Class Method Details

.configurationObject



193
194
195
# File 'lib/fragment_client.rb', line 193

def configuration
  @configuration ||= Configuration.new
end

.configure {|configuration| ... } ⇒ Object

Yields:



198
199
200
# File 'lib/fragment_client.rb', line 198

def configure(&blk)
  yield(configuration)
end

Instance Method Details

#query(query, variables) ⇒ Object



124
125
126
127
# File 'lib/fragment_client.rb', line 124

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