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]
function walletResolvers () {
const resolvers = {}
function injectResolvers (resolvers) {
console.group('injected GraphQL resolvers:')
for (
const w of SERVER_WALLET_DEFS) {
const {
@ -29,7 +29,8 @@ function walletResolvers () {
server: { walletType, walletField, resolverName, testConnect }
// app and worker import file differently
} = 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({
schema,
wallet: { field: walletField, type: walletType },
@ -47,6 +48,8 @@ function walletResolvers () {
}, { settings, data }, { me, models })
}
}
console.groupEnd()
return resolvers
}
@ -128,7 +131,7 @@ export function createHmac (hash) {
return crypto.createHmac('sha256', key).update(Buffer.from(hash, 'hex')).digest('hex')
}
export default {
const resolvers = {
Query: {
invoice: getInvoice,
wallet: async (parent, { id }, { me, models }) => {
@ -459,7 +462,6 @@ export default {
}
return { id }
},
...walletResolvers(),
removeWallet: async (parent, { id }, { me, models }) => {
if (!me) {
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 }) => {
try {
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 { SERVER_WALLET_DEFS } from '@/api/resolvers/wallet'
function walletTypeDefs () {
const typeDefs = SERVER_WALLET_DEFS.map(
function injectTypeDefs (typeDefs) {
console.group('injected GraphQL type defs:')
const injected = SERVER_WALLET_DEFS.map(
(w) => {
let args = 'id: ID, '
args += w.fields.map(f => {
@ -13,12 +14,16 @@ function walletTypeDefs () {
return arg
}).join(', ')
args += ', settings: AutowithdrawSettings!'
return `${w.server.resolverName}(${args}): Boolean`
}).join('\n')
return typeDefs
const typeDef = `${w.server.resolverName}(${args}): Boolean`
console.log(typeDef)
return typeDef
})
console.groupEnd()
return `${typeDefs}\n\nextend type Mutation {\n${injected.join('\n')}\n}`
}
export default gql`
const typeDefs = `
extend type Query {
invoice(id: ID!): Invoice!
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!
cancelInvoice(hash: String!, hmac: String!): Invoice!
dropBolt11(id: ID): Withdrawl
${walletTypeDefs()}
removeWallet(id: ID!): Boolean
deleteWalletLogs(wallet: String): Boolean
}
@ -141,3 +145,5 @@ export default gql`
message: String!
}
`
export default gql`${injectTypeDefs(typeDefs)}`