fragment-alpha-sdk 0.1.5 → 0.1.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/fragment.schema.json +6978 -2932
- data/lib/fragment_client.rb +28 -15
- data/lib/queries.graphql +4 -38
- metadata +2 -2
data/lib/fragment_client.rb
CHANGED
@@ -11,17 +11,17 @@ require 'sorbet-runtime'
|
|
11
11
|
# A support module for the client
|
12
12
|
module FragmentGraphQl
|
13
13
|
extend T::Sig
|
14
|
-
|
14
|
+
HTTP = T.let(GraphQL::Client.const_get(:HTTP).new('https://api.fragment.dev/graphql') do
|
15
15
|
extend T::Sig
|
16
16
|
sig { params(context: T.untyped).returns(T::Hash[T.untyped, T.untyped]) }
|
17
17
|
def headers(context)
|
18
18
|
{ 'Authorization' => 'Bearer %s' % context[:access_token] }
|
19
19
|
end
|
20
|
-
end
|
20
|
+
end, GraphQL::Client::HTTP)
|
21
21
|
|
22
22
|
Schema = T.let(GraphQL::Client.load_schema("#{__dir__}/fragment.schema.json"), T.untyped)
|
23
23
|
|
24
|
-
Client = T.let(GraphQL::Client.new(schema: Schema, execute:
|
24
|
+
Client = T.let(GraphQL::Client.new(schema: Schema, execute: HTTP), GraphQL::Client)
|
25
25
|
|
26
26
|
Queries = T.let(Client.parse(
|
27
27
|
File.read("#{__dir__}/queries.graphql")
|
@@ -32,9 +32,14 @@ end
|
|
32
32
|
class FragmentClient
|
33
33
|
extend T::Sig
|
34
34
|
|
35
|
-
sig
|
36
|
-
|
35
|
+
sig do
|
36
|
+
params(client_id: String, client_secret: String, extra_queries_filename: T.nilable(String),
|
37
|
+
execute: GraphQL::Client::HTTP).void
|
38
|
+
end
|
39
|
+
def initialize(client_id, client_secret, extra_queries_filename: nil, execute: FragmentGraphQl::HTTP)
|
40
|
+
@client = T.let(GraphQL::Client.new(schema: FragmentGraphQl::Schema, execute: execute), GraphQL::Client)
|
37
41
|
conn = create_conn(client_id, client_secret)
|
42
|
+
# TODO: the token may need to be refreshed if the client is around for a long time
|
38
43
|
@token = T.let(get_token(conn, client_id), String)
|
39
44
|
FragmentGraphQl::Queries.constants.each do |q|
|
40
45
|
name = q.to_s.gsub(/[a-z]([A-Z])/) do |m|
|
@@ -51,12 +56,16 @@ class FragmentClient
|
|
51
56
|
File.read(extra_queries_filename)
|
52
57
|
)
|
53
58
|
queries.constants.each do |q|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
59
|
+
define_method_from_query(q, queries)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def define_method_from_query(q, queries)
|
64
|
+
name = q.to_s.gsub(/[a-z]([A-Z])/) do |m|
|
65
|
+
format('%<lower>s_%<upper>s', lower: m[0], upper: m[1].downcase)
|
66
|
+
end.gsub(/^[A-Z]/, &:downcase)
|
67
|
+
define_singleton_method(name) do |v|
|
68
|
+
query(queries.const_get(q), v)
|
60
69
|
end
|
61
70
|
end
|
62
71
|
|
@@ -65,22 +74,26 @@ class FragmentClient
|
|
65
74
|
f.request :url_encoded
|
66
75
|
f.request :authorization, :basic, client_id, client_secret
|
67
76
|
f.adapter :net_http
|
77
|
+
f.response :raise_error
|
68
78
|
end, Faraday::Connection)
|
69
79
|
end
|
70
80
|
|
71
81
|
sig { params(query: T.untyped, variables: T.untyped).returns(T.untyped) }
|
72
82
|
def query(query, variables)
|
73
|
-
|
83
|
+
@client.query(query, variables: variables, context: { access_token: @token })
|
74
84
|
end
|
75
85
|
|
76
86
|
private
|
77
87
|
|
78
88
|
sig { params(conn: Faraday::Connection, client_id: String).returns(String) }
|
79
89
|
def get_token(conn, client_id)
|
80
|
-
|
81
|
-
|
90
|
+
begin
|
91
|
+
response = conn.post('/oauth2/token') do |req|
|
92
|
+
req.body = format('grant_type=client_credentials&scope=https://api.fragment.dev/*&client_id=%s', client_id)
|
93
|
+
end
|
94
|
+
rescue Faraday::ClientError => e
|
95
|
+
raise StandardError, "oauth Authentication failed: '%s'" % e.to_s
|
82
96
|
end
|
83
|
-
|
84
97
|
T.let(JSON.parse(response.body)['access_token'], String)
|
85
98
|
end
|
86
99
|
end
|
data/lib/queries.graphql
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
mutation CreateLedger($ik:
|
1
|
+
mutation CreateLedger($ik: SafeString!, $ledger: CreateLedgerInput!) {
|
2
2
|
createLedger(ik: $ik, ledger: $ledger) {
|
3
|
-
|
3
|
+
__typename
|
4
4
|
... on CreateLedgerResult {
|
5
5
|
ledger {
|
6
6
|
id
|
@@ -75,7 +75,7 @@ mutation ReconcileTx($entry: LedgerEntryInput!) {
|
|
75
75
|
}
|
76
76
|
}
|
77
77
|
|
78
|
-
mutation AddLedgerEntry($entry: LedgerEntryInput!, $ik:
|
78
|
+
mutation AddLedgerEntry($entry: LedgerEntryInput!, $ik: SafeString!) {
|
79
79
|
addLedgerEntry(entry: $entry, ik: $ik) {
|
80
80
|
__typename
|
81
81
|
... on AddLedgerEntryResult {
|
@@ -146,7 +146,7 @@ mutation UpdateLedgerAccount(
|
|
146
146
|
}
|
147
147
|
}
|
148
148
|
|
149
|
-
mutation NewCustomLink($name: String!, $ik:
|
149
|
+
mutation NewCustomLink($name: String!, $ik: SafeString!) {
|
150
150
|
createCustomLink(name: $name, ik: $ik) {
|
151
151
|
__typename
|
152
152
|
... on CreateCustomLinkResult {
|
@@ -195,24 +195,6 @@ mutation SyncBankAccounts(
|
|
195
195
|
}
|
196
196
|
}
|
197
197
|
|
198
|
-
mutation MakeBankTransfer($bankTransfer: MakeBankTransferInput!, $ik: ID!) {
|
199
|
-
makeBankTransfer(bankTransfer: $bankTransfer, ik: $ik) {
|
200
|
-
__typename
|
201
|
-
... on MakeBankTransferResult {
|
202
|
-
bankTransfer {
|
203
|
-
id
|
204
|
-
description
|
205
|
-
status
|
206
|
-
}
|
207
|
-
isIkReplay
|
208
|
-
}
|
209
|
-
... on Error {
|
210
|
-
code
|
211
|
-
message
|
212
|
-
}
|
213
|
-
}
|
214
|
-
}
|
215
|
-
|
216
198
|
query LedgerEntry($ledgerEntry: LedgerEntryMatchInput!) {
|
217
199
|
ledgerEntry(ledgerEntry: $ledgerEntry) {
|
218
200
|
id
|
@@ -222,7 +204,6 @@ query LedgerEntry($ledgerEntry: LedgerEntryMatchInput!) {
|
|
222
204
|
description
|
223
205
|
workspaceId
|
224
206
|
ledgerId
|
225
|
-
orderId
|
226
207
|
lines {
|
227
208
|
nodes {
|
228
209
|
id
|
@@ -231,21 +212,6 @@ query LedgerEntry($ledgerEntry: LedgerEntryMatchInput!) {
|
|
231
212
|
}
|
232
213
|
}
|
233
214
|
|
234
|
-
# copied from https://fragment.dev/api-reference#query-bankTransfer
|
235
|
-
query BankTransfer($bankTransfer: BankTransferMatchInput!) {
|
236
|
-
bankTransfer(bankTransfer: $bankTransfer) {
|
237
|
-
id
|
238
|
-
description
|
239
|
-
status
|
240
|
-
order {
|
241
|
-
id
|
242
|
-
description
|
243
|
-
entryIds
|
244
|
-
stateMachine
|
245
|
-
}
|
246
|
-
}
|
247
|
-
}
|
248
|
-
|
249
215
|
# copied from https://fragment.dev/api-reference#query-ledger
|
250
216
|
query Ledger($ledger: LedgerMatchInput!) {
|
251
217
|
ledger(ledger: $ledger) {
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fragment-alpha-sdk
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- fragment
|
@@ -81,7 +81,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
83
|
requirements: []
|
84
|
-
rubygems_version: 3.
|
84
|
+
rubygems_version: 3.4.10
|
85
85
|
signing_key:
|
86
86
|
specification_version: 4
|
87
87
|
summary: an alpha version for the fragment client sdk
|