Skip to content

Commit ff988a6

Browse files
committed
Initial work refactoring class Meta to class arguments.
Initial work refactoring class Meta to class arguments. Refactoring firehose imports to explicit imports. More blackening of example code. More refactoring of `class Meta` into class arguments.
1 parent 67c4310 commit ff988a6

26 files changed

+318
-316
lines changed

docs/execution/dataloader.rst

+4-4
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,10 @@ leaner code and at most 4 database requests, and possibly fewer if there are cac
106106

107107
.. code:: python
108108
109-
class User(graphene.ObjectType):
110-
name = graphene.String()
111-
best_friend = graphene.Field(lambda: User)
112-
friends = graphene.List(lambda: User)
109+
class User(ObjectType):
110+
name = String()
111+
best_friend = Field(lambda: User)
112+
friends = List(lambda: User)
113113
114114
def resolve_best_friend(self, info):
115115
return user_loader.load(self.best_friend_id)

docs/execution/execute.rst

+20-18
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ For executing a query a schema, you can directly call the ``execute`` method on
77

88
.. code:: python
99
10-
schema = graphene.Schema(...)
11-
result = schema.execute('{ name }')
10+
schema = Schema(...)
11+
result = schema.execute("{ name }")
1212
1313
``result`` represents the result of execution. ``result.data`` is the result of executing the query, ``result.errors`` is ``None`` if no errors occurred, and is a non-empty list if an error occurred.
1414

@@ -21,14 +21,15 @@ You can pass context to a query via ``context``.
2121

2222
.. code:: python
2323
24-
class Query(graphene.ObjectType):
25-
name = graphene.String()
24+
class Query(ObjectType):
25+
name = String()
2626
2727
def resolve_name(root, info):
28-
return info.context.get('name')
28+
return info.context.get("name")
2929
30-
schema = graphene.Schema(Query)
31-
result = schema.execute('{ name }', context={'name': 'Syrus'})
30+
31+
schema = Schema(Query)
32+
result = schema.execute("{ name }", context={"name": "Syrus"})
3233
3334
3435
@@ -40,22 +41,23 @@ You can pass variables to a query via ``variables``.
4041

4142
.. code:: python
4243
43-
class Query(graphene.ObjectType):
44-
user = graphene.Field(User, id=graphene.ID(required=True))
44+
class Query(ObjectType):
45+
user = Field(User, id=ID(required=True))
4546
4647
def resolve_user(root, info, id):
4748
return get_user_by_id(id)
4849
49-
schema = graphene.Schema(Query)
50+
51+
schema = Schema(Query)
5052
result = schema.execute(
51-
'''
52-
query getUser($id: ID) {
53+
"""
54+
query getUser($id: ID) {
5355
user(id: $id) {
54-
id
55-
firstName
56-
lastName
56+
id
57+
firstName
58+
lastName
59+
}
5760
}
58-
}
59-
''',
60-
variables={'id': 12},
61+
""",
62+
variables={"id": 12},
6163
)

docs/quickstart.rst

+11-9
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Let’s build a basic GraphQL schema from scratch.
1212
Requirements
1313
------------
1414

15-
- Python (2.7, 3.4, 3.5, 3.6, pypy)
15+
- Python (3.6+, pypy)
1616
- Graphene (2.0)
1717

1818
Project setup
@@ -35,15 +35,17 @@ one field: ``hello`` and an input name. And when we query it, it should return `
3535

3636
.. code:: python
3737
38-
import graphene
38+
from graphene import ObjectType, Schema, String
3939
40-
class Query(graphene.ObjectType):
41-
hello = graphene.String(argument=graphene.String(default_value="stranger"))
40+
41+
class Query(ObjectType):
42+
hello = String(argument=String(default_value="stranger"))
4243
4344
def resolve_hello(self, info, argument):
44-
return 'Hello ' + argument
45+
return f"Hello {argument}"
46+
4547
46-
schema = graphene.Schema(query=Query)
48+
schema = Schema(query=Query)
4749
4850
Querying
4951
--------
@@ -52,11 +54,11 @@ Then we can start querying our schema:
5254

5355
.. code:: python
5456
55-
result = schema.execute('{ hello }')
56-
print(result.data['hello']) # "Hello stranger"
57+
result = schema.execute("{ hello }")
58+
print(result.data["hello"]) # "Hello stranger"
5759
5860
# or passing the argument in the query
5961
result = schema.execute('{ hello (argument: "graph") }')
60-
print(result.data['hello']) # "Hello graph"
62+
print(result.data["hello"]) # "Hello graph"
6163
6264
Congrats! You got your first graphene schema working!

docs/relay/connection.rst

+3-6
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,9 @@ and ``other`` an extra field in the Connection Edge.
1616

1717
.. code:: python
1818
19-
class ShipConnection(Connection):
19+
class ShipConnection(Connection, node=Ship):
2020
extra = String()
2121
22-
class Meta:
23-
node = Ship
24-
2522
class Edge:
2623
other = String()
2724
@@ -37,8 +34,8 @@ that implements ``Node`` will have a default Connection.
3734

3835
.. code:: python
3936
40-
class Faction(graphene.ObjectType):
41-
name = graphene.String()
37+
class Faction(ObjectType):
38+
name = String()
4239
ships = relay.ConnectionField(ShipConnection)
4340
4441
def resolve_ships(self, info):

docs/relay/mutations.rst

+10-13
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,12 @@ subclass of ``relay.ClientIDMutation``.
1212
.. code:: python
1313
1414
class IntroduceShip(relay.ClientIDMutation):
15-
1615
class Input:
17-
ship_name = graphene.String(required=True)
18-
faction_id = graphene.String(required=True)
16+
ship_name = String(required=True)
17+
faction_id = String(required=True)
1918
20-
ship = graphene.Field(Ship)
21-
faction = graphene.Field(Faction)
19+
ship = Field(Ship)
20+
faction = Field(Faction)
2221
2322
@classmethod
2423
def mutate_and_get_payload(cls, root, info, **input):
@@ -28,22 +27,20 @@ subclass of ``relay.ClientIDMutation``.
2827
faction = get_faction(faction_id)
2928
return IntroduceShip(ship=ship, faction=faction)
3029
31-
32-
3330
Accepting Files
3431
---------------
3532

3633
Mutations can also accept files, that's how it will work with different integrations:
3734

3835
.. code:: python
3936
40-
class UploadFile(graphene.ClientIDMutation):
41-
class Input:
42-
pass
43-
# nothing needed for uploading file
37+
class UploadFile(ClientIDMutation):
38+
class Input:
39+
pass
40+
# nothing needed for uploading file
4441
45-
# your return fields
46-
success = graphene.String()
42+
# your return fields
43+
success = String()
4744
4845
@classmethod
4946
def mutate_and_get_payload(cls, root, info, **input):

docs/relay/nodes.rst

+10-16
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,10 @@ Example usage (taken from the `Starwars Relay example`_):
1414

1515
.. code:: python
1616
17-
class Ship(graphene.ObjectType):
18-
'''A ship in the Star Wars saga'''
19-
class Meta:
20-
interfaces = (relay.Node, )
17+
class Ship(ObjectType, interfaces=(relay.Node,)):
18+
"""A ship in the Star Wars saga"""
2119
22-
name = graphene.String(description='The name of the ship.')
20+
name = String(description="The name of the ship.")
2321
2422
@classmethod
2523
def get_node(cls, info, id):
@@ -45,26 +43,22 @@ Example of a custom node:
4543

4644
.. code:: python
4745
48-
class CustomNode(Node):
49-
50-
class Meta:
51-
name = 'Node'
52-
46+
class CustomNode(Node, name="Node"):
5347
@staticmethod
5448
def to_global_id(type, id):
55-
return '{}:{}'.format(type, id)
49+
return "{}:{}".format(type, id)
5650
5751
@staticmethod
5852
def get_node_from_global_id(info, global_id, only_type=None):
59-
type, id = global_id.split(':')
53+
type, id = global_id.split(":")
6054
if only_type:
6155
# We assure that the node type that we want to retrieve
6256
# is the same that was indicated in the field type
63-
assert type == only_type._meta.name, 'Received not compatible node.'
57+
assert type == only_type._meta.name, "Received not compatible node."
6458
65-
if type == 'User':
59+
if type == "User":
6660
return get_user(id)
67-
elif type == 'Photo':
61+
elif type == "Photo":
6862
return get_photo(id)
6963
7064
@@ -94,7 +88,7 @@ Example usage:
9488

9589
.. code:: python
9690
97-
class Query(graphene.ObjectType):
91+
class Query(ObjectType):
9892
# Should be CustomNode.Field() if we want to use our custom Node
9993
node = relay.Node.Field()
10094

docs/testing/index.rst

+9-14
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,11 @@ To use the test client, instantiate ``graphene.test.Client`` and retrieve GraphQ
4040
4141
from graphene.test import Client
4242
43+
4344
def test_hey():
4445
client = Client(my_schema)
45-
executed = client.execute('''{ hey }''')
46-
assert executed == {
47-
'data': {
48-
'hey': 'hello!'
49-
}
50-
}
46+
executed = client.execute("""{ hey }""")
47+
assert executed == {"data": {"hey": "hello!"}}
5148
5249
5350
Execute parameters
@@ -61,14 +58,11 @@ You can also add extra keyword arguments to the ``execute`` method, such as
6158
6259
from graphene.test import Client
6360
61+
6462
def test_hey():
6563
client = Client(my_schema)
66-
executed = client.execute('''{ hey }''', context={'user': 'Peter'})
67-
assert executed == {
68-
'data': {
69-
'hey': 'hello Peter!'
70-
}
71-
}
64+
executed = client.execute("""{ hey }""", context={"user": "Peter"})
65+
assert executed == {"data": {"hey": "hello Peter!"}}
7266
7367
7468
Snapshot testing
@@ -95,7 +89,7 @@ Here is a simple example on how our tests will look if we use ``pytest``:
9589
# This will create a snapshot dir and a snapshot file
9690
# the first time the test is executed, with the response
9791
# of the execution.
98-
snapshot.assert_match(client.execute('''{ hey }'''))
92+
snapshot.assert_match(client.execute("""{ hey }"""))
9993
10094
10195
If we are using ``unittest``:
@@ -104,8 +98,9 @@ If we are using ``unittest``:
10498
10599
from snapshottest import TestCase
106100
101+
107102
class APITestCase(TestCase):
108103
def test_api_me(self):
109104
"""Testing the API for /me"""
110105
client = Client(my_schema)
111-
self.assertMatchSnapshot(client.execute('''{ hey }'''))
106+
self.assertMatchSnapshot(client.execute("""{ hey }"""))

docs/types/abstracttypes.rst

+8-6
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,20 @@ plus the ones defined in ``UserFields``.
2020

2121
.. code:: python
2222
23-
import graphene
23+
from graphene import AbstractType, ObjectType, InputObjectType, String
2424
25-
class UserFields(graphene.AbstractType):
26-
name = graphene.String()
2725
28-
class User(graphene.ObjectType, UserFields):
29-
pass
26+
class UserFields(AbstractType):
27+
name = String()
28+
3029
31-
class UserInput(graphene.InputObjectType, UserFields):
30+
class User(ObjectType, UserFields):
3231
pass
3332
3433
34+
class UserInput(InputObjectType, UserFields):
35+
pass
36+
3537
.. code::
3638
3739
type User {

0 commit comments

Comments
 (0)