0

I have a resolver set up that queries mock db file (a json).

Query: {
        users: (_, filters, { dataSources }) => {
            console.log(dataSources.userAPI.getAllUsers(filters))
            return dataSources.userAPI.getAllUsers(filters);
        }
}

userAPI.getALlUsers is below. First I want to filter the list of users. Then for each filtered user, I want to add more fields to it before return the mapped users

function getAllUsers(args) {
    const { targetId, skip, limit } = args;

    const filteredUsers = new Promise((resolve, reject) => {
        const users = db.users.filter(u => {
            if (u.apps[targetId]) return u;
        });
        if (users.length === 0) return reject();
        else return resolve(users);
    });

    filteredUsers.then((users, err) => {
        if (err) console.log(err);
        else {
            const mappedUsers = users.map(u => {
                const withReturnFields = {
                    email: u.email,
                    fullname: u.fullname
                };
                return withReturnFields;
            });
            return mappedUsers.slice(skip, skip + limit);
        }
    });
}

I think my promise isn't working as expected. Any insights? My schemas are all set up correctly. I tried console logging

Kaisin Li
  • 514
  • 3
  • 7
  • 21
  • Closing this as a dupe. As Nazar showed, you need to return the Promise inside the resolver. If you're unable to resolve the other error you're seeing, feel free to open a new question with the full error message and your relevant Apollo Server config. – Daniel Rearden Nov 08 '19 at 03:04

1 Answers1

1

Your getAllUsers function returns undefined. When resolver return undefined it means that the object could not be found, you should return a promise instead.

return filteredUsers.then((users, err) => {
Nazar Litvin
  • 758
  • 6
  • 10
  • I added return and I got this error : ```{"message": "Abstract type User must resolve to an Object type at runtime for field Query.users with value { email: \"john.doe@test.com\", fullname: \"Full Name1\" }, received \"undefined\". Either the User type should provide a \"resolveType\" function or each possible type should provide an \"isTypeOf\" function."}``` why is it still receiving undefined? Below is my schemas: ``` users( targetId: ID skip: Int # for pagination limit: Int # for pagination ): [User!]! interface User { email: String! fullname: String! } – Kaisin Li Nov 08 '19 at 02:56
  • 1
    That is unrelated to your original issue. You've defined the User type as either a union or interface. That means you must also provide either a `resolveType` function for User, or a `isTypeOf` function for each type User could be. You're either missing `resolveType` entirely or you did not implement it correctly. – Daniel Rearden Nov 08 '19 at 03:00