Here’s a repo. This is still Meteor 2.16. There are a few questions that might be helpful to resolve prior to updating this to Meteor 3:
The existing Meteor Apollo skeleton seeks to add Meteor user to Apollo Context, but the Context object does not appear to be accessible from the resolvers. See: imports/apollo/resolvers.js:
const resolvers = {
Query: {
getLink: async function findLinkById(parent, args, contextValue) {
// context not available here?
return await LinksCollection.findOne(args.id);
},
getLinks: async function (parent, args, contextValue) {
// context not available here?
return await LinksCollection.find().fetch()
}
}
};
Context should be the third param passed to a resolver. But it has no defined value in the resolvers above.
Putting a call to Meteor.user() inside a resolver does not work in Meteor 2.x as I had thought. I’ve been using it there for years, so possibly this is something swydo:ddp-apollo enabled. Totally okay though, because we are trying to pass that data to the resolvers via context.
Which brings us to setup for context. Please see server/apollo.js:
Meteor.methods({
'getUserId'() {
let userId = null
try {
userId = Meteor.userId()
} catch (error) {
console.log('getUserId: ', error)
}
return userId
}
});
const context = async function ({req}) {
let userTest = await getUser(req.headers.authorization)
let userTest2 = Meteor.call(
"getUserId"
);
let userTest3 = await Meteor.users.findOneAsync();
return {
user: await getUser(req.headers.authorization)
};
};
Note: this only runs when a graphQL query runs.
As you can see, I’m trying several tests here to obtain Meteor.user data. At the moment, they are returning as follows:
- userTest: undefined
- userTest2: null
- userTest3: undefined
For all I know, all 3 are correct, since there is no logged-in user. The skeleton app has no login function.
Is there a way to determine if one or more of these are working?
UPDATE: This same Apollo setup does communicate context to my resolvers in my Mv3 app. So probably it requires the latest node and/or apollo packages. Creating an Mv3 version of this repo now…
UPDATE 2: The Mv3 repo does communicate context to the resolver!
Once we figure out how to get Meteor.user() into context, we will be there!
The code that needs to be iterated is at server/apollo.js
.
@ denyhs, I don’t know Meteor v3 well enough to think of ways to look into this. Your advice will be greatly appreciated.
Note: had the wrong link to the Mv3 repo before. Fixed now.