Skip to content

Commit bbce7d5

Browse files
committed
Test all code snippets in the documentation
1 parent da6d040 commit bbce7d5

File tree

10 files changed

+370
-16
lines changed

10 files changed

+370
-16
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ The current version 3.0.0a2 of GraphQL-core is up-to-date
1616
with GraphQL.js version 14.4.2.
1717

1818
All parts of the API are covered by an extensive test suite
19-
of currently 1905 unit tests.
19+
of currently 1915 unit tests.
2020

2121

2222
## Documentation

docs/usage/extension.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ We also need to attach a resolver function to the new field::
2929

3030
Now we can query only the last name of a human::
3131

32+
from graphql import graphql_sync
33+
3234
result = graphql_sync(schema, """
3335
{
3436
human(id: "1000") {

docs/usage/introspection.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ create a query that omits the descriptions with::
1717
In practice you would run this query against a remote server, but we can also run it
1818
against the schema we have just built above::
1919

20+
from graphql import graphql_sync
21+
2022
introspection_query_result = graphql_sync(schema, query)
2123

2224
The ``data`` attribute of the introspection query result now gives us a dictionary,

docs/usage/methods.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ executing a query.
99
In our case, we could create a ``Root`` class with three methods as root resolvers, like
1010
so::
1111

12-
class Root():
12+
class Root:
1313
"""The root resolvers"""
1414

1515
def hero(self, info, episode):
@@ -22,7 +22,7 @@ so::
2222
return droid_data.get(id)
2323

2424

25-
Since we haven't defined synchronous methods only, we will use the
25+
Since we have defined synchronous methods only, we will use the
2626
:func:`graphql.graphql_sync` function to execute a query, passing a ``Root()`` object as
2727
the ``root_value``::
2828

docs/usage/parser.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ the AST document::
3434

3535
This will give the same result as manually creating the AST document::
3636

37+
from graphql.language.ast import *
38+
3739
document = DocumentNode(definitions=[
3840
ObjectTypeDefinitionNode(
3941
name=NameNode(value='Query'),

docs/usage/queries.rst

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Here is one way to use it::
2828
""")
2929
print(result)
3030

31-
asyncio.get_event_loop().run_until_complete(main())
31+
asyncio.get_event_loop().run_until_complete(query_artoo())
3232

3333
In our query, we asked for the droid with the id 2001, which is R2-D2, and its primary
3434
function, Astromech. When everything has been implemented correctly as shown above, you
@@ -79,9 +79,8 @@ Let's see what happens when we make a mistake in the query, by querying a non-ex
7979
You will get the following result as output::
8080

8181
ExecutionResult(data=None, errors=[GraphQLError(
82-
"Cannot query field 'homeTown' on type 'Human'."
83-
" Did you mean 'homePlanet'?",
84-
locations=[SourceLocation(line=5, column=7)])])
82+
"Cannot query field 'homeTown' on type 'Human'. Did you mean 'homePlanet'?",
83+
locations=[SourceLocation(line=5, column=9)])])
8584

8685
This is very helpful. Not only do we get the exact location of the mistake in the query,
8786
but also a suggestion for correcting the bad field name.

docs/usage/resolvers.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ them later)::
4141
'2000': threepio, '2001': artoo}
4242

4343

44-
def get_character_type(character, info):
44+
def get_character_type(character, _info, _type):
4545
return 'Droid' if character['id'] in droid_data else 'Human'
4646

4747

@@ -50,29 +50,29 @@ them later)::
5050
return human_data.get(id) or droid_data.get(id)
5151

5252

53-
def get_friends(character, info):
53+
def get_friends(character, _info):
5454
"""Allows us to query for a character's friends."""
5555
return map(get_character, character.friends)
5656

5757

58-
def get_hero(root, info, episode):
58+
def get_hero(root, _info, episode):
5959
"""Allows us to fetch the undisputed hero of the trilogy, R2-D2."""
6060
if episode == 5:
6161
return luke # Luke is the hero of Episode V
6262
return artoo # Artoo is the hero otherwise
6363

6464

65-
def get_human(root, info, id):
65+
def get_human(root, _info, id):
6666
"""Allows us to query for the human with the given id."""
6767
return human_data.get(id)
6868

6969

70-
def get_droid(root, info, id):
70+
def get_droid(root, _info, id):
7171
"""Allows us to query for the droid with the given id."""
7272
return droid_data.get(id)
7373

7474

75-
def get_secret_backstory(character, info):
75+
def get_secret_backstory(_character, _info):
7676
"""Raise an error when attempting to get the secret backstory."""
7777
raise RuntimeError('secretBackstory is secret.')
7878

docs/usage/sdl.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ So we would also need to manually define these values, like so::
6565
This would allow us to query the schema built from SDL just like the manually assembled
6666
schema::
6767

68+
from graphql import graphql_sync
69+
6870
result = graphql_sync(schema, """
6971
{
7072
hero(episode: EMPIRE) {

docs/usage/validator.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ As a result, you will get a complete list of all errors that the validators has
2424
In this case, we will get::
2525

2626
[GraphQLError(
27-
"Expected type String!, found NEWHOPE.",
27+
'Expected type String!, found NEWHOPE.',
2828
locations=[SourceLocation(line=3, column=17)]),
2929
GraphQLError(
3030
"Cannot query field 'homeTown' on type 'Human'."
3131
" Did you mean 'homePlanet'?",
3232
locations=[SourceLocation(line=5, column=9)]),
3333
GraphQLError(
34-
"Field 'friends' of type '[Character]' must have a"
35-
" sub selection of subfields. Did you mean 'friends { ... }'?",
34+
"Field 'friends' of type '[Character]' must have a sub selection of subfields."
35+
" Did you mean 'friends { ... }'?",
3636
locations=[SourceLocation(line=6, column=9)])]
3737

3838
These rules are available in the :data:`specified_rules` list and implemented in the

0 commit comments

Comments
 (0)