gpt4 book ai didi

javascript - 在 Apollo Server 中的解析器和变更之间共享逻辑

转载 作者:行者123 更新时间:2023-12-02 21:39:26 25 4
gpt4 key购买 nike

这是我的解析器文件:

import { gql } from 'apollo-server-express'
import * as db from '../database'

export const typeDefs = gql`
extend type Query {
getFoos(): Foo
}

type Foo {
id: ID!
missingBar: [String]
}

input addBarInput {
fooId: ID!
barId: ID!
}

extend type Mutation {
addBar(input: addBarInput): Foo
startFoo(fooId: ID!): Foo
}
`

export const resolvers = {
Query: {
getFoos: async () => db.foo.findAll({include: [db.bar]}),
},
Foo: {
missingBar: async (obj, args, context, info) => {
// here starts the relevant part of code
const someBarIds = await db.some_table.findAll({
where: {bar_id: obj.bar_id},
}).map(el => el.bar_id)

const otherBarIds = await db.other_table.findAll({
where: {id: obj.bars.map(b => b.id)},
}).map(el => el.id)

const missingBarIds = someBarIds.filter(x => !otherBarIds.includes(x));
// here ends the relevant part of code

return await db.instruments.findAll({where: {id: missingInstrumentsIds}}).map(instrument => instrument.name)
},
},
Mutation: {
addBar: async (prev, { input }) => {
const foo = await db.foo.findByPk(input.fooId)

const bar = await db.users.findByPk(input.userId)
await bar.setBar(foo)

return foo
},
startFoo: async (prev, { input }) => {
const foo = await db.foo.findByPk(input.fooId)

// here I would like to have the code from missingBar without copypasting it
// then I need to do more logic (irrelevant for this question) using that missingBarIds array

return foo
},
}
}

作为注释,我不认为 missingBar 中有精确的 Sequelize 逻辑。解析器对于问题的核心很重要,所以我简化了它。其要点是,仅当所有必需的元素已添加到 foo 时,我才想在数据库上执行更新。 ,并且该检查的执行逻辑与 missingBar 中的逻辑完全相同。解析器。

有没有办法在不同的突变和解析器之间共享部分逻辑?理想情况下,在某处声明一个函数并在两个地方使用它?

在这种情况下,这个假设函数的唯一参数将是 foo 的实例。对象(这是一个 Sequelize 模型,但我再次认为这在这种情况下并不真正相关),返回值将是 missingBarIds数组。

最佳答案

在较高层次上,您可以编写一个函数并在两个解析器中使用它。如果您不想将其放在同一个模块中,请将其放在不同的模块中并导入。这没有什么问题。

也就是说,由于您使用的是 Sequelize,因此您可以将静态方法和实例方法添加到模型类中。

静态方法可以这样写:

Foo.doSomething = function doSomething () {
...
}

并使用模型类调用:

Foo.doSomething()

实例方法可以这样写:

Foo.prototype.doSomething = function doSomething () {
...
}

然后在类的实例上调用:

const foo = await Foo.findOne()
foo.doSomething()

请注意,当您定义实例方法时,只要不使用箭头函数,this 就会引用实例本身。

关于javascript - 在 Apollo Server 中的解析器和变更之间共享逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60405088/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com