Comments (9)
Hi @Cito , we might need to reopen this!
Unfortunately, in cases such as using an inherited __typename
property, basic demangling is not enough.
Let's imagine I have the following (Relay inspired) Node and subclass:
class Node:
@property
def __typename(self):
return self.__class__.__name__
class A(Node):
....
Obviously _A__typename
will not be found, and we will ignore _Node__typename
.
Something like this (instead of just using a simple getattr()
) would help instead:
def get_typename(value):
for cls in value.__class__.__mro__:
__typename = getattr(value, f"_{cls.__name__}__typename", None)
if __typename:
return __typename
return None
What do you think?
from graphql-core.
This is implemented now in the master branch and will be available in the next beta release.
from graphql-core.
HI @ezyang, thanks for reporting this issue.
is_type_of
is actually a method of the GraphQL type which checks whether the given object is of that type. What you're probably looking for is the __typename
attribute which you can set on objects to specify their GraphQL type as a string and which is checked by default_resolve_type_fn
. This is the way it's done also in GraphQL.js which graphql-core-next copies.
However, I see that that graphql-core-next currently checks __typename
only on objects that are returned as dicts, not on (data)class instances like in your case.
It would certainly be a good idea and very simple to extend graphql-core-next to check for __typename
attributes on non-dict objects as well - you would simply set __typename = 'Repository'
on your Repository class. Unfortunately, there is an issue here in that Python applies name mangling to attributes starting with two underscores (a feature intended to be used for fake "private" attributes that is frowned upon but unfortunately still exists in Python 3).
So probably we should use the special name __typename__
instead (these names do not underly name mangling). It's not nice, since these special names are actually reserved for Python, and we deviate a bit from the GraphQL API, but it's the only obvious solution that comes to my mind just now. What do you think?
from graphql-core.
A special name like __typename__
seems fine to me, as long as it's prominently documented.
Note that it's not impossible to demangle it, if you really wanted to. But I think it's probably better here to just hew to Pythonic conventions.
from graphql-core.
I also considered demangling and immediately dismissed the idea. But now after sleeping over it, I am thinking maybe it is not so bad an idea after all. The rule for the mangling - prefixing with the class name- is well documened and not implementation specific, and the class name can be easily inspected. That would allow us to be completely compatible to GraphQL.js and consistent between dicts and class based objects.
Another option would be check both attributes (__typename
and __typename__
), but that would go against the Zen of Python.
from graphql-core.
So I decided to go with the demangling. This will be available in the next version (released soon).
Please reopen if you have any better ideas.
from graphql-core.
Version 1.0,2 with this fix has now been released on PyPI.
from graphql-core.
Good point @Hellzed. Will add your A(Node)
example and maybe and a more complicated one with multiple inheritance to the test suite. Your suggested code will probably solve this.
from graphql-core.
Version 3.1.0b1 with this fix has been released now.
from graphql-core.
Related Issues (20)
- Trying to inject `id` field on every `selection_set` HOT 1
- Can default values form cycles? HOT 2
- Include name in representation of named nodes HOT 1
- performance issues due to breadth first execution of grapqhl queries in case of async resolvers during calls burst HOT 4
- A
- subscribe breaking change in graphql version 3.3.0a3 HOT 1
- [Question] How to map operation variables to their uses? HOT 2
- Using Relays in Graphql-core raises a TypeError HOT 4
- `out_name` is not respected for input objects used as default argument value
- Segfault during schema parsing HOT 3
- Segfault during schema parsing
- `graphql.utilities.print_schema` module contents are hidden behind `utilities` package
- `deepcopy` fails if the schema has a directive with an enum argument
- Both `typing.ByteString` and `typing.Text` are deprecated and slated for removal in Python 3.14 HOT 3
- Add the ability to specify `GraphQLResolveInfo.context` type HOT 1
- I use middleware for that right now HOT 1
- construction fails for depth around > 200 / resource exhaustion? HOT 9
- Cancel resolver tasks if execution of an operation is terminated HOT 7
- Support for official @oneof directive HOT 1
- How do you run tests with an IDE? HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from graphql-core.