I’m trying to upgrade my meteor/apollo app to use 2.0 on the server, but can’t seem to get it working. Any ideas what I’m missing?
import { ApolloServer, gql } from 'apollo-server';
import { Meteor } from 'meteor/meteor';
import { Accounts } from 'meteor/accounts-base';
import { typeDefs, resolvers } from '/imports/api/schema';
import loaders from '/imports/api/services/DataLoaders';
export const getUserForContext = async loginToken => {
// there is a possible current user connected!
if (loginToken) {
// throw an error if the token is not a string
check(loginToken, String);
// the hashed token is the key to find the possible current user in the db
const hashedToken = Accounts._hashLoginToken(loginToken);
// get the possible current user from the database
// note: no need of a fiber aware findOne + a fiber aware call break tests
// runned with practicalmeteor:mocha if eslint is enabled
const currentUser = await Meteor.users.rawCollection().findOne({
'services.resume.loginTokens.hashedToken': hashedToken,
});
// the current user exists
if (currentUser) {
// find the right login token corresponding, the current user may have
// several sessions logged on different browsers / computers
const tokenInformation = currentUser.services.resume.loginTokens.find(
tokenInfo => tokenInfo.hashedToken === hashedToken
);
// get an exploitable token expiration date
const expiresAt = Accounts._tokenExpiration(tokenInformation.when);
// true if the token is expired
const isExpired = expiresAt < new Date();
// if the token is still valid, give access to the current user
// information in the resolvers context
if (!isExpired) {
// return a new context object with the current user & her id
return currentUser;
}
}
}
return {};
};
const server = new ApolloServer({
resolvers,
typeDefs,
//typeDefs: gql`${typeDefs}`,
mocks: true,
debug: Meteor.isDevelopment,
context: ({ req }) => ({
loaders,
user: async () => {
const loginToken = req.headers['meteor-login-token'];
return getUserForContext(loginToken);
}
}),
onHealthCheck: () => fetch('https://fourtonfish.com/hellosalut/?mode=auto'),
});
server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});
I go to http://localhost:4000/ and all I see is graphql playground. I think I’m missing some magic from createApolloClient in ‘meteor/apollo’.
update:
Of course, I can hit http://localhost:3000/ and get the meteor app, but I can’t seem to connect to the graphql endpoint unfortunately.
Update:
Maybe WebApp is required: https://github.com/meteor/meteor/issues/10039