Use inject function for resolvers and typeDefs

This commit is contained in:
ekzyis 2024-07-07 20:04:33 +02:00
parent 85cfda330b
commit a69bca0f05
2 changed files with 22 additions and 12 deletions

View File

@ -20,8 +20,8 @@ import { fetchLnAddrInvoice } from '@/lib/wallet'
export const SERVER_WALLET_DEFS = [lnd, lnAddr, cln] export const SERVER_WALLET_DEFS = [lnd, lnAddr, cln]
function walletResolvers () { function injectResolvers (resolvers) {
const resolvers = {} console.group('injected GraphQL resolvers:')
for ( for (
const w of SERVER_WALLET_DEFS) { const w of SERVER_WALLET_DEFS) {
const { const {
@ -29,7 +29,8 @@ function walletResolvers () {
server: { walletType, walletField, resolverName, testConnect } server: { walletType, walletField, resolverName, testConnect }
// app and worker import file differently // app and worker import file differently
} = w.default || w } = w.default || w
resolvers[resolverName] = async (parent, { settings, ...data }, { me, models }) => { console.log(resolverName)
resolvers.Mutation[resolverName] = async (parent, { settings, ...data }, { me, models }) => {
return await upsertWallet({ return await upsertWallet({
schema, schema,
wallet: { field: walletField, type: walletType }, wallet: { field: walletField, type: walletType },
@ -47,6 +48,8 @@ function walletResolvers () {
}, { settings, data }, { me, models }) }, { settings, data }, { me, models })
} }
} }
console.groupEnd()
return resolvers return resolvers
} }
@ -128,7 +131,7 @@ export function createHmac (hash) {
return crypto.createHmac('sha256', key).update(Buffer.from(hash, 'hex')).digest('hex') return crypto.createHmac('sha256', key).update(Buffer.from(hash, 'hex')).digest('hex')
} }
export default { const resolvers = {
Query: { Query: {
invoice: getInvoice, invoice: getInvoice,
wallet: async (parent, { id }, { me, models }) => { wallet: async (parent, { id }, { me, models }) => {
@ -459,7 +462,6 @@ export default {
} }
return { id } return { id }
}, },
...walletResolvers(),
removeWallet: async (parent, { id }, { me, models }) => { removeWallet: async (parent, { id }, { me, models }) => {
if (!me) { if (!me) {
throw new GraphQLError('you must be logged in', { extensions: { code: 'UNAUTHENTICATED' } }) throw new GraphQLError('you must be logged in', { extensions: { code: 'UNAUTHENTICATED' } })
@ -556,6 +558,8 @@ export default {
} }
} }
export default injectResolvers(resolvers)
export const addWalletLog = async ({ wallet, level, message }, { me, models }) => { export const addWalletLog = async ({ wallet, level, message }, { me, models }) => {
try { try {
await models.walletLog.create({ data: { userId: me.id, wallet: wallet.type, level, message } }) await models.walletLog.create({ data: { userId: me.id, wallet: wallet.type, level, message } })

View File

@ -1,8 +1,9 @@
import { gql } from 'graphql-tag' import { gql } from 'graphql-tag'
import { SERVER_WALLET_DEFS } from '@/api/resolvers/wallet' import { SERVER_WALLET_DEFS } from '@/api/resolvers/wallet'
function walletTypeDefs () { function injectTypeDefs (typeDefs) {
const typeDefs = SERVER_WALLET_DEFS.map( console.group('injected GraphQL type defs:')
const injected = SERVER_WALLET_DEFS.map(
(w) => { (w) => {
let args = 'id: ID, ' let args = 'id: ID, '
args += w.fields.map(f => { args += w.fields.map(f => {
@ -13,12 +14,16 @@ function walletTypeDefs () {
return arg return arg
}).join(', ') }).join(', ')
args += ', settings: AutowithdrawSettings!' args += ', settings: AutowithdrawSettings!'
return `${w.server.resolverName}(${args}): Boolean` const typeDef = `${w.server.resolverName}(${args}): Boolean`
}).join('\n') console.log(typeDef)
return typeDefs return typeDef
})
console.groupEnd()
return `${typeDefs}\n\nextend type Mutation {\n${injected.join('\n')}\n}`
} }
export default gql` const typeDefs = `
extend type Query { extend type Query {
invoice(id: ID!): Invoice! invoice(id: ID!): Invoice!
withdrawl(id: ID!): Withdrawl! withdrawl(id: ID!): Withdrawl!
@ -37,7 +42,6 @@ export default gql`
sendToLnAddr(addr: String!, amount: Int!, maxFee: Int!, comment: String, identifier: Boolean, name: String, email: String): Withdrawl! sendToLnAddr(addr: String!, amount: Int!, maxFee: Int!, comment: String, identifier: Boolean, name: String, email: String): Withdrawl!
cancelInvoice(hash: String!, hmac: String!): Invoice! cancelInvoice(hash: String!, hmac: String!): Invoice!
dropBolt11(id: ID): Withdrawl dropBolt11(id: ID): Withdrawl
${walletTypeDefs()}
removeWallet(id: ID!): Boolean removeWallet(id: ID!): Boolean
deleteWalletLogs(wallet: String): Boolean deleteWalletLogs(wallet: String): Boolean
} }
@ -141,3 +145,5 @@ export default gql`
message: String! message: String!
} }
` `
export default gql`${injectTypeDefs(typeDefs)}`