Generate wallet mutation from fields

This commit is contained in:
ekzyis 2024-07-06 04:56:28 +02:00
parent 0a0085fe82
commit 00f78daadc
4 changed files with 47 additions and 12 deletions

View File

@ -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

View File

@ -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)
}

View File

@ -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'
}

View File

@ -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!) {