-
Notifications
You must be signed in to change notification settings - Fork 276
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
Support middleware #64
Comments
love the concept! being able to add a middleware at every level open so much scenarios. I feel like |
Interesting. I like the idea, going to think more about how it could work, what it would mean for types, how it would work in terms of execution, etc. |
TypeGraphQL already has |
This is actually what I was looking for. Do you guys need help? @Weakky Do you have any |
Is there anything happening with this? |
Nope, not yet. PR's welcome. Haven't had time to start much work on this aside from starting to think about how it could work. |
Any progress or plan for this? I am considering using nexus or yoga2 in my new project, but the lack of middleware support turns out to be a major (or even only) obstacle. Currently, we need to use And once I go with yoga2, I cannot even figure out how to make my yoga2 project work together with the existing |
@beeplin You can configure middleware, it is just not build into nexus, which makes it a bit more complex. But it is still fairly easy. Where is the problem for you using graphql-middleware and graphql-shield? |
@P4sca1 this might be related more to import { GraphQLServer } from 'graphql-yoga'
const server = new GraphQLServer({
typeDefs,
resolvers,
middlewares: [
shield(shields, {
fallbackRule: deny,
graphiql: true,
debug,
}),
middlewares,
forward(...forwardings)('prismaBinding'),
],
context,
resolverValidationOptions,
debug,
}) But in the new |
Do you have Access to the apollo Server? If so you could overwrite apolloServer.schema after it was created. |
@P4sca1 Thanks! Following your direction I just managed to eject import { applyMiddleware } from 'graphql-middleware'
// ...
const schema = makePrismaSchema({
// ...
})
applyMiddleware(schema, {
Query: {
// middlewares
},
})
const apolloServer = new ApolloServer.ApolloServer({
schema,
context,
})
// ... |
I also like the idea of having middleware on a per-type / per-field basis. It would be great to also have a rule field, which directly integrates with |
With prisma 2 It would be nice to have this: t.crud.createOneUser({ alias: 'signupUser', middleware: xxx })
t.crud.deleteOnePost({ middleware: yyy}) |
I have been able to use |
@Hebilicious I did something similar on my project which uses nexus prisma, prisma client, graphql yoga, yup etc etc. Because nexus generates the schema I wasn't willing/able to add my validation onto the mutation object directly like they library suggests here (https://github.com/JCMais/graphql-yup-middleware#setting-the-validation-schema-of-each-mutation). Instead I wrote my own middleware (just a few lines long) which just intercepts the mutation and looks at the field name (name of the mutation, createUser, createPost, etc) and matches the mutation name with a yup validator (i just have them sitting in an object). Then we take that validator and validate it versus the incoming arguments. |
@jasonkuhrt any plan on this? |
Middlewares should be live soon as part of a plugin system implemented in prisma-labs/graphql-framework-experiment#242. It'll probably be available in beta only first. |
So this is more or less implemented in the latest 0.12.0-rc.4, which will turn into 0.12.0 shortly after a few final patches. Early docs below: https://nexus.js.org/docs/api-plugins https://nexus.js.org/docs/plugin-nullabilityguard https://nexus.js.org/docs/plugin-fieldauthorize Beginning to open new issues labeled |
@tgriesser - Is there an issue to track the validate` plugin already somewhere? Coudln't find it here yet. Looking forward to it! |
Description
As nexus recently implemented some kind of authorization system (which is a kind of middleware), I think we should also allow for more generic middlewares.
Proposal
Middlewares should be definable at every level of the API:
Globally
On an object type (not sure whether it makes sense to have it elsewhere)
On a field
Concerns
The current proposal might makes it hard to build abstractions upon it.
A use-case might be to provide an ACL style authorization system, in which you probably want to handle everything in one file.
With the current proposal, it doesn't seem trivial (or impossible) to inject middlewares with enough granularity (eg: on a field), because fields are evaluated later during the schema construction.
graphql-middleware
use the following data-structure to define middlewares at every level of the API:It might be necessary to have something alike on the global
middlewares
option (and eventually on types)Open questions
authorize
as a separate use-case ?authorize
a simple use-case ofmiddlewares
and provide docs?Thanks 🙌
The text was updated successfully, but these errors were encountered: