INNER CODE UNIT · JavaScript
dataSources
apollographql/fullstack-tutorial · final/server/src/index.js:18
const dataSources = () => ({
launchAPI: new LaunchAPI(),
userAPI: new UserAPI({ store }),
});
// the function that sets up the global context for each resolver, using the req
const context = async ({ req }) => {
// simple auth check on every request
const auth = (req.headers && req.headers.authorization) || '';
const email = Buffer.from(auth, 'base64').toString('ascii');
// if the email isn't formatted validly, return null for user
if (!isEmail.validate(email)) return { user: null };
// find a user by their email
const users = await store.users.findOrCreate({ where: { email } });
const user = users && users[0] ? users[0] : null;
return { user };