Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why applyMiddleware? #1814

Closed
lucasconstantino opened this issue Oct 13, 2018 · 1 comment
Closed

Why applyMiddleware? #1814

lucasconstantino opened this issue Oct 13, 2018 · 1 comment

Comments

@lucasconstantino
Copy link

lucasconstantino commented Oct 13, 2018

As it is of my understanding, ApolloServer 2.0 way of integrating an existing server to it is using the applyMiddleware method. I'm wondering why not having something to extract the sub-application (such as a express's Router) instead of doing this highly side-effect based approach of connecting directly to an existing app.

The approach of an exposed sub-application would highly reduce the amount of configuration and assumptions now taken by the current implementation of the apollo-server-express version of the applyMiddleware. For instance:

  • path: can be completely dropped;
  • cors: should be handled by the upper application, not ApolloServer;
  • bodyParserConfig: same as above;
  • disableHealthCheck: health check logic should be exposed separately, not polluting the upper app;
  • onHealthCheck: same as above.

My idea is it should be possible for me to do something like this:

./graphql.js

const { ApolloServer, gql } = require('apollo-server-express')

const typeDefs = gql`...`
const resolvers = { ... }

const apolloServer = new ApolloServer({ typeDefs, resolvers })

module.exports = apolloServer.middleware() // or `.app()` or `.router()`

./server.js

const express = require('express')
const graphql = require('./graphql')

express()
  .use('/graphql', graphql)
  .listen(3000, () => console.log(`> Ready on http://localhost:3000`))

This way I can invert the order of knowledge, making the graphql logic completely unaware of the application from above.

@razor-x
Copy link

razor-x commented Oct 19, 2018

I've noticed there are a lot of similar requests coming in for the missing middleware in v2. I'm wondering if we should consolidate them in one issue and close the others.

This one seems like the best candidate: #1308

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants