Merge remote-tracking branch 'graphql/master' into feature/django

This commit is contained in:
Adam Charnock 2015-12-29 13:30:05 +00:00
commit 58421cda14
6 changed files with 61 additions and 0 deletions

View File

@ -82,3 +82,21 @@ graphene.Field(graphene.String(), to=graphene.String())
# Is equivalent to: # Is equivalent to:
graphene.Field(graphene.String(), to=graphene.Argument(graphene.String())) graphene.Field(graphene.String(), to=graphene.Argument(graphene.String()))
``` ```
## Using custom object types as argument
To use a custom object type as an argument, you need to inherit `graphene.InputObjectType`, not `graphene.ObjectType`.
```python
class CustomArgumentObjectType(graphene.InputObjectType):
field1 = graphene.String()
field2 = graphene.String()
```
Then, when defining this in an argument, you need to wrap it in an `Argument` object.
```python
graphene.Field(graphene.String(), to=graphene.Argument(CustomArgumentObjectType))
```

View File

@ -20,6 +20,7 @@ def convert_django_field(field):
@convert_django_field.register(models.EmailField) @convert_django_field.register(models.EmailField)
@convert_django_field.register(models.SlugField) @convert_django_field.register(models.SlugField)
@convert_django_field.register(models.URLField) @convert_django_field.register(models.URLField)
@convert_django_field.register(models.GenericIPAddressField)
@convert_django_field.register(UUIDField) @convert_django_field.register(UUIDField)
def convert_field_to_string(field): def convert_field_to_string(field):
return String(description=field.help_text) return String(description=field.help_text)

View File

@ -0,0 +1,38 @@
from django.core.management.base import BaseCommand, CommandError
import importlib
import json
class Command(BaseCommand):
help = 'Dump Graphene schema JSON to file'
can_import_settings = True
def add_arguments(self, parser):
from django.conf import settings
parser.add_argument(
'--schema',
type=str,
dest='schema',
default=getattr(settings, 'GRAPHENE_SCHEMA', ''),
help='Django app containing schema to dump, e.g. myproject.core.schema')
parser.add_argument(
'--out',
type=str,
dest='out',
default=getattr(settings, 'GRAPHENE_SCHEMA_OUTPUT', 'schema.json'),
help='Output file (default: schema.json)')
def handle(self, *args, **options):
schema_module = options['schema']
if schema_module == '':
raise CommandError('Specify schema on GRAPHENE_SCHEMA setting or by using --schema')
i = importlib.import_module(schema_module)
schema_dict = {'data': i.schema.introspect()}
with open(options['out'], 'w') as outfile:
json.dump(schema_dict, outfile)
self.stdout.write(self.style.SUCCESS('Successfully dumped GraphQL schema to %s' % options['out']))

View File

@ -48,6 +48,10 @@ def test_should_url_convert_string():
assert_conversion(models.URLField, graphene.String) assert_conversion(models.URLField, graphene.String)
def test_should_ipaddress_convert_string():
assert_conversion(models.GenericIPAddressField, graphene.String)
def test_should_auto_convert_id(): def test_should_auto_convert_id():
assert_conversion(models.AutoField, graphene.ID, primary_key=True) assert_conversion(models.AutoField, graphene.ID, primary_key=True)