Skip to content

Instantly share code, notes, and snippets.

@acomito
Created August 11, 2019 12:38
Show Gist options
  • Save acomito/55f3c1305ff60d792ddfaa81deb0e6b3 to your computer and use it in GitHub Desktop.
Save acomito/55f3c1305ff60d792ddfaa81deb0e6b3 to your computer and use it in GitHub Desktop.
// import mongoose from 'mongoose';
import AccountsPassword from '@accounts/password';
import MongoDBInterface from '@accounts/mongo';
import AccountsServer from '@accounts/server';
import { AccountsModule } from '@accounts/graphql-api';
import { ApolloServer } from 'apollo-server';
import { merge } from 'lodash';
import { typeDefs, CustomResolvers } from './src/graphql-api/index';
import { db } from './src/modules/mongodb.js';
import emailTransporter from './src/modules/email.js';
import UserProfileHelpers from 'collections/UserProfiles/helpers';
const password = new AccountsPassword();
const accountsServer = new AccountsServer(
{
db: new MongoDBInterface(db),
tokenSecret: 'SECRET'
},
{
password,
}
);
export const accountsGraphQL = AccountsModule.forRoot({ accountsServer });
const typeDefsWithAccounts = [typeDefs, accountsGraphQL.typeDefs];
const resolvers = merge(accountsGraphQL.resolvers, CustomResolvers);
// Give apollo server it's options object
const server = new ApolloServer({
resolvers,
typeDefs: typeDefsWithAccounts,
context: async req => {
let result = await accountsGraphQL.context(req);
if (!result.user) {
return {
user: null,
};
}
let userProfile = await UserProfileHelpers.getById(result.user.id);
return {
...result,
user: {
roles: userProfile && userProfile.roles ? userProfile.roles : [],
...result.user,
},
};
},
});
// This `listen` method launches a web-server.
server.listen({ port: process.env.PORT || 8080 }).then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment