mirror of
				https://github.com/graphql-python/graphene.git
				synced 2025-11-04 18:07:48 +03:00 
			
		
		
		
	Updated docs
This commit is contained in:
		
							parent
							
								
									394a1beb07
								
							
						
					
					
						commit
						0002d42e38
					
				| 
						 | 
					@ -48,7 +48,7 @@ Here is one example for you to get started:
 | 
				
			||||||
class Query(graphene.ObjectType):
 | 
					class Query(graphene.ObjectType):
 | 
				
			||||||
    hello = graphene.String(description='A typical hello world')
 | 
					    hello = graphene.String(description='A typical hello world')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def resolve_hello(self):
 | 
					    def resolve_hello(self, info):
 | 
				
			||||||
        return 'World'
 | 
					        return 'World'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
schema = graphene.Schema(query=Query)
 | 
					schema = graphene.Schema(query=Query)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -65,7 +65,7 @@ Here is one example for you to get started:
 | 
				
			||||||
    class Query(graphene.ObjectType):
 | 
					    class Query(graphene.ObjectType):
 | 
				
			||||||
        hello = graphene.String(description='A typical hello world')
 | 
					        hello = graphene.String(description='A typical hello world')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        def resolve_hello(self):
 | 
					        def resolve_hello(self, info):
 | 
				
			||||||
            return 'World'
 | 
					            return 'World'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    schema = graphene.Schema(query=Query)
 | 
					    schema = graphene.Schema(query=Query)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -45,26 +45,17 @@ With 2.0:
 | 
				
			||||||
```python
 | 
					```python
 | 
				
			||||||
my_field = graphene.String(my_arg=graphene.String())
 | 
					my_field = graphene.String(my_arg=graphene.String())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def resolve_my_field(self, my_arg):
 | 
					def resolve_my_field(self, info, my_arg):
 | 
				
			||||||
    return ...
 | 
					    return ...
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
And, if the resolver want to receive the context:
 | 
					And, if the resolver want to get the context:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
```python
 | 
					```python
 | 
				
			||||||
my_field = graphene.String(my_arg=graphene.String())
 | 
					my_field = graphene.String(my_arg=graphene.String())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def resolve_my_field(self, context: graphene.Context, my_arg):
 | 
					def resolve_my_field(self, info, my_arg):
 | 
				
			||||||
    return ...
 | 
					    context = info.context
 | 
				
			||||||
```
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
which is equivalent in Python 2 to:
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
```python
 | 
					 | 
				
			||||||
my_field = graphene.String(my_arg=graphene.String())
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
@annotate(context=graphene.Context)
 | 
					 | 
				
			||||||
def resolve_my_field(self, context, my_arg):
 | 
					 | 
				
			||||||
    return ...
 | 
					    return ...
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -95,7 +86,7 @@ class Pet(CommonFields, Interface):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
### resolve\_only\_args
 | 
					### resolve\_only\_args
 | 
				
			||||||
 | 
					
 | 
				
			||||||
`resolve_only_args` is now deprecated in favor of type annotations (using the polyfill `@graphene.annotate` in Python 2 in case is necessary for accessing `context` or `info`).
 | 
					`resolve_only_args` is now deprecated as the resolver API has been simplified.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Before:
 | 
					Before:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -114,7 +105,7 @@ With 2.0:
 | 
				
			||||||
class User(ObjectType):
 | 
					class User(ObjectType):
 | 
				
			||||||
    name = String()
 | 
					    name = String()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def resolve_name(self):
 | 
					    def resolve_name(self, info):
 | 
				
			||||||
        return self.name
 | 
					        return self.name
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -227,10 +218,9 @@ class UserInput(InputObjectType):
 | 
				
			||||||
