Comments (6)
Sounds reasonable. Actually it already works when you set the description manually. Here is an example:
class LazyText:
def __init__(self, text):
self.text = text
def __str__(self):
return self.text + ' (lazily evaluated)'
from graphql import *
queryType = GraphQLObjectType('Query', {
'someField': GraphQLField(GraphQLString)},
description='test description')
queryType.description = LazyText(queryType.description)
schema = GraphQLSchema(query=queryType)
query = get_introspection_query(descriptions=True)
result = graphql_sync(schema, query)
print(result.data['__schema']['types'][0]['description'])
We would need to remove the restriction for descriptions to be strings in the constructors of all the GraphQL types. Anything that can be converted to a string with str() (which is literally anything in Python) would then qualify as a description.
But removing the type hints and runtime type checks has also disadvantages; these can prevent real errors when you pass wrong arguments.
One solution would be to have the lazy texts masquerade as strings, then everything would just work:
class LazyText(str):
def __init__(self, text):
self.text = text
def __str__(self):
return self.text + ' (lazily evaluated)'
from graphql import *
queryType = GraphQLObjectType('Query', {
'someField': GraphQLField(GraphQLString)},
description=LazyText('test description'))
schema = GraphQLSchema(query=queryType)
query = get_introspection_query(descriptions=True)
result = graphql_sync(schema, query)
print(result.data['__schema']['types'][0]['description'])
Unfortunately, the Django lazy translations don't do that:
>>> from django.utils.translation import gettext_lazy
>>> isinstance(gettext_lazy('text'), str))
False
One solution would be to put a second wrapper around the Django translation:
from django.utils.translation import gettext
class GetTextLazy(str):
def __init__(self, msg):
self.msg = msg
def __str__(self):
return gettext(self.msg)
from graphql import *
queryType = GraphQLObjectType('Query', {
'someField': GraphQLField(GraphQLString)},
description=GetTextLazy('test description'))
schema = GraphQLSchema(query=queryType)
query = get_introspection_query(descriptions=True)
result = graphql_sync(schema, query)
print(result.data['__schema']['types'][0]['description'])
I''let this set for a while to see if we can come up with other/better ideas before starting to remove all the checks. Maybe it could be made configurable somehow. E.g. there could be a global DescriptionType = str which could be monkeypatched to another class or tuple of classes or to None to completely remove type checks for descriptions.
from graphql-core.
Another idea would be to use an ABC for DescriptionType, to let library users register their own types. But it seems mypy doesn't support ABCMeta.register(), which would be a problem when typing the uses of graphql-core.
from graphql-core.
Interesting idea. But as you already mentioned, it would only work for the runtime checks. And you would need to know the base class for the lazy strings which can be somewhat obscure (it's called "Promise" in Django, for instance) and might not even be publicly accessible. And you can't completely switch off the type checks that way - DecriptionType.register(object)
does not work.
from graphql-core.
I think a global variable would be simpler and more straightforward, though a bit "unclean". E.g. we can have a module pyutils.types
with a global variable Description = str
, and types.Description
would be used both as type alias for the type hint description: Optional[types.Description]
and for the runtime check isinstance(description, types.Description)
. You could then set types.Description = (str, LazyString)
while building the schema, or even pyutils.Description = object
to switch the runtime check off.
from graphql-core.
Btw, the Django docs mention this problem here, it also exists with other libraries. The recommended solution to cast to str()
would not work in this case though. It really should happen only later in the resolvers.
from graphql-core.
I have now implemented a different solution that allows registering additional classes that can be used as descriptions. Please reopen if you see any problems with this solution or have a better idea.
from graphql-core.
Related Issues (20)
- Can you inject fields to an operation with a Visitor? HOT 2
- Use type guards for `language.predicates` HOT 1
- 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
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.