-
Notifications
You must be signed in to change notification settings - Fork 183
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
Bind Python Types to GraphQL Input Types [How To] #333
Comments
Hello! Thank you for asking, but issue tracker is used exclusively for tracking bugs and roadmap. We are using Spectrum for general Q&A and usage discussion. A very similar question is already being discussed here: https://spectrum.chat/ariadne/general/how-to-access-resolver-arguments-as-custom-pydantic-any-other-objects-except-pure-dictionaries~8209983f-6e2d-4215-9976-d13d1e1acad2 |
@rafalp - the link you provided returns a 404. Do you know whether past discussions have been archived somewhere? |
Yeah, Spectrum disappeared few months after GH bought them out. What author asks about is not possible to do in "pure" GraphQL, and this is what vanilla Ariadne does. But it's something I'll keep on radar for Ariadne GraphQL Modules. In the meantime you would have to cast them manually in resolver or write custom decorator. Maybe we could have this decorator as an utility in Ariadne? |
I'll reopen this because its something we could do in Ariadne proper, but not a high priority. |
Thanks for the quick response @rafalp. I can see the benefit of such a utility being available in Ariadne, although to your point it's not unreasonable to write it in user-land. In general, I think any ergonomic improvements to the relationship between the GraphQL schema and python type annotations is really powerful. While I haven't used it yet, the new While manually adding a decorator to a resolver is simple and safe, I'd like to briefly advocate for a "bolder" approach (more magic, but also more convenient): I think if Ariadne did some analysis of the resolver type signature at schema creation time (e.g. using inspect.get_annotations) and added this casting utility decorator automatically for any resolvers whose annotations are As an aside: Given the really serious performance implications of using the built-in |
Yes. Basically there's no way in GraphQL to tell query executor to represent value of argument or input field as an object other than scalar. And scalars only exist in GraphQL schemas as GraphQL lets us say what names should inputs and arguments values be passed to in Python, but that's it. So this conversion has to be done in the resolver, so best we could do would be to wrap user's resolver in custom resolver that does this conversion before passing the value to resolver proper. So we would have to implement resolver decorator anyway, we would just need to have a "do_magic_args_types_conversion" option on |
I was wrong! Apparently Inputs in graphql-core now have an |
Ok, so Something like this doesn't work:
But with small change it does:
This opens interesting possibilities like using different Python type depending on input's contents themselves. But I need to decide if Ariadne should funnel people to custom factory functions/lambda, or do it for them (which I am currently 90% against). |
That's great for customizing the For example, I have a custom class |
You can easily write utility function that sets custom |
Perhaps it's just unclear in the docs, but I don't see any way to automatically convert from the input dictionaries given by Ariadne, to my resolvers, into custom Python types (i.e. dataclasses)
Is this supported? I found the docs on bindables, but from just that I can't tell if that's what I want.
Example
The text was updated successfully, but these errors were encountered: