Skip to content

Feature gql cli print schema #258

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

Merged
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
9 changes: 9 additions & 0 deletions docs/gql-cli/intro.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.. _gql_cli:

gql-cli
=======

Expand Down Expand Up @@ -69,3 +71,10 @@ Then execute query from the file:

$ cat query.gql | gql-cli wss://countries.trevorblades.com/graphql
{"continent": {"name": "Africa"}}

Print the GraphQL schema in a file
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. code-block:: shell

$ gql-cli https://countries.trevorblades.com/graphql --print-schema > schema.graphql
5 changes: 5 additions & 0 deletions docs/usage/validation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ The schema can be provided as a String (which is usually stored in a .graphql fi

client = Client(schema=schema_str)

.. note::
You can download a schema from a server by using :ref:`gql-cli <gql_cli>`

:code:`$ gql-cli https://SERVER_URL/graphql --print-schema > schema.graphql`

OR can be created using python classes:

.. code-block:: python
Expand Down
21 changes: 19 additions & 2 deletions gql/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from argparse import ArgumentParser, Namespace, RawDescriptionHelpFormatter
from typing import Any, Dict

from graphql import GraphQLError
from graphql import GraphQLError, print_schema
from yarl import URL

from gql import Client, __version__, gql
Expand Down Expand Up @@ -38,6 +38,9 @@
# Execute query saved in a file
cat query.gql | gql-cli wss://countries.trevorblades.com/graphql

# Print the schema of the backend
gql-cli https://countries.trevorblades.com/graphql --print-schema

"""


Expand Down Expand Up @@ -92,6 +95,12 @@ def get_parser(with_examples: bool = False) -> ArgumentParser:
help="set the operation_name value",
dest="operation_name",
)
parser.add_argument(
"--print-schema",
help="get the schema from instrospection and print it",
action="store_true",
dest="print_schema",
)

return parser

Expand Down Expand Up @@ -241,7 +250,15 @@ async def main(args: Namespace) -> int:
exit_code = 0

# Connect to the backend and provide a session
async with Client(transport=transport) as session:
async with Client(
transport=transport, fetch_schema_from_transport=args.print_schema
) as session:

if args.print_schema:
schema_str = print_schema(session.client.schema)
print(schema_str)

return exit_code

while True:

Expand Down
35 changes: 31 additions & 4 deletions tests/custom_scalars/test_money.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,19 +410,15 @@ async def handler(request):
data = await request.json()
source = data["query"]

print(f"data keys = {data.keys()}")
try:
variables = data["variables"]
print(f"variables = {variables!r}")
except KeyError:
variables = None

result = graphql_sync(
schema, source, variable_values=variables, root_value=root_value
)

print(f"backend result = {result!r}")

return web.json_response(
{
"data": result.data,
Expand Down Expand Up @@ -742,3 +738,34 @@ def test_serialize_value_with_nullable_type():
nullable_int = GraphQLInt

assert serialize_value(nullable_int, None) is None


@pytest.mark.asyncio
async def test_gql_cli_print_schema(event_loop, aiohttp_server, capsys):

from gql.cli import get_parser, main

server = await make_money_backend(aiohttp_server)

url = str(server.make_url("/"))

parser = get_parser(with_examples=True)
args = parser.parse_args([url, "--print-schema"])

exit_code = await main(args)

assert exit_code == 0

# Check that the result has been printed on stdout
captured = capsys.readouterr()
captured_out = str(captured.out).strip()

print(captured_out)
assert (
"""
type Subscription {
spend(money: Money): Money
}
""".strip()
in captured_out
)