Skip to content

Commit d31624b

Browse files
committed
build_client_schema: add dev check for invalid introspection argument
Replicates graphql/graphql-js@91d673f
1 parent 01cdace commit d31624b

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ a query language for APIs created by Facebook.
1313
[![Code Style](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/ambv/black)
1414

1515
The current version 1.0.5 of GraphQL-core-next is up-to-date with GraphQL.js version
16-
14.3.1. All parts of the API are covered by an extensive test suite of currently 1822
16+
14.3.1. All parts of the API are covered by an extensive test suite of currently 1823
1717
unit tests.
1818

1919

graphql/utilities/build_client_schema.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ def build_client_schema(
5151
This function expects a complete introspection result. Don't forget to check the
5252
"errors" field of a server response before calling this function.
5353
"""
54+
if not isinstance(introspection, dict) or not isinstance(
55+
introspection.get("__schema"), dict
56+
):
57+
raise TypeError(
58+
"Invalid or incomplete introspection result. Ensure that you"
59+
" are passing the 'data' attribute of an introspection response"
60+
" and no 'errors' were returned alongside: " + inspect(introspection)
61+
)
62+
5463
# Get the schema from the introspection result.
5564
schema_introspection = introspection["__schema"]
5665

tests/utilities/test_build_client_schema.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,27 @@ def describe_throws_when_given_invalid_introspection():
487487
"""
488488
)
489489

490+
def throws_when_introspection_is_missing_schema_property():
491+
with raises(TypeError) as exc_info:
492+
# noinspection PyTypeChecker
493+
build_client_schema(None) # type: ignore
494+
495+
assert str(exc_info.value) == (
496+
"Invalid or incomplete introspection result. Ensure that you"
497+
" are passing the 'data' attribute of an introspection response"
498+
" and no 'errors' were returned alongside: None"
499+
)
500+
501+
with raises(TypeError) as exc_info:
502+
# noinspection PyTypeChecker
503+
build_client_schema({}) # type: ignore
504+
505+
assert str(exc_info.value) == (
506+
"Invalid or incomplete introspection result. Ensure that you"
507+
" are passing the 'data' attribute of an introspection response"
508+
" and no 'errors' were returned alongside: {}"
509+
)
510+
490511
def throws_when_referenced_unknown_type():
491512
introspection = introspection_from_schema(dummy_schema)
492513

0 commit comments

Comments
 (0)