2

I am trying to implement a simple API with GraphQL. My queries and my mutations are in place and working, but now I'm trying to include subscriptions as well.

I already added the subscription in the schema, I included the event publish in the addUser mutation and defined the subscribe function for the subscription type.

Now, when I am trying to run a subscription query in the graphiql in-browser IDE, I get this error:

"The \"properties\" argument must be of type Array. Received type object"

Attached is the schema object. Did I configured something wrong or am I missing something? Thanks!

P.S I also need to mention that I am using mongoose to store the data on an a mongo instance, hence the entities.

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;

I expect that when I run the subscription query, it will run listening for events being published and when from another tab I will run a mutation to add a new user, the first tab will catch the event and return details of the user in the payload.

ggluta
  • 143
  • 1
  • 11
  • 1
    I encountered a similar problem, but later solved it by looking at my resolvers... try reverting back to your previous changes before you found the problem and solve it from there – Niscolinx Feb 02 '21 at 21:23

0 Answers0