From 00f78daadc36415524f0a75f24101aca28c2160a Mon Sep 17 00:00:00 2001 From: ekzyis Date: Sat, 6 Jul 2024 04:56:28 +0200 Subject: [PATCH] Generate wallet mutation from fields --- api/typeDefs/wallet.js | 20 +++++++++++++++++++- components/wallet/index.js | 29 +++++++++++++++++++++++++++-- components/wallet/lnd.js | 3 +-- fragments/wallet.js | 7 ------- 4 files changed, 47 insertions(+), 12 deletions(-) diff --git a/api/typeDefs/wallet.js b/api/typeDefs/wallet.js index 00fe0cc5..e4164549 100644 --- a/api/typeDefs/wallet.js +++ b/api/typeDefs/wallet.js @@ -1,4 +1,22 @@ import { gql } from 'graphql-tag' +import { SERVER_WALLET_DEFS } from '@/components/wallet' + +function walletTypeDefs () { + const typeDefs = SERVER_WALLET_DEFS.map( + (w) => { + let args = 'id: ID, ' + args += w.fields.map(f => { + let arg = `${f.name}: String` + if (!f.optional) { + arg += '!' + } + return arg + }).join(', ') + args += ', settings: AutowithdrawSettings!' + return `${w.server.resolverName}(${args}): Boolean` + }).join('\n') + return typeDefs +} export default gql` extend type Query { @@ -19,7 +37,7 @@ export default gql` sendToLnAddr(addr: String!, amount: Int!, maxFee: Int!, comment: String, identifier: Boolean, name: String, email: String): Withdrawl! cancelInvoice(hash: String!, hmac: String!): Invoice! dropBolt11(id: ID): Withdrawl - upsertWalletLND(id: ID, socket: String!, macaroon: String!, cert: String, settings: AutowithdrawSettings!): Boolean + ${walletTypeDefs()} upsertWalletCLN(id: ID, socket: String!, rune: String!, cert: String, settings: AutowithdrawSettings!): Boolean upsertWalletLNAddr(id: ID, address: String!, settings: AutowithdrawSettings!): Boolean removeWallet(id: ID!): Boolean diff --git a/components/wallet/index.js b/components/wallet/index.js index 4f0bf392..12f18e3f 100644 --- a/components/wallet/index.js +++ b/components/wallet/index.js @@ -9,13 +9,15 @@ import * as lnbits from '@/components/wallet/lnbits' import * as nwc from '@/components/wallet/nwc' import * as lnc from '@/components/wallet/lnc' import * as lnd from '@/components/wallet/lnd' -import { useApolloClient, useQuery } from '@apollo/client' +import { gql, useApolloClient, useQuery } from '@apollo/client' import { REMOVE_WALLET, WALLET_BY_TYPE } from '@/fragments/wallet' import { autowithdrawInitial } from '../autowithdraw-shared' // wallet definitions export const WALLET_DEFS = [lnbits, nwc, lnc, lnd] +export const SERVER_WALLET_DEFS = WALLET_DEFS.filter(w => w.server) + export const Status = { Initialized: 'Initialized', Enabled: 'Enabled', @@ -177,8 +179,9 @@ function useServerConfig (wallet) { ...config }) => { try { + const mutation = generateMutation(wallet) return await client.mutate({ - mutation: wallet.server.mutation, + mutation, variables: { id: walletId, ...config, @@ -211,6 +214,28 @@ function useServerConfig (wallet) { return [config, saveConfig, clearConfig] } +function generateMutation (wallet) { + const { resolverName } = wallet.server + + let headerArgs = '$id: ID, ' + headerArgs += wallet.fields.map(f => { + let arg = `$${f.name}: String` + if (!f.optional) { + arg += '!' + } + return arg + }).join(', ') + headerArgs += ', $settings: AutowithdrawSettings!' + + let inputArgs = 'id: $id, ' + inputArgs += wallet.fields.map(f => `${f.name}: $${f.name}`).join(', ') + inputArgs += ', settings: $settings' + + return gql`mutation ${resolverName}(${headerArgs}) { + ${resolverName}(${inputArgs}) + }` +} + export function getWalletByName (name) { return WALLET_DEFS.find(def => def.name === name) } diff --git a/components/wallet/lnd.js b/components/wallet/lnd.js index 358a6d9b..87c8350a 100644 --- a/components/wallet/lnd.js +++ b/components/wallet/lnd.js @@ -1,4 +1,3 @@ -import { UPSERT_WALLET_LND } from '@/fragments/wallet' import { LNDAutowithdrawSchema } from '@/lib/validate' export const name = 'lnd' @@ -44,5 +43,5 @@ export const schema = LNDAutowithdrawSchema export const server = { walletType: 'LND', - mutation: UPSERT_WALLET_LND + resolverName: 'upsertWalletLND' } diff --git a/fragments/wallet.js b/fragments/wallet.js index 329e6c1d..24b352c6 100644 --- a/fragments/wallet.js +++ b/fragments/wallet.js @@ -107,13 +107,6 @@ mutation upsertWalletLNAddr($id: ID, $address: String!, $settings: AutowithdrawS } ` -export const UPSERT_WALLET_LND = -gql` -mutation upsertWalletLND($id: ID, $socket: String!, $macaroon: String!, $cert: String, $settings: AutowithdrawSettings!) { - upsertWalletLND(id: $id, socket: $socket, macaroon: $macaroon, cert: $cert, settings: $settings) -} -` - export const UPSERT_WALLET_CLN = gql` mutation upsertWalletCLN($id: ID, $socket: String!, $rune: String!, $cert: String, $settings: AutowithdrawSettings!) {