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

标签 javascript node.js apollo-server

这是我的解析器文件:

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/

相关文章:

javascript - 我无法在自定义的 tumblr 页面上使用过滤器

javascript - 小书签的执行环境与浏览器控制台相同吗?

mongodb - Node.js connect-mongo数据库连接问题

css - 如果指定字体格式,字体将不起作用

node.js - apollo-server-express CORS 问题

javascript - 将元素从一个 div 移动到另一个 div

javascript - ckeditor getData() 似乎没有正常工作

javascript - 如何使用tcp套接字将图像从[python客户端]发送到[node.js服务器]?

ecmascript-6 - 尝试使用 babel 构建 apollo-server 返回语法错误

graphql - 如何使用 apollo-server 进行缓存