GraphQL订阅错误:“ \“ properties \”参数必须为数组类型。接收的对象类型”

时间:2019-10-04 21:10:53

标签: graphql graphql-js graphql-subscriptions

我正在尝试使用GraphQL实现一个简单的API。我的查询和我的变异已经就位并且可以正常工作,但是现在我也试图包括订阅

我已经在模式中添加了预订,我在addUser变体中包含了发布事件,并为预订类型定义了预订功能。

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

  

“ \” properties \“参数必须为Array类型。收到的对象类型为

附加的是架构对象。我配置有误还是错过了某些东西?谢谢!

P.S我还需要提及的是,我正在使用 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;

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

0 个答案:

没有答案