fragment-alpha-sdk 0.1.8 → 0.1.11

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 62a4d3cb2aa95e912261ccb28b7bc7a9b406e16e87beec88eea1f49706c3ffe0
4
- data.tar.gz: 3e13aefb9c50d3301a048f28a4e5220bab037e223aad43fd508cb9c687896526
3
+ metadata.gz: 7e8f99cd2f06c14ecf62e7013f975abc3246c7807b3f76a8b778fccaf547e55c
4
+ data.tar.gz: 7733c9007935bbce62ead495ac49afb354e64a3eab845c1b346ec353b3938958
5
5
  SHA512:
6
- metadata.gz: 137cb1729e102a1aa668a4e7076435146701d99fd3f274cfef777580c3adb79f115bec45af56453eb46ad2ed3efe89e153d419e0da37fdd1bd6d96673e923865
7
- data.tar.gz: 817d73d5ac8a07b940d0ddd5bb760a3417dac4ba80995604b82c82bcd68a152a46cb43945d7ba1fb794ee788d55bca2672bca5893deed1a317f7afe571955fec
6
+ metadata.gz: e4290cb21848714c3a7b36ce18d7039a86fd79892c46ebeef200675ccae88f20b04c8f773ccfefb690e68fa222fde6f74578136fc5fe38a6dc08fd01a91767b5
7
+ data.tar.gz: db6b63a08a2f9e25075314c222b1ca82c3c87354fca7d184546e75244946935b3951ae26989b7d6a1f2844c83803586b5304deace462e0a087266fc984ed8dec
@@ -1,4 +1,4 @@
1
- # typed: strict
1
+ # typed: false
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require 'faraday'
@@ -7,25 +7,29 @@ require 'json'
7
7
  require 'graphql/client'
8
8
  require 'graphql/client/http'
9
9
  require 'sorbet-runtime'
10
+ require 'uri'
10
11
 
11
12
  # A support module for the client
12
13
  module FragmentGraphQl
13
14
  extend T::Sig
14
- HTTP = T.let(GraphQL::Client.const_get(:HTTP).new('https://api.fragment.dev/graphql') do
15
+
16
+ CustomHTTP = Class.new(GraphQL::Client::HTTP) do
15
17
  extend T::Sig
16
18
  sig { params(context: T.untyped).returns(T::Hash[T.untyped, T.untyped]) }
17
19
  def headers(context)
18
20
  { 'Authorization' => format('Bearer %s', context[:access_token]) }
19
21
  end
20
- end, GraphQL::Client::HTTP)
22
+ end
23
+
24
+ HTTP = T.let(CustomHTTP.new('https://api.fragment.dev/graphql'), GraphQL::Client::HTTP)
21
25
 
22
- Schema = T.let(GraphQL::Client.load_schema("#{__dir__}/fragment.schema.json"), T.untyped)
26
+ FragmentSchema = T.let(GraphQL::Client.load_schema("#{__dir__}/fragment.schema.json"), T.untyped)
23
27
 
24
- Client = T.let(GraphQL::Client.new(schema: Schema, execute: HTTP), GraphQL::Client)
28
+ Client = T.let(GraphQL::Client.new(schema: FragmentSchema, execute: HTTP), GraphQL::Client)
25
29
 
26
- Queries = T.let(Client.parse(
27
- File.read("#{__dir__}/queries.graphql")
28
- ), T.untyped)
30
+ FragmentQueries = T.let(Client.parse(
31
+ File.read("#{__dir__}/queries.graphql")
32
+ ), T.untyped)
29
33
  end
30
34
 
31
35
  # A client for Fragment
@@ -34,26 +38,41 @@ class FragmentClient
34
38
 
35
39
  sig do
36
40
  params(client_id: String, client_secret: String, extra_queries_filename: T.nilable(String),
37
- execute: GraphQL::Client::HTTP).void
41
+ execute: T.nilable(GraphQL::Client::HTTP), api_url: T.nilable(String),
42
+ oauth_url: T.nilable(String), oauth_scope: T.nilable(String)).void
38
43
  end
39
- def initialize(client_id, client_secret, extra_queries_filename: nil, execute: FragmentGraphQl::HTTP)
40
- set_instance_variables(execute, client_id, client_secret)
41
- define_method_from_queries(FragmentGraphQl::Queries)
44
+ def initialize(client_id, client_secret, extra_queries_filename: nil, execute: nil, api_url: nil,
45
+ oauth_url: 'https://auth.fragment.dev/oauth2/token', oauth_scope: 'https://api.fragment.dev/*')
46
+ @oauth_scope = T.let(oauth_scope, String)
47
+ @oauth_url = T.let(URI.parse(oauth_url), URI)
48
+ @client_id = T.let(client_id, String)
49
+ @client_secret = T.let(client_secret, String)
50
+
51
+ execute ||= api_url ? FragmentGraphQl::CustomHTTP.new(URI.parse(api_url)) : FragmentGraphQl::HTTP
52
+ @execute = T.let(execute, GraphQL::Client::HTTP)
53
+
54
+ @client = T.let(GraphQL::Client.new(schema: FragmentGraphQl::FragmentSchema, execute: @execute), GraphQL::Client)
55
+ @conn = T.let(create_conn, Faraday::Connection)
56
+ # TODO: the token may need to be refreshed if the client is around for a long time
57
+ @token = T.let(create_token, String)
58
+
59
+ define_method_from_queries(FragmentGraphQl::FragmentQueries)
42
60
  return if extra_queries_filename.nil?
