Get user id in appollo resolver

how can i get user id in an apollo resolver
i have a resolver like this

state: Meteor.bindEnvironment((record) => {
            console.log(arguments);
            return UserEventStates.findOne({eventId: record._id, createdBy: Meteor.userId()});
        }),

but obviously i got this error
Exception in callback of async function: Error: Meteor.userId can only be invoked in method calls or publications.

You should try to use this.userId for most of the times. Try to avoid called Meteor.user() on the server. It’ll fetch the user object from Mongo, everytime you invoke it.

no that cannot work, becouse of the context. this.userId() doesnt exists in this context.
currently i reovled it by injecting user id into the record during the query operation where i have access to user context.
Not elegant solution but it works, i hope there’s a better way to do this.

I have setup Apollo 2 to get the user context using Apollo Meteor Integration Guide. And the user is available in the context.

If you have done the same, then maybe you can write the resolver in this way.

state (obj, {record}, {user} ){
if(user)
  {
     const userId = user._id
     // Your function call
  }
throw new Error("Not Authorized")
}

well, i didnt explain well the problem.
Here is the structure of the resolver of my self define type “Event”

Query: {        
    eventDetail(obj, {_id}, {user}) {
        // query logic
    }
},
// resolve record
Event: {  
    state: Meteor.bindEnvironment((record) => {
        return UserEventStates.findOne({eventId: record._id, createdBy: Meteor.userId()}) || {};
    })
},
Mutation: {
    // other mutation queries
}

I can get user info without problem in query/mutation operations, but I’m trying to resolve the record of type Event that has a property named “state”. In that context i dont have access to the user info.

Oh. That’s makes more sense.