-
Notifications
You must be signed in to change notification settings - Fork 41
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
v2 API RFC #4
Comments
Since regular functions work as resolvers, e.g.:
Is the only purpose of If so, I propose the following API: import { composable } from 'apollo-resolvers';
const deleteUser = (_, args, ctx) => { ... };
const editUser = (_, args, ctx) => { ... };
const followUser = (_, args, ctx) => { ... };
const unfollowUser = (_, args, ctx) => { ... };
const banUser = (_, args, ctx) => { ... };
const user = (_, args, ctx) => { ... };
const isLoggedIn = composable((_, args, { user }) => {
if (!user) throw new Error('Please log in.')
});
const isAdmin = composable((_, args, { user }) => {
if (!user.isAdmin) throw new Error('Unauthorized!');
});
export default {
Query: {
allPosts,
user: isLoggedIn.compose(user),
},
Mutation: {
followUser,
unfollowUser,
isAdmin.compose({
deleteUser,
editUser,
banUser,
})
},
}; As for baseResolvers, perhaps export default combineResolvers([
postResolvers,
userResolvers,
], { baseResolver }); This would result in far more manageable code IMO! 😃 |
I think this is a solid direction, but I also think there is value in handling errors as the "bubble up", so we should probably also support error resolvers in the same way that |
How so? How does it differ from |
Well, regular resolvers are unidirectional. The request pipes down from source to sink. Errors are handled after the |
These resolvers act like Middlewares, I think the idea of composable resolvers is really good. Keeping the "middleware" idea in mind, in Express you can use an error resolver at the end. But based on @thebigredgeek comment, the resolvers are resolved in the reverse order in error cases. Instead of extending a baseResolver you can just put it as a simple function at a position 0 of an array of these middlewares it will be simple, reusable and manageable. // userResolver.js
import { composable as applyMiddleware } from 'apollo-resolvers';
import { isAuthenticated, isAuthorized } from '.,/middlewares';
const updateUser= (_root, args, context) => { /* do your stuffs here */ };
const userResolver = {
Mutation: {
updateUser: applyMiddleware([ isAuthorized, isAuthenticated, userResolver ]),
}
}
export default userResolver; And the error resolver is only required in a single point of application. This makes sense for you guys? |
@thebergamo that's an interesting approach, but how would you implement an error resolver in this case? |
@scf4 Sorry, I've been busy at my new job. Just starting to get back into focusing on GraphQL. I had to set up a bunch of NodeJS infrastructure first haha. I'm all for a more elegant API. So long as you can pass |
For V2, I'd also really like to refactor into typescript to make the project more developer friendly as we extend the lib's functionality. |
^ this is me, by the way haha. I have a second account for work |
Hey @thebigredgeek I can help, just need to learn a little bit more about TypeScript. Well, the idea of the error handler, is to have a simple function like express middleware, but in the case of express you register the middleware for error at the end chain of middlewares.
And in this case, will be really good if this handler can be declared just once. |
@thebergamo @thebigredgeek how about Flow instead of TS? |
Also, how would you feel about expanding the scope of this project to become more of a library (or very small and fairly unopinionated framework) for building GraphQL server apps with Apollo? It's surprisingly difficult to find info on best practices and solving common problems with JS GraphQL APIs. You're really on your own, especially compared to what's available on the front end. There's a lot of reinventing the wheel. The JS GraphQL community could seriously benefit from something like this. |
@scf4 I don't know what the @thebigredgeek thinks about expanding this project to something more. IMHO is better keeping this module focused on this and create a new one to be the library to build GraphQL APIs. It's really really hard to find best pratices for somethings :p |
@thebigredgeek Nice package! Thanks! The only reason I'm not using it right away is that in V.1 the Why wouldn't you push V2 to npm (with, say, V1.1)? It won't break the backward compatibility, it's an additional feature. Meanwhile, V2 work will go on. Anyway, thanks a lot. |
I'm building a framework called |
@scf4 Not sure on Flow. I've used it before and I like it, but Typescript removes almost all of the developer tooling boilerplate (babel, etc). I am on the fence about using Flow rather than typescript. Would love more insight into your thoughts about this. |
One change we are making is adding the |
Typescript support is implemented! |
If anyone wants to implement a |
@thebigredgeek posted my first pass at compose in #31 |
@thebigredgeek Has #11 been implemented? The current version of [combineResolvers] has a different signature and I can't seem to figure out how to use it. It would be nice if it allowed to add resolver(s) to another or a group of existing resolvers (that already have a base)
|
@thebigredgeek I've been getting Circle CI notifications about v1 API depreciation. I'm planning to refactor the circleci.yml to conform to the v2 API sometime this week, time permitting. In any case we have till the end of the month before they drop support for v1. |
Just saw your PR! Nice job |
For those interested, I'm modernizing this package here: #67 |
List ideas here
The text was updated successfully, but these errors were encountered: