|
| 1 | +import { GraphQLUtils } from './utils.js'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Minimal GraphQL transport layer responsible for broadcasting signed |
| 5 | + * payments to a Mina daemon and inspecting the transaction pool. |
| 6 | + */ |
| 7 | +export class GraphQLClient { |
| 8 | + constructor(url) { |
| 9 | + this.url = url; |
| 10 | + } |
| 11 | + |
| 12 | + /** |
| 13 | + * Posts a signed payment mutation to the configured GraphQL endpoint. |
| 14 | + * Surfaces detailed errors while preserving the structured response |
| 15 | + * the caller uses to confirm transaction submission. |
| 16 | + */ |
| 17 | + async sendPayment(signedPayment) { |
| 18 | + const query = GraphQLUtils.createPaymentMutation(signedPayment); |
| 19 | + |
| 20 | + console.log('\n🚀 Sending payment via GraphQL'); |
| 21 | + console.log(`🌐 Endpoint: ${this.url}`); |
| 22 | + console.log('📝 Mutation payload:'); |
| 23 | + console.log(query); |
| 24 | + |
| 25 | + try { |
| 26 | + const response = await fetch(this.url, { |
| 27 | + method: 'POST', |
| 28 | + headers: { 'Content-Type': 'application/json' }, |
| 29 | + body: JSON.stringify({ operationName: null, query, variables: {} }), |
| 30 | + }); |
| 31 | + |
| 32 | + return await this.handleResponse(response); |
| 33 | + } catch (error) { |
| 34 | + throw new Error(`Request error: ${error.message}`); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Normalizes the GraphQL response shape by either returning JSON data |
| 40 | + * or throwing a rich error that upstream callers can surface. |
| 41 | + */ |
| 42 | + async handleResponse(response) { |
| 43 | + if (response.status === 200) { |
| 44 | + const rawBody = await response.text(); |
| 45 | + |
| 46 | + let json; |
| 47 | + try { |
| 48 | + json = JSON.parse(rawBody); |
| 49 | + } catch (parseError) { |
| 50 | + throw new Error( |
| 51 | + `Unexpected JSON payload: ${parseError.message}. Raw response: ${rawBody}` |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + if (json.errors?.length) { |
| 56 | + const combinedErrors = json.errors |
| 57 | + .map(error => error.message ?? JSON.stringify(error)) |
| 58 | + .join(' | '); |
| 59 | + throw new Error(`GraphQL errors: ${combinedErrors}`); |
| 60 | + } |
| 61 | + |
| 62 | + console.log('📦 GraphQL response payload:'); |
| 63 | + console.dir(json, { depth: null }); |
| 64 | + return json; |
| 65 | + } else { |
| 66 | + const text = await response.text(); |
| 67 | + throw new Error(`GraphQL error (${response.status}): ${text}`); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Queries the daemon's pooled commands and returns true when the given |
| 73 | + * transaction ID is currently staged for inclusion in a block. |
| 74 | + */ |
| 75 | + async checkTransactionInPool(transactionId) { |
| 76 | + const query = ` |
| 77 | + query MyQuery { |
| 78 | + pooledUserCommands { |
| 79 | + id |
| 80 | + } |
| 81 | + } |
| 82 | + `; |
| 83 | + |
| 84 | + try { |
| 85 | + const response = await fetch(this.url, { |
| 86 | + method: 'POST', |
| 87 | + headers: { 'Content-Type': 'application/json' }, |
| 88 | + body: JSON.stringify({ |
| 89 | + operationName: 'MyQuery', |
| 90 | + query, |
| 91 | + variables: {} |
| 92 | + }), |
| 93 | + }); |
| 94 | + |
| 95 | + const rawBody = await response.text(); |
| 96 | + if (response.status !== 200) { |
| 97 | + throw new Error(`GraphQL error (${response.status}): ${rawBody}`); |
| 98 | + } |
| 99 | + |
| 100 | + let json; |
| 101 | + try { |
| 102 | + json = JSON.parse(rawBody); |
| 103 | + } catch (parseError) { |
| 104 | + throw new Error( |
| 105 | + `Unexpected JSON payload when checking pool: ${parseError.message}. Raw response: ${rawBody}` |
| 106 | + ); |
| 107 | + } |
| 108 | + |
| 109 | + if (json.errors?.length) { |
| 110 | + const combinedErrors = json.errors |
| 111 | + .map(error => error.message ?? JSON.stringify(error)) |
| 112 | + .join(' | '); |
| 113 | + throw new Error(`GraphQL errors while checking pool: ${combinedErrors}`); |
| 114 | + } |
| 115 | + |
| 116 | + const pooledCommands = json.data?.pooledUserCommands || []; |
| 117 | + return pooledCommands.some(command => command.id === transactionId); |
| 118 | + } catch (error) { |
| 119 | + console.error('Error checking transaction in pool:', error.message); |
| 120 | + throw error; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Convenience method that lists transaction IDs in the current pool. |
| 126 | + * Useful for manual debugging or exploratory scripts. |
| 127 | + */ |
| 128 | + async getPooledUserCommands() { |
| 129 | + const query = ` |
| 130 | + query MyQuery { |
| 131 | + pooledUserCommands { |
| 132 | + id |
| 133 | + } |
| 134 | + } |
| 135 | + `; |
| 136 | + |
| 137 | + try { |
| 138 | + const response = await fetch(this.url, { |
| 139 | + method: 'POST', |
| 140 | + headers: { 'Content-Type': 'application/json' }, |
| 141 | + body: JSON.stringify({ |
| 142 | + operationName: 'MyQuery', |
| 143 | + query, |
| 144 | + variables: {} |
| 145 | + }), |
| 146 | + }); |
| 147 | + |
| 148 | + const rawBody = await response.text(); |
| 149 | + if (response.status !== 200) { |
| 150 | + throw new Error(`GraphQL error (${response.status}): ${rawBody}`); |
| 151 | + } |
| 152 | + |
| 153 | + let json; |
| 154 | + try { |
| 155 | + json = JSON.parse(rawBody); |
| 156 | + } catch (parseError) { |
| 157 | + throw new Error( |
| 158 | + `Unexpected JSON payload when fetching pooled commands: ${parseError.message}. Raw response: ${rawBody}` |
| 159 | + ); |
| 160 | + } |
| 161 | + |
| 162 | + if (json.errors?.length) { |
| 163 | + const combinedErrors = json.errors |
| 164 | + .map(error => error.message ?? JSON.stringify(error)) |
| 165 | + .join(' | '); |
| 166 | + throw new Error(`GraphQL errors while fetching pooled commands: ${combinedErrors}`); |
| 167 | + } |
| 168 | + |
| 169 | + console.log('📦 Pooled commands response payload:'); |
| 170 | + console.dir(json, { depth: null }); |
| 171 | + return json.data?.pooledUserCommands || []; |
| 172 | + } catch (error) { |
| 173 | + console.error('Error fetching pooled commands:', error.message); |
| 174 | + throw error; |
| 175 | + } |
| 176 | + } |
| 177 | +} |
0 commit comments