impress-2020/src/server/index.js

88 lines
2.1 KiB
JavaScript
Raw Normal View History

const { beelinePlugin } = require("./lib/beeline-graphql");
2020-09-02 23:00:16 -07:00
const { gql, makeExecutableSchema } = require("apollo-server");
2020-04-22 11:51:36 -07:00
const { getUserIdFromToken } = require("./auth");
2020-04-22 11:51:36 -07:00
const connectToDb = require("./db");
const buildLoaders = require("./loaders");
2020-04-22 11:51:36 -07:00
const rootTypeDefs = gql`
directive @cacheControl(maxAge: Int!) on FIELD_DEFINITION | OBJECT
type Mutation
type Query
2020-04-22 11:51:36 -07:00
`;
function mergeTypeDefsAndResolvers(modules) {
const allTypeDefs = [];
const allResolvers = {};
for (const { typeDefs, resolvers } of modules) {
allTypeDefs.push(typeDefs);
for (const typeName of Object.keys(resolvers)) {
allResolvers[typeName] = {
...allResolvers[typeName],
...resolvers[typeName],
};
}
}
2020-05-23 11:32:05 -07:00
return { typeDefs: allTypeDefs, resolvers: allResolvers };
}
2020-05-23 11:32:05 -07:00
const schema = makeExecutableSchema(
mergeTypeDefsAndResolvers([
{ typeDefs: rootTypeDefs, resolvers: {} },
require("./types/AppearanceLayer"),
2020-11-24 14:24:34 -08:00
require("./types/ClosetList"),
require("./types/Item"),
require("./types/MutationsForSupport"),
require("./types/Outfit"),
require("./types/Pet"),
require("./types/PetAppearance"),
require("./types/User"),
require("./types/Zone"),
])
);
const plugins = [];
2020-08-17 01:27:05 -07:00
if (process.env["NODE_ENV"] !== "test") {
plugins.push(beelinePlugin);
}
const config = {
schema,
2020-09-02 23:00:16 -07:00
context: async ({ req }) => {
2020-04-22 11:51:36 -07:00
const db = await connectToDb();
2020-05-23 11:32:05 -07:00
2020-09-02 23:19:50 -07:00
const auth = (req && req.headers && req.headers.authorization) || "";
const authMatch = auth.match(/^Bearer (.+)$/);
const token = authMatch && authMatch[1];
2020-09-02 23:00:16 -07:00
const currentUserId = await getUserIdFromToken(token);
return {
db,
2020-09-02 23:00:16 -07:00
currentUserId,
...buildLoaders(db),
};
2020-04-22 11:51:36 -07:00
},
2020-04-23 01:09:17 -07:00
2020-08-17 01:27:05 -07:00
plugins,
2020-05-23 11:32:05 -07:00
2020-04-23 01:09:17 -07:00
// Enable Playground in production :)
introspection: true,
playground: {
endpoint: "/api/graphql",
},
};
2020-04-22 11:51:36 -07:00
if (require.main === module) {
const { ApolloServer } = require("apollo-server");
const server = new ApolloServer(config);
2020-04-22 11:51:36 -07:00
server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});
}
module.exports = { config };