Skip to content
This repository has been archived by the owner on Jul 27, 2020. It is now read-only.

Support middlewares out-of-the-box #88

Open
Weakky opened this issue Mar 1, 2019 · 4 comments
Open

Support middlewares out-of-the-box #88

Weakky opened this issue Mar 1, 2019 · 4 comments

Comments

@Weakky
Copy link
Contributor

Weakky commented Mar 1, 2019

Description

There are two types of middleware that should be easily definable with Yoga:

  • "HTTP" middleware: most probably express middlewares
  • GraphQL middleware: a function to be run before (or after) a resolver is executed
    • middlewares should be definable at every level (globally, type-level, field-level)

Current state:

  • HTTP middleware: Only possible by ejecting
  • GraphQL middleware: Only possible by ejecting (eg: using graphql-middleware)

Ideally, both HTTP middlewares and GraphQL middlewares should be definable without having to eject.

Unfortunately, because Yoga doesn't require a server to be defined by default, users have no way to access the underlying components.

  • Fortunately, for GraphQL middlewares, this can be done on the schema layer and thus should be handled by nexus (Proposal here: Support middleware graphql-nexus/nexus#64)
  • For HTTP middlewares however, we need to find an elegant way to expose the underlying express instance for people to compose with it

Motivation

Obvious use-cases:

Some ideas

Accessing the underlying express instance

Option 1

Have an express folder, in which all files gets the underlying express instance injected

export default ({ app }) => {
  // app.use(passport.initialize())
  // app.post('/login', ...)
}

Option 2

Do we really need several files in a folder? What about a single express.ts file ?

import { logging, otherMiddleware } from './express-middleware'

export default ({ app }) => {
  // app.use(logging)  
  // app.post('/login', otherMiddleware, handler)
  // app.post('/logout', otherMiddleware, handler)
}

Option 3

Any better idea?


Because HTTP middlewares and GraphQL middlewares are two different concepts, it feels hard to come up with an API that elegantly conciles the two.

We'd love to hear feedback/different API design for express middlewares as well as nexus middlewares 🙌

Thanks!

@Weakky
Copy link
Contributor Author

Weakky commented Mar 14, 2019

Option 2 was released under [email protected] for accessing express middlewares

@DevanB
Copy link
Contributor

DevanB commented Mar 16, 2019

Thanks for this @Weakky! I'm very curious to see graphql middlewares support without ejecting soon. Until then, I'll simply eject (since it's a reversible process 😄 )

@ivan-kleshnin
Copy link

ivan-kleshnin commented Mar 22, 2019

Would be great if someone clarified how Prisma 2, Yoga 2 and Prisma Nexus are or will be related 😓
I'm kinda lost here. There are so many moving parts changing as I'm typing it.

@DevanB
Copy link
Contributor

DevanB commented Mar 24, 2019

@ivan-kleshnin I think this question is better asked in the Prisma forum for all to see there, not as a comment on a middlewares discussion issue.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants