-
Notifications
You must be signed in to change notification settings - Fork 5
/
schema.py
138 lines (110 loc) · 3.75 KB
/
schema.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import json
import graphene
from graphene.types.generic import GenericScalar
from bigchaindb_driver import BigchainDB
from bigchaindb_driver.exceptions import NotFoundError
class OutputType(graphene.ObjectType):
name = 'Output'
description ='...'
condition = GenericScalar()
public_keys = graphene.List(graphene.String)
amount = graphene.String()
@classmethod
def from_json(cls, output):
return cls(**output)
class InputType(graphene.ObjectType):
name = 'Input'
description = '...'
owners_before = graphene.List(graphene.String)
fulfillment = graphene.String()
fulfills = graphene.Field(lambda: FulfillsType)
@classmethod
def from_json(cls, input_):
fulfills = FulfillsType.from_json(input_['fulfills'])
return cls(
owners_before=input_['owners_before'],
fulfillment=input_['fulfillment'],
fulfills=fulfills
)
class TransactionType(graphene.ObjectType):
name = 'Transaction'
description = '...'
id = graphene.String()
operation = graphene.String()
version = graphene.String()
asset = GenericScalar()
metadata = GenericScalar()
inputs = graphene.List(InputType)
outputs = graphene.List(OutputType)
@classmethod
def from_json(cls, retrieved_tx):
outputs = [
OutputType.from_json(output) for output in retrieved_tx['outputs']
]
inputs = [
InputType.from_json(input_) for input_ in retrieved_tx['inputs']
]
return cls(
id=retrieved_tx['id'],
version=retrieved_tx['version'],
inputs=inputs,
outputs=outputs,
operation=retrieved_tx['operation'],
asset=retrieved_tx['asset'],
metadata=retrieved_tx['metadata'],
)
class FulfillsType(graphene.ObjectType):
name = 'Fulfills'
description = '...'
output_index = graphene.Int()
transaction = graphene.Field(TransactionType)
@classmethod
def from_json(cls, fulfills):
bdb = BigchainDB()
try:
retrieved_tx = bdb.transactions.retrieve(fulfills['transaction_id'])
return cls(
output_index=fulfills['output_index'],
transaction=TransactionType.from_json(retrieved_tx)
)
except (KeyError, TypeError, NotFoundError):
return cls(
output_index=None,
transaction=None
)
class QueryType(graphene.ObjectType):
name = 'Query'
description = '...'
transaction = graphene.Field(
TransactionType,
id=graphene.String()
)
transactions = graphene.Field(
graphene.List(TransactionType),
asset_id=graphene.String(),
operation=graphene.String()
)
outputs = graphene.Field(
graphene.List(FulfillsType),
public_key=graphene.String()
)
def resolve_transaction(self, args, context, info):
bdb = BigchainDB()
txid = args.get('id')
retrieved_tx = bdb.transactions.retrieve(txid)
return TransactionType.from_json(retrieved_tx)
def resolve_transactions(self, args, context, info):
bdb = BigchainDB()
asset_id = args.get('asset_id')
operation = args.get('operation', None)
retrieved_txs = bdb.transactions.get(asset_id=asset_id,
operation=operation)
return [TransactionType.from_json(tx) for tx in retrieved_txs]
def resolve_outputs(self, args, context, info):
bdb = BigchainDB()
public_key = args.get('public_key')
outputs = bdb.outputs.get(public_key)
return [FulfillsType.from_json(output) for output in outputs]
schema = graphene.Schema(
query = QueryType
)