Skip to content

Commit

Permalink
Updated docs adding resolver page with @with_context instructions. Fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
syrusakbary committed May 19, 2016
1 parent c1c4af8 commit 981a7f6
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 33 deletions.
53 changes: 27 additions & 26 deletions docs/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,36 @@ siteTitle = "Graphene"
ga = "UA-12613282-7"

[docs.quickstart]
name = "Quickstart"
pages = [
"/docs/quickstart/",
]
name = "Quickstart"
pages = [
"/docs/quickstart/",
]

[docs.walkthrough]
name = "Walkthrough"
pages = [
"/docs/interfaces/",
"/docs/objecttypes/",
"/docs/mutations/",
"/docs/basic-types/",
"/docs/enums/",
"/docs/relay/",
]
name = "Walkthrough"
pages = [
"/docs/interfaces/",
"/docs/objecttypes/",
"/docs/resolvers/",
"/docs/mutations/",
"/docs/basic-types/",
"/docs/enums/",
"/docs/relay/",
]

[docs.django]
name = "Django"
pages = [
"/docs/django/tutorial/",
"/docs/django/filtering/",
"/docs/django/authorization/",
"/docs/django/introspection-schema/",
"/docs/django/debug/",
]
name = "Django"
pages = [
"/docs/django/tutorial/",
"/docs/django/filtering/",
"/docs/django/authorization/",
"/docs/django/introspection-schema/",
"/docs/django/debug/",
]

[docs.sqlalchemy]
name = "SQLAlchemy"
pages = [
"/docs/sqlalchemy/tutorial/",
"/docs/sqlalchemy/tips/",
]
name = "SQLAlchemy"
pages = [
"/docs/sqlalchemy/tutorial/",
"/docs/sqlalchemy/tips/",
]
17 changes: 10 additions & 7 deletions docs/pages/docs/django/authorization.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class Query(ObjectType):
## User-based Queryset Filtering

If you are using `graphql-django-view` you can access Django's request object
via `info.request_context`.
via `with_context` decorator.

```python
from graphene import ObjectType
Expand All @@ -71,18 +71,20 @@ class Query(ObjectType):
class Meta:
abstract = True

def resolve_my_posts(self, args, info):
if not info.request_context.user.is_authenticated():
@with_context
def resolve_my_posts(self, args, context, info):
# context will reference to the Django request
if not context.user.is_authenticated():
return []
else:
return Post.objects.filter(owner=info.request_context.user)
return Post.objects.filter(owner=context.user)
```

If you're using your own view, passing the request context into the schema is
simple.

```python
result = schema.execute(query, request_context=request)
result = schema.execute(query, context_value=request)
```

## Filtering ID-based node access
Expand All @@ -100,13 +102,14 @@ class PostNode(DjangoNode):
only_fields = ('title', 'content')

@classmethod
def get_node(Cls, id, info):
@with_context
def get_node(Cls, id, context, info):
try:
post = Cls._meta.model.objects.get(id=id)
except Cls._meta.model.DoesNotExist:
return None

if post.published or info.request_context.user is post.owner:
if post.published or context.user is post.owner:
return Cls(instance)
else:
return None
Expand Down
62 changes: 62 additions & 0 deletions docs/pages/docs/resolvers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
---
title: Resolvers
description: Walkthrough Resolvers
---

# Resolvers

A resolver is a method that resolves certain field within a `ObjectType`.
The resolver of a field will be, if not specified otherwise, the `resolve_{field_name}` within the `ObjectType`.

By default a resolver will take the `args`, and `info` arguments.
*This is likely to be simplified in the future*.


## Quick example

This example model defines a `Query` type, which has a reverse field that reverses the given `word`
argument using the `resolve_reverse` method in the class.

```python
import graphene

class Query(graphene.ObjectType):
reverse = graphene.String(word=graphene.String())

def resolve_reverse(self, args, info):
word = args.get('word')
return word[::-1]
```

## Resolvers outside the class

A field could also specify a custom resolver outside the class:

```python
import graphene

def reverse(root, args, info):
word = args.get('word')
return word[::-1]

class Query(graphene.ObjectType):
reverse = graphene.String(word=graphene.String(), resolver=reverse)
```


## Context

A query in a GraphQL schema could have some context that we can use in any resolver.
In this case we need to decorate the resolver function with `with_context`.

```python
class Query(graphene.ObjectType):
name = graphene.String()

@with_context
def resolve_name(self, args, context, info):
return context['name']


result = schema.execute(query, context_value={'name': 'Peter'})
```

0 comments on commit 981a7f6

Please sign in to comment.