43
61
 
44
- queries = FragmentGraphQl::Client.parse(
62
+ queries = @client.parse(
45
63
  File.read(extra_queries_filename)
46
64
  )
47
65
  define_method_from_queries(queries)
48
66
  end
49
67
 
50
- def set_instance_variables(execute, client_id, client_secret)
51
- @client = T.let(GraphQL::Client.new(schema: FragmentGraphQl::Schema, execute: execute), GraphQL::Client)
52
- conn = create_conn(client_id, client_secret)
53
- # TODO: the token may need to be refreshed if the client is around for a long time
54
- @token = T.let(get_token(conn, client_id), String)
68
+
69
+ sig { params(query: T.untyped, variables: T.untyped).returns(T.untyped) }
70
+ def query(query, variables)
71
+ @client.query(query, variables: variables, context: { access_token: @token })
55
72
  end
56
73
 
74
+ private
75
+
57
76
  def define_method_from_queries(queries)
58
77
  queries.constants.each do |qry|
59
78
  name = qry.to_s.gsub(/[a-z]([A-Z])/) do |m|
@@ -65,27 +84,22 @@ class FragmentClient
65
84
  end
66
85
  end
67
86
 
68
- def create_conn(client_id, client_secret)
69
- T.let(Faraday.new('https://auth.fragment.dev') do |f|
87
+ sig { returns(Faraday::Connection) }
88
+ def create_conn
89
+ T.let(Faraday.new(@oauth_url) do |f|
70
90
  f.request :url_encoded
71
- f.request :authorization, :basic, client_id, client_secret
91
+ f.request :authorization, :basic, @client_id, @client_secret
72
92
  f.adapter :net_http
73
93
  f.response :raise_error
74
94
  end, Faraday::Connection)
75
95
  end
76
96
 
77
- sig { params(query: T.untyped, variables: T.untyped).returns(T.untyped) }
78
- def query(query, variables)
79
- @client.query(query, variables: variables, context: { access_token: @token })
80
- end
81
-
82
- private
83
-
84
- sig { params(conn: Faraday::Connection, client_id: String).returns(String) }
85
- def get_token(conn, client_id)
97
+ sig { returns(String) }
98
+ def create_token
86
99
  begin
87
- response = conn.post('/oauth2/token') do |req|
88
- req.body = format('grant_type=client_credentials&scope=https://api.fragment.dev/*&client_id=%s', client_id)
100
+ response = @conn.post do |req|
101
+ req.body = format('grant_type=client_credentials&scope=%<scope>s&client_id=%<id>s', scope: @oauth_scope,
102
+ id: @client_id)
89
103
  end
90
104
  rescue Faraday::ClientError => e
91
105
  raise StandardError, format("oauth Authentication failed: '%s'", e.to_s)
data/lib/queries.graphql CHANGED
@@ -1,13 +1,23 @@
1
1
  mutation StoreSchema($schema: SchemaInput!) {
2
2
  storeSchema(schema: $schema) {
3
- schema {
4
- key
5
- name
6
- version {
7
- created
8
- version
3
+ ... on StoreSchemaResult {
4
+ schema {
5
+ key
6
+ name
7
+ version {
8
+ created
9
+ version
10
+ }
9
11
  }
10
12
  }
13
+ ... on BadRequestError {
14
+ code
15
+ message
16
+ }
17
+ ... on InternalError {
18
+ code
19
+ message
20
+ }
11
21
  }
12
22
  }
13
23
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fragment-alpha-sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
4
+ version: 0.1.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - fragment
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-02-09 00:00:00.000000000 Z
11
+ date: 1980-01-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -52,7 +52,7 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0.5'
55
- description:
55
+ description:
56
56
  email: snoble@fragment.dev
57
57
  executables: []
58
58
  extensions: []
@@ -66,7 +66,7 @@ homepage: https://fragment.dev
66
66
  licenses:
67
67
  - Nonstandard
68
68
  metadata: {}
69
- post_install_message:
69
+ post_install_message:
70
70
  rdoc_options: []
71
71
  require_paths:
72
72
  - lib
@@ -81,8 +81,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
83
  requirements: []
84
- rubygems_version: 3.0.3.1
85
- signing_key:
84
+ rubygems_version: 3.4.10
85
+ signing_key:
86
86
  specification_version: 4
87
87
  summary: an alpha version for the fragment client sdk
88
88
  test_files: []