Skip to content

Commit 58421cd

Browse files
author
Adam Charnock
committed
Merge remote-tracking branch 'graphql/master' into feature/django
2 parents 6562a78 + 062c0f9 commit 58421cd

File tree

6 files changed

+61
-0
lines changed

6 files changed

+61
-0
lines changed

docs/pages/docs/basic-types.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,21 @@ graphene.Field(graphene.String(), to=graphene.String())
8282
# Is equivalent to:
8383
graphene.Field(graphene.String(), to=graphene.Argument(graphene.String()))
8484
```
85+
86+
87+
## Using custom object types as argument
88+
89+
To use a custom object type as an argument, you need to inherit `graphene.InputObjectType`, not `graphene.ObjectType`.
90+
91+
```python
92+
class CustomArgumentObjectType(graphene.InputObjectType):
93+
field1 = graphene.String()
94+
field2 = graphene.String()
95+
96+
```
97+
98+
Then, when defining this in an argument, you need to wrap it in an `Argument` object.
99+
100+
```python
101+
graphene.Field(graphene.String(), to=graphene.Argument(CustomArgumentObjectType))
102+
```

graphene/contrib/django/converter.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def convert_django_field(field):
2020
@convert_django_field.register(models.EmailField)
2121
@convert_django_field.register(models.SlugField)
2222
@convert_django_field.register(models.URLField)
23+
@convert_django_field.register(models.GenericIPAddressField)
2324
@convert_django_field.register(UUIDField)
2425
def convert_field_to_string(field):
2526
return String(description=field.help_text)

graphene/contrib/django/management/__init__.py

Whitespace-only changes.

graphene/contrib/django/management/commands/__init__.py

Whitespace-only changes.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from django.core.management.base import BaseCommand, CommandError
2+
3+
import importlib
4+
import json
5+
6+
7+
class Command(BaseCommand):
8+
help = 'Dump Graphene schema JSON to file'
9+
can_import_settings = True
10+
11+
def add_arguments(self, parser):
12+
from django.conf import settings
13+
parser.add_argument(
14+
'--schema',
15+
type=str,
16+
dest='schema',
17+
default=getattr(settings, 'GRAPHENE_SCHEMA', ''),
18+
help='Django app containing schema to dump, e.g. myproject.core.schema')
19+
20+
parser.add_argument(
21+
'--out',
22+
type=str,
23+
dest='out',
24+
default=getattr(settings, 'GRAPHENE_SCHEMA_OUTPUT', 'schema.json'),
25+
help='Output file (default: schema.json)')
26+
27+
def handle(self, *args, **options):
28+
schema_module = options['schema']
29+
if schema_module == '':
30+
raise CommandError('Specify schema on GRAPHENE_SCHEMA setting or by using --schema')
31+
i = importlib.import_module(schema_module)
32+
33+
schema_dict = {'data': i.schema.introspect()}
34+
35+
with open(options['out'], 'w') as outfile:
36+
json.dump(schema_dict, outfile)
37+
38+
self.stdout.write(self.style.SUCCESS('Successfully dumped GraphQL schema to %s' % options['out']))

graphene/contrib/django/tests/test_converter.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ def test_should_url_convert_string():
4848
assert_conversion(models.URLField, graphene.String)
4949

5050

51+
def test_should_ipaddress_convert_string():
52+
assert_conversion(models.GenericIPAddressField, graphene.String)
53+
54+
5155
def test_should_auto_convert_id():
5256
assert_conversion(models.AutoField, graphene.ID, primary_key=True)
5357

0 commit comments

Comments
 (0)