-
Notifications
You must be signed in to change notification settings - Fork 2k
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
[Feature] Add resolvers to arguments #3789
Comments
I've also pondered the idea for validating arguments through an argument resolver, I'd suggest some amendments:
For point 3 eg. args: {
limit: {
type: new GraphQLNonNull(GraphQLInt),
resolve(args, context) {
if (args.limit > 1000) {
throw Error("Must be < 1000.")
}
return limit;
}
}
} There's probably a lot more to consider, the idea sort of seems an obvious one, and maybe there's a good reason we're missing as to why it hasn't been implemented. Related: #361 |
Hello, I've worked with GraphQL lately and i encountered such scenarios where such feature is required to keep clean organized code. As I've used graphql v15.8.0 and such feature is not implemented i created it myself, check repo . |
@AhmedAyachi thank you so much for commenting. It’s great that this has been solved in user land! I am going to close this issue for now, if there’s additional functionality that we need to integrate, we can revisit. |
Details:
Instead of receiving the classic
parent, args, ctx
parameters, argument resolvers would receivevalue
which formally corresponds to the value of the argument andctx
the context.Unlike normal resolvers, argument resolvers can't return a value. To communicate information with the main resolver, the context will be used to attach desired information.
Argument resolvers will be executed sequentially in the order of the argument's definition. This allows clear communication between all argument resolvers and with the main resolver.
This feature would allow the pre-processing of arguments before reaching the main resolver. It would make parts of the code more readable and reusable.
As demonstrated in the example below, an arg resolver could fetch an item in the db using an id passed as an argument and then attach the resolved item to the context so that it can be used by the main resolver later on. It could also handle errors, such as ItemNotFound if the id doesn't match an item in the db.
The sequential execution allows arg resolvers to be reused but to behave in different ways depending on the context. In the following example, we want to ensure that Entity has a unique name before adding it to the db. But we also want to ensure the uniqueness of the name while editing Entity's name. Therefore, we can reuse our arg resolver for both mutations. However, we don't want editEntity to throw an error if the name hasn't changed. This can be solved using the context: if ctx.entity exists, we can check to see if we are changing Entity's name to a new name. If ctx.entity doesn't exist, this means we are adding a new entity to the db and that we should ensure the Entity's name uniqueness.
Exemple
Implementation
While I believe that this feature can become very handy, it will be optional and won't change anything to the execution of the code when not used.
To make it work, a similar function to
executeFields
should be created to loop onfieldDef.args
and run the resolvers if defined.The text was updated successfully, but these errors were encountered: