Skip to content

Commit

Permalink
Initial work refactoring class Meta to class arguments.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
changeling committed Jun 4, 2019
1 parent 67c4310 commit 65d799f
Show file tree
Hide file tree
Showing 24 changed files with 315 additions and 313 deletions.
8 changes: 4 additions & 4 deletions docs/execution/dataloader.rst
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,10 @@ leaner code and at most 4 database requests, and possibly fewer if there are cac

.. code:: python
class User(graphene.ObjectType):
name = graphene.String()
best_friend = graphene.Field(lambda: User)
friends = graphene.List(lambda: User)
class User(ObjectType):
name = String()
best_friend = Field(lambda: User)
friends = List(lambda: User)
def resolve_best_friend(self, info):
return user_loader.load(self.best_friend_id)
Expand Down
38 changes: 20 additions & 18 deletions docs/execution/execute.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ For executing a query a schema, you can directly call the ``execute`` method on

.. code:: python
schema = graphene.Schema(...)
result = schema.execute('{ name }')
schema = Schema(...)
result = schema.execute("{ name }")
``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.

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

.. code:: python
class Query(graphene.ObjectType):
name = graphene.String()
class Query(ObjectType):
name = String()
def resolve_name(root, info):
return info.context.get('name')
return info.context.get("name")
schema = graphene.Schema(Query)
result = schema.execute('{ name }', context={'name': 'Syrus'})
schema = Schema(Query)
result = schema.execute("{ name }", context={"name": "Syrus"})
Expand All @@ -40,22 +41,23 @@ You can pass variables to a query via ``variables``.

.. code:: python
class Query(graphene.ObjectType):
user = graphene.Field(User, id=graphene.ID(required=True))
class Query(ObjectType):
user = Field(User, id=ID(required=True))
def resolve_user(root, info, id):
return get_user_by_id(id)
schema = graphene.Schema(Query)
schema = Schema(Query)
result = schema.execute(
'''
query getUser($id: ID) {
"""
query getUser($id: ID) {
user(id: $id) {
id
firstName
lastName
id
firstName
lastName
}
}
}
''',
variables={'id': 12},
""",
variables={"id": 12},
)
20 changes: 11 additions & 9 deletions docs/quickstart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Let’s build a basic GraphQL schema from scratch.
Requirements
------------

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

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

.. code:: python
import graphene
from graphene import ObjectType, Schema, String
class Query(graphene.ObjectType):
hello = graphene.String(argument=graphene.String(default_value="stranger"))
class Query(ObjectType):
hello = String(argument=String(default_value="stranger"))
def resolve_hello(self, info, argument):
return 'Hello ' + argument
return f"Hello {argument}"
schema = graphene.Schema(query=Query)
schema = Schema(query=Query)
Querying
--------
Expand All @@ -52,11 +54,11 @@ Then we can start querying our schema:

.. code:: python
result = schema.execute('{ hello }')
print(result.data['hello']) # "Hello stranger"
result = schema.execute("{ hello }")
print(result.data["hello"]) # "Hello stranger"
# or passing the argument in the query
result = schema.execute('{ hello (argument: "graph") }')
print(result.data['hello']) # "Hello graph"
print(result.data["hello"]) # "Hello graph"
Congrats! You got your first graphene schema working!
9 changes: 3 additions & 6 deletions docs/relay/connection.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@ and ``other`` an extra field in the Connection Edge.

.. code:: python
class ShipConnection(Connection):
class ShipConnection(Connection, node=Ship):
extra = String()
class Meta:
node = Ship
class Edge:
other = String()
Expand All @@ -37,8 +34,8 @@ that implements ``Node`` will have a default Connection.

.. code:: python
class Faction(graphene.ObjectType):
name = graphene.String()
class Faction(ObjectType):
name = String()
ships = relay.ConnectionField(ShipConnection)
def resolve_ships(self, info):
Expand Down
23 changes: 10 additions & 13 deletions docs/relay/mutations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@ subclass of ``relay.ClientIDMutation``.
.. code:: python
class IntroduceShip(relay.ClientIDMutation):
class Input:
ship_name = graphene.String(required=True)
faction_id = graphene.String(required=True)
ship_name = String(required=True)
faction_id = String(required=True)
ship = graphene.Field(Ship)
faction = graphene.Field(Faction)
ship = Field(Ship)
faction = Field(Faction)
@classmethod
def mutate_and_get_payload(cls, root, info, **input):
Expand All @@ -28,22 +27,20 @@ subclass of ``relay.ClientIDMutation``.
faction = get_faction(faction_id)
return IntroduceShip(ship=ship, faction=faction)
Accepting Files
---------------

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

.. code:: python
class UploadFile(graphene.ClientIDMutation):
class Input:
pass
# nothing needed for uploading file
class UploadFile(ClientIDMutation):
class Input:
pass
# nothing needed for uploading file
# your return fields
success = graphene.String()
# your return fields
success = String()
@classmethod
def mutate_and_get_payload(cls, root, info, **input):
Expand Down
26 changes: 10 additions & 16 deletions docs/relay/nodes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@ Example usage (taken from the `Starwars Relay example`_):

.. code:: python
class Ship(graphene.ObjectType):
'''A ship in the Star Wars saga'''
class Meta:
interfaces = (relay.Node, )
class Ship(ObjectType, interfaces=(relay.Node,)):
"""A ship in the Star Wars saga"""
name = graphene.String(description='The name of the ship.')
name = String(description="The name of the ship.")
@classmethod
def get_node(cls, info, id):
Expand All @@ -45,26 +43,22 @@ Example of a custom node:

.. code:: python
class CustomNode(Node):
class Meta:
name = 'Node'
class CustomNode(Node, name="Node"):
@staticmethod
def to_global_id(type, id):
return '{}:{}'.format(type, id)
return "{}:{}".format(type, id)
@staticmethod
def get_node_from_global_id(info, global_id, only_type=None):
type, id = global_id.split(':')
type, id = global_id.split(":")
if only_type:
# We assure that the node type that we want to retrieve
# is the same that was indicated in the field type
assert type == only_type._meta.name, 'Received not compatible node.'
assert type == only_type._meta.name, "Received not compatible node."
if type == 'User':
if type == "User":
return get_user(id)
elif type == 'Photo':
elif type == "Photo":
return get_photo(id)
Expand Down Expand Up @@ -94,7 +88,7 @@ Example usage:

.. code:: python
class Query(graphene.ObjectType):
class Query(ObjectType):
# Should be CustomNode.Field() if we want to use our custom Node
node = relay.Node.Field()
Expand Down
23 changes: 9 additions & 14 deletions docs/testing/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,11 @@ To use the test client, instantiate ``graphene.test.Client`` and retrieve GraphQ
from graphene.test import Client
def test_hey():
client = Client(my_schema)
executed = client.execute('''{ hey }''')
assert executed == {
'data': {
'hey': 'hello!'
}
}
executed = client.execute("""{ hey }""")
assert executed == {"data": {"hey": "hello!"}}
Execute parameters
Expand All @@ -61,14 +58,11 @@ You can also add extra keyword arguments to the ``execute`` method, such as
from graphene.test import Client
def test_hey():
client = Client(my_schema)
executed = client.execute('''{ hey }''', context={'user': 'Peter'})
assert executed == {
'data': {
'hey': 'hello Peter!'
}
}
executed = client.execute("""{ hey }""", context={"user": "Peter"})
assert executed == {"data": {"hey": "hello Peter!"}}
Snapshot testing
Expand All @@ -95,7 +89,7 @@ Here is a simple example on how our tests will look if we use ``pytest``:
# This will create a snapshot dir and a snapshot file
# the first time the test is executed, with the response
# of the execution.
snapshot.assert_match(client.execute('''{ hey }'''))
snapshot.assert_match(client.execute("""{ hey }"""))
If we are using ``unittest``:
Expand All @@ -104,8 +98,9 @@ If we are using ``unittest``:
from snapshottest import TestCase
class APITestCase(TestCase):
def test_api_me(self):
"""Testing the API for /me"""
client = Client(my_schema)
self.assertMatchSnapshot(client.execute('''{ hey }'''))
self.assertMatchSnapshot(client.execute("""{ hey }"""))
14 changes: 8 additions & 6 deletions docs/types/abstracttypes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,20 @@ plus the ones defined in ``UserFields``.

.. code:: python
import graphene
from graphene import AbstractType, ObjectType, InputObjectType, String
class UserFields(graphene.AbstractType):
name = graphene.String()
class User(graphene.ObjectType, UserFields):
pass
class UserFields(AbstractType):
name = String()
class UserInput(graphene.InputObjectType, UserFields):
class User(ObjectType, UserFields):
pass
class UserInput(InputObjectType, UserFields):
pass
.. code::
type User {
Expand Down
Loading

0 comments on commit 65d799f

Please sign in to comment.