【问题标题】:How to push an object into an array with GraphQL?如何使用 GraphQL 将对象推送到数组中?
【发布时间】:2018-12-21 21:44:09
【问题描述】:

我想更改解析器在我的数据库中创建支付卡的方式。所以现在我在一个集合中创建钱包,然后在另一个集合中创建付款,并在我的付款中使用 wallet_id 来链接它们。但现在我想将付款推送到钱包中定义的卡片 [] 中。知道如何在解析器中执行此操作吗?

这是我的钱包架构

const WalletSchema = new Schema({
    tokens: {
        type: Number,
        default: 0
    },
    userId: {
        type: Schema.Types.ObjectId,
        ref: 'User',
        unique: true
    },
    cards: [
        {
            type: Schema.Types.ObjectId,
            ref: 'Payment'
        }
    ]
}, { timestamps: true });

这是我的createPayment解析器

createPayment: async (_, { wallet_id, ...args }, { user }) => {
    try {
        await requireAuth(user);
        const payment = await Payment.create({ ...args, wallet: wallet_id });

        pubsub.publish(PAYMENT_ADDED, { [PAYMENT_ADDED]: payment });

        return payment;
    } catch (error) {
        throw error;
    }
},

有什么想法吗?

【问题讨论】:

    标签: javascript graphql


    【解决方案1】:

    您需要为卡支付创建另一个 Schema;

    const WalletSchema = new Schema({
        tokens: {
            type: Number,
            default: 0
        },
        userId: {
            type: Schema.Types.ObjectId,
            ref: 'User',
            unique: true
        },
        cards: [ PaymentSchema ] <-- here
    }, { timestamps: true });
    

    // 新模式

    const PaymentSchema = new Schema({
         type: Schema.Types.ObjectId,
         ref: 'Payment'
       });
    

    基于 Wallet 架构你需要编写暴露 graphql 架构

    【讨论】:

      猜你喜欢
      • 2020-04-29
      • 2015-09-02
      • 2022-11-02
      • 2020-06-21
      • 1970-01-01
      • 2018-11-21
      • 2021-01-12
      • 1970-01-01
      • 2020-05-21
      相关资源
      最近更新 更多