apollographql / meteor-integration

🚀 meteor add apollo
http://dev.apollodata.com/core/meteor.html
108 stars 45 forks source link

Meteor integration changes #134

Closed aadamsx closed 5 years ago

aadamsx commented 5 years ago

The Integrating with Meteor docs state to either use the swydo package for add meteor apollo like so:

meteor add apollo

My question is, it seems this is no longer valid right? That apollo package was actually this package at one point? And now this meteor-integration package has turned into a "template" for pulling in the accounting information right?

By the way, the MAIN reason to build Apollo Server with Meteor is to get to the Accounts package to allow it to integrate with an existing Meteor app. This is my code so far:

Meteor 1.8

As you can see, the example I saw orignially imported 'meteor/apollo' which I think was your package as it was before. Instead now I think I should remove that, and just use the getUser from this lib's template, as I've shown in the auth.js file below this code.

\\index.js
import express from 'express';
import { ApolloServer, gql } from 'apollo-server-express';
import { WebApp } from 'meteor/webapp';
import { getUser } from 'meteor/apollo'; // DON'T USE THIS
import cors from 'cors';
import schema from './schema';
// import getUser from './auth.js'; // USE THIS INSTEAD

const server = new ApolloServer({
  schema: schema,
  // typeDefs,
  // resolvers,
  context: async ({ req }) => ({
    user: await getUser(req.headers.authorization)
  })
})

const app = express();
app.use(cors());
server.applyMiddleware({ app })

app.listen(4000, () => {
  console.log('server is running on port 4000...');
});

This is your template code:

\\auth.js
import { Meteor } from 'meteor/meteor'
import { Accounts } from 'meteor/accounts-base'
import { check } from 'meteor/check'

export const getUser = async loginToken => {
  if (loginToken) {
    check(loginToken, String)

    const hashedToken = Accounts._hashLoginToken(loginToken)

    const user = await Meteor.users.rawCollection().findOne({
      'services.resume.loginTokens.hashedToken': hashedToken
    })

    if (user) {
      // find the right login token corresponding, the current user may have
      // several sessions logged on different browsers / computers
      const tokenInformation = user.services.resume.loginTokens.find(
        tokenInfo => tokenInfo.hashedToken === hashedToken
      )

      const expiresAt = Accounts._tokenExpiration(tokenInformation.when)

      const isExpired = expiresAt < new Date()

      if (!isExpired) {
        return user
      }
    }
  }
}