class Query(ObjectType):
 | 
					class Query(ObjectType):
 | 
				
			||||||
    user = graphene.Field(User, input=UserInput())
 | 
					    user = graphene.Field(User, input=UserInput())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def resolve_user(self, input):
 | 
					    def resolve_user(self, info, id):
 | 
				
			||||||
        if input.is_valid:
 | 
					        if input.is_valid:
 | 
				
			||||||
            return get_user(input.id)
 | 
					            return get_user(input.id)
 | 
				
			||||||
 | 
					 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -266,7 +256,7 @@ class Base(ObjectType):
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    id = ID()
 | 
					    id = ID()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def resolve_id(self):
 | 
					    def resolve_id(self, info):
 | 
				
			||||||
        return "{type}_{id}".format(
 | 
					        return "{type}_{id}".format(
 | 
				
			||||||
            type=self.__class__.__name__,
 | 
					            type=self.__class__.__name__,
 | 
				
			||||||
            id=self.id
 | 
					            id=self.id
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -99,8 +99,8 @@ leaner code and at most 4 database requests, and possibly fewer if there are cac
 | 
				
			||||||
        best_friend = graphene.Field(lambda: User)
 | 
					        best_friend = graphene.Field(lambda: User)
 | 
				
			||||||
        friends = graphene.List(lambda: User)
 | 
					        friends = graphene.List(lambda: User)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        def resolve_best_friend(self):
 | 
					        def resolve_best_friend(self, info):
 | 
				
			||||||
            return user_loader.load(self.best_friend_id)
 | 
					            return user_loader.load(self.best_friend_id)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        def resolve_friends(self):
 | 
					        def resolve_friends(self, info):
 | 
				
			||||||
            return user_loader.load_many(self.friend_ids)
 | 
					            return user_loader.load_many(self.friend_ids)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -24,9 +24,8 @@ You can pass context to a query via ``context_value``.
 | 
				
			||||||
    class Query(graphene.ObjectType):
 | 
					    class Query(graphene.ObjectType):
 | 
				
			||||||
        name = graphene.String()
 | 
					        name = graphene.String()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        @graphene.annotate(context=graphene.Context)
 | 
					        def resolve_name(self, info):
 | 
				
			||||||
        def resolve_name(self, context):
 | 
					            return info.context.get('name')
 | 
				
			||||||
            return context.get('name')
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    schema = graphene.Schema(Query)
 | 
					    schema = graphene.Schema(Query)
 | 
				
			||||||
    result = schema.execute('{ name }', context_value={'name': 'Syrus'})
 | 
					    result = schema.execute('{ name }', context_value={'name': 'Syrus'})
 | 
				
			||||||
| 
						 | 
					@ -44,8 +43,8 @@ You can pass variables to a query via ``variable_values``.
 | 
				
			||||||
    class Query(graphene.ObjectType):
 | 
					    class Query(graphene.ObjectType):
 | 
				
			||||||
        user = graphene.Field(User)
 | 
					        user = graphene.Field(User)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        def resolve_user(self, args, context, info):
 | 
					        def resolve_user(self, info):
 | 
				
			||||||
            return context.get('user')
 | 
					            return info.context.get('user')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    schema = graphene.Schema(Query)
 | 
					    schema = graphene.Schema(Query)
 | 
				
			||||||
    result = schema.execute(
 | 
					    result = schema.execute(
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -31,10 +31,10 @@ This middleware only continues evaluation if the ``field_name`` is not ``'user'`
 | 
				
			||||||
.. code:: python
 | 
					.. code:: python
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    class AuthorizationMiddleware(object):
 | 
					    class AuthorizationMiddleware(object):
 | 
				
			||||||
        def resolve(self, next, root, args, context, info):
 | 
					        def resolve(self, next, root, info, **args):
 | 
				
			||||||
            if info.field_name == 'user':
 | 
					            if info.field_name == 'user':
 | 
				
			||||||
                return None
 | 
					                return None
 | 
				
			||||||
            return next(root, args, context, info)
 | 
					            return next(root, info, **args)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
And then execute it with:
 | 
					And then execute it with:
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -39,7 +39,7 @@ one field: ``hello`` and an input name. And when we query it, it should return `
 | 
				
			||||||
    class Query(graphene.ObjectType):
 | 
					    class Query(graphene.ObjectType):
 | 
				
			||||||
        hello = graphene.String(name=graphene.String(default_value="stranger"))
 | 
					        hello = graphene.String(name=graphene.String(default_value="stranger"))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        def resolve_hello(self, name):
 | 
					        def resolve_hello(self, info, name):
 | 
				
			||||||
            return 'Hello ' + name
 | 
					            return 'Hello ' + name
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    schema = graphene.Schema(query=Query)
 | 
					    schema = graphene.Schema(query=Query)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -41,5 +41,5 @@ that implements ``Node`` will have a default Connection.
 | 
				
			||||||
        name = graphene.String()
 | 
					        name = graphene.String()
 | 
				
			||||||
        ships = relay.ConnectionField(ShipConnection)
 | 
					        ships = relay.ConnectionField(ShipConnection)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        def resolve_ships(self):
 | 
					        def resolve_ships(self, info):
 | 
				
			||||||
            return []
 | 
					            return []
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -25,7 +25,7 @@ This example model defines a Person, with a first and a last name:
 | 
				
			||||||
        last_name = graphene.String()
 | 
					        last_name = graphene.String()
 | 
				
			||||||
        full_name = graphene.String()
 | 
					        full_name = graphene.String()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        def resolve_full_name(self):
 | 
					        def resolve_full_name(self, info):
 | 
				
			||||||
            return '{} {}'.format(self.first_name, self.last_name)
 | 
					            return '{} {}'.format(self.first_name, self.last_name)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
**first\_name** and **last\_name** are fields of the ObjectType. Each
 | 
					**first\_name** and **last\_name** are fields of the ObjectType. Each
 | 
				
			||||||
| 
						 | 
					@ -71,7 +71,7 @@ method in the class.
 | 
				
			||||||
    class Query(graphene.ObjectType):
 | 
					    class Query(graphene.ObjectType):
 | 
				
			||||||
        reverse = graphene.String(word=graphene.String())
 | 
					        reverse = graphene.String(word=graphene.String())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        def resolve_reverse(self, word):
 | 
					        def resolve_reverse(self, info, word):
 | 
				
			||||||
            return word[::-1]
 | 
					            return word[::-1]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Resolvers outside the class
 | 
					Resolvers outside the class
 | 
				
			||||||
| 
						 | 
					@ -83,7 +83,7 @@ A field can use a custom resolver from outside the class:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    import graphene
 | 
					    import graphene
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def reverse(root, word):
 | 
					    def reverse(root, info, word):
 | 
				
			||||||
        return word[::-1]
 | 
					        return word[::-1]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    class Query(graphene.ObjectType):
 | 
					    class Query(graphene.ObjectType):
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user