Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add watch option to graphql_schema #656

Merged
merged 2 commits into from
Jun 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/introspection.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ Advanced Usage
The ``--indent`` option can be used to specify the number of indentation spaces to
be used in the output. Defaults to `None` which displays all data on a single line.

The ``--watch`` option can be used to run ``./manage.py graphql_schema`` in watch mode, where it will automatically output a new schema every time there are file changes in your project

To simplify the command to ``./manage.py graphql_schema``, you can
specify the parameters in your settings.py:

Expand Down
4 changes: 1 addition & 3 deletions graphene_django/debug/tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ def resolve_reporter(self, info, **args):
"""
expected = {
"reporter": {"lastName": "ABA"},
"_debug": {
"sql": [{"rawSql": str(Reporter.objects.order_by("pk")[:1].query)}]
},
"_debug": {"sql": [{"rawSql": str(Reporter.objects.order_by("pk")[:1].query)}]},
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just because I ran make format by the way

}
schema = graphene.Schema(query=Query)
result = schema.execute(
Expand Down
37 changes: 28 additions & 9 deletions graphene_django/management/commands/graphql_schema.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import importlib
import json
import functools

from django.core.management.base import BaseCommand, CommandError
from django.utils import autoreload

from graphene_django.settings import graphene_settings

Expand Down Expand Up @@ -32,6 +34,14 @@ def add_arguments(self, parser):
help="Output file indent (default: None)",
)

parser.add_argument(
"--watch",
dest="watch",
default=False,
action="store_true",
help="Updates the schema on file changes (default: False)",
)


class Command(CommandArguments):
help = "Dump Graphene schema JSON to file"
Expand All @@ -41,6 +51,18 @@ def save_file(self, out, schema_dict, indent):
with open(out, "w") as outfile:
json.dump(schema_dict, outfile, indent=indent, sort_keys=True)

def get_schema(self, schema, out, indent):
schema_dict = {"data": schema.introspect()}
if out == "-":
self.stdout.write(json.dumps(schema_dict, indent=indent, sort_keys=True))
else:
self.save_file(out, schema_dict, indent)

style = getattr(self, "style", None)
success = getattr(style, "SUCCESS", lambda x: x)

self.stdout.write(success("Successfully dumped GraphQL schema to %s" % out))

def handle(self, *args, **options):
options_schema = options.get("schema")

Expand All @@ -63,13 +85,10 @@ def handle(self, *args, **options):
)

indent = options.get("indent")
schema_dict = {"data": schema.introspect()}
if out == "-":
self.stdout.write(json.dumps(schema_dict, indent=indent, sort_keys=True))
watch = options.get("watch")
if watch:
autoreload.run_with_reloader(
functools.partial(self.get_schema, schema, out, indent)
)
else:
self.save_file(out, schema_dict, indent)

style = getattr(self, "style", None)
success = getattr(style, "SUCCESS", lambda x: x)

self.stdout.write(success("Successfully dumped GraphQL schema to %s" % out))
self.get_schema(schema, out, indent)