Snippet: Run GraphQL and Express in the Same Backend

Snippet: Run GraphQL and Express in the Same Backend

If you ever need the benefits of node express and graphql, this will allow you to run both at once in the same code base.
 
This can be useful if you’d like to protect API calls (Like authentication) with something like cloudflare but also want to serve content easily using GraphQL
export const startServer = async () = { const gqlServer = new ApolloServer({typedefs, resolvers}) const expressServer = createExpressServer(); gqlServer.start(); gqlServer.applyMiddleware({ expressServer, path: '/' }) expressServer.listen({ 4000 }); console.log(`Server ready at: http://localhost:4000${gqlServer.graphqlPath}`); }; startServer();

Guides and practical notes, training references, and code snippets shared freely for learning and career growth.