Katharina Ußling
Katharina Ußling

Reputation: 21

GraphQL Resolver for Relations

I would like to write a mutation, which when creating a tracker, enters the tracker model ID and the user ID for the respective tracker model and the user. My question relates to the return statement in the Resolve function: how can I execute both functions directly after each other, so that it´s being written in both collections?

Here is my mutation. As it is currently written, only the second variable is executed, in this case only "tm".

createTracker: {
      type: TrackerType,
      args: {
        trackerModelID: {type: GraphQLID },
        userId: {type: GraphQLID },
        access_token: {type: GraphQLString },
      },
      resolve(parent, args){
        let tracker = new Tracker({
          trackerModelID: args.trackerModelID,
          userId: args.userId,
          access_token: args.access_token,
        });
        tracker.save();
        let tm = TrackerModel.updateOne({ _id: args.trackerModelID }, { $push: { trackerIds: tracker._id } });
        let us = User.updateOne({ _id: args.userId }, { $push: { trackerIds: tracker._id } });
        return us && tm
      },
    },

Upvotes: 0

Views: 99

Answers (2)

Katharina Ußling
Katharina Ußling

Reputation: 21

Thank you, I tried all these ways, but it did´nt work.

But now, I found the answer by myself, this works:

tracker.save();
return User.updateOne({ _id: args.userId }, { $push: { trackerIds: tracker._id } }).then(() =>{
 return TrackerModel.updateOne({ _id: args.trackerModelID }, { $push: { trackerIds: tracker._id } });
 }
)

Upvotes: 1

Zain Ul Abideen
Zain Ul Abideen

Reputation: 1602

give this a try;

1) remove return us && tm

2) declare array: var results = [us , tm];

3) return array: return results

Upvotes: 0

Related Questions