首页 > 解决方案 > GraphQL 订阅错误:“\”properties\“参数必须是数组类型。接收类型对象”

问题描述

我正在尝试使用 GraphQL 实现一个简单的 API。我的查询和我的突变已经到位并且正在工作,但现在我也在尝试包括订阅

我已经在模式中添加了订阅,我在addUser突变中包含了事件发布,并为订阅类型定义了订阅函数。

现在,当我尝试graphiql在浏览器内 IDE 中运行订阅查询时,我收到此错误:

"\"properties\" 参数必须是数组类型。接收类型对象"

附加的是模式对象。我是不是配置错了什么或者我错过了什么?谢谢!

PS 我还需要提到我正在使用mongoose将数据存储在一个 mongo 实例上,因此是实体。

import {
    GraphQLFloat,
    GraphQLID,
    GraphQLInt,
    GraphQLList,
    GraphQLNonNull,
    GraphQLObjectType,
    GraphQLSchema,
    GraphQLString
} from 'graphql';
// models
import UserType from '../types/user/UserType';
import AccountType from '../types/account/AccountType';
import TransactionType from '../types/transaction/TransactionType';
// entities
import User from '../entities/user/user';
import Account from '../entities/account/account';
import Transaction from '../entities/transaction/transaction';
// subscriptions
import { PubSub } from 'graphql-subscriptions';

// subscriptions
const pubsub = new PubSub();
const USER_CREATED = 'user_created';

// the acceptable starting point of our graph
const RootQueryType = new GraphQLObjectType({
    name: 'RootQueryType',
    fields: () => ({
        // query individual entities in the database
        user: {
            type: UserType,
            description: 'The current user identified by an id',
            args: {
                id: {
                    type: GraphQLID
                }
            },
            resolve(parent, args) {
                return User.findById(args.id);
            }
        },
        account: {
            type: AccountType,
            description: 'Details about the account in question identified by an id',
            args: {
                id: {
                    type: GraphQLID
                }
            },
            resolve(parent, args) {
                return Account.findById(args.id);
            }
        },
        transaction: {
            type: TransactionType,
            description: 'Details about the transaction in question identified by an id',
            args: {
                id: {
                    type: GraphQLID
                }
            },
            resolve(parent, args) {
                return Transaction.findById(args.id);
            }
        },
        // query all entities in the database
        users: {
            type: new GraphQLList(UserType),
            resolve: (parent, args) => {
                return User.find({});
            }
        },
        accounts: {
            type: new GraphQLList(AccountType),
            resolve: (parent, args) => {
                return Account.find({});
            }
        },
        transactions: {
            type: new GraphQLList(TransactionType),
            resolve(parent, args) {
                return Transaction.find({});
            }
        }
    })
});

const MutationType = new GraphQLObjectType({
    name: 'Mutation',
    fields: () => ({
        addUser: {
            type: UserType,
            args: {
                name: {
                    type: new GraphQLNonNull(GraphQLString)
                },
                age: {
                    type: new GraphQLNonNull(GraphQLInt)
                },
                email: {
                    type: new GraphQLNonNull(GraphQLString)
                }
            },
            resolve(parent, args) {
                let user = new User({
                    name: args.name,
                    age: args.age,
                    email: args.email
                });
                pubsub.publish(USER_CREATED, {
                    newUser: user
                });
                return user.save();
            }
        },
        addAccount: {
            type: AccountType,
            args: {
                currency: {
                    type: new GraphQLNonNull(GraphQLString)
                },
                balance: {
                    type: new GraphQLNonNull(GraphQLFloat)
                },
                holderId: {
                    type: new GraphQLNonNull(GraphQLString)
                }
            },
            resolve(parent, args) {
                let account = new Account({
                    currency: args.currency,
                    balance: args.balance,
                    holderId: args.holderId
                });
                return account.save().then(() => console.log('user created'));
            }
        },
        addTransaction: {
            type: TransactionType,
            args: {
                sourceAccountId: {
                    type: new GraphQLNonNull(GraphQLString)
                },
                targetAccountId: {
                    type: new GraphQLNonNull(GraphQLString)
                },
                amount: {
                    type: new GraphQLNonNull(GraphQLFloat)
                }
            },
            resolve(parent, args) {
                let transaction = new Transaction({
                    sourceAccountId: args.sourceAccountId,
                    tagetAccountId: args.tagetAccountId,
                    timestamp: new Date(),
                    amount: args.amount
                });

                Account.findById(args.sourceAccountId, (err, account) => {
                    if (!err) {
                        account.balance -= args.amount;
                        return account.save();
                    }
                });

                Account.findById(args.targetAccountId, (err, account) => {
                    if (!err) {
                        account.balance += args.amount;
                        return account.save();
                    }
                });

                return transaction.save();
            }
        }
    })
});

const SubscriptionType = new GraphQLObjectType({
    name: 'Subscription',
    fields: () => ({
        newUser: {
            type: UserType,
            description: 'This subscription is going to provide information every time a new user creation event fires',
            resolve: (payload, args, context, info) => {
                console.table(payload, args, context, info); // debugging
                return payload;
            },
            subscribe: () => pubsub.asyncIterator(USER_CREATED)
        }
    })
});

const schema = new GraphQLSchema({
    query: RootQueryType,
    mutation: MutationType,
    subscription: SubscriptionType
});

export default schema;

我希望当我运行订阅查询时,它将运行监听正在发布的事件,当我从另一个选项卡运行突变以添加新用户时,第一个选项卡将捕获事件并在有效负载中返回用户的详细信息.

标签: graphqlgraphql-jsgraphql-subscriptions

解决方案


推荐阅读