mirror of
https://github.com/graphql-python/graphene.git
synced 2025-07-24 23:09:46 +03:00
Fixed Flake8
This commit is contained in:
parent
2feb62f597
commit
21e9f7a977
|
@ -64,10 +64,12 @@ USER_DATA = [
|
|||
},
|
||||
]
|
||||
|
||||
|
||||
def fetch_user_from_database(user_id):
|
||||
user, = [user for user in USER_DATA if user['user_id'] == user_id]
|
||||
return user
|
||||
|
||||
|
||||
def fetch_users_from_database(args):
|
||||
full_name = args.get('fullName', None)
|
||||
if full_name:
|
||||
|
@ -78,8 +80,9 @@ def fetch_users_from_database(args):
|
|||
if sort_direction and sort_direction == 'DESC':
|
||||
reversed = True
|
||||
|
||||
filtered_users = [user for user in USER_DATA if full_name is None or full_name in
|
||||
user['first_name'].upper() + ' ' + user['last_name'].upper()]
|
||||
filtered_users = [user for user in USER_DATA if full_name is None or
|
||||
full_name in user['first_name'].upper() + ' ' +
|
||||
user['last_name'].upper()]
|
||||
if sort_key and sort_direction:
|
||||
filtered_users.sort(lambda item: item[sort_key], reverse=reversed)
|
||||
|
||||
|
@ -87,39 +90,46 @@ def fetch_users_from_database(args):
|
|||
|
||||
schema = graphene.Schema()
|
||||
|
||||
|
||||
class User(relay.Node):
|
||||
"""A User fetched from the database"""
|
||||
user_id = graphene.ID()
|
||||
first_name = graphene.String()
|
||||
last_name = graphene.String()
|
||||
full_name = graphene.String(description='A field created by setting the first and last name.')
|
||||
full_name = graphene.String(
|
||||
description='A field created by setting the first and last name.')
|
||||
email_address = graphene.String()
|
||||
age = graphene.Int()
|
||||
|
||||
@classmethod
|
||||
def get_node(cls, user_id, info):
|
||||
user_dict = fetch_user_from_database(user_id)
|
||||
# user_dict will contain the fields, user_id, first_name, last_name, email_address and age
|
||||
user_dict['id'] = user_dict['user_id'] # will be used to set the global ID used by relay
|
||||
# user_dict will contain the fields, user_id, first_name, last_name,
|
||||
# email_address and age
|
||||
# will be used to set the global ID used by relay
|
||||
user_dict['id'] = user_dict['user_id']
|
||||
return User(**user_dict)
|
||||
|
||||
def resolve_full_name(self, *args):
|
||||
return ' '.join([self.first_name, self.last_name])
|
||||
|
||||
|
||||
def _user_container(user):
|
||||
# user_dict will contain the fields, user_id, first_name, last_name, email_address and age
|
||||
# user_dict will contain the fields, user_id, first_name, last_name,
|
||||
# email_address and age
|
||||
user['id'] = user['user_id']
|
||||
return User(**user)
|
||||
|
||||
|
||||
# This will be our root query
|
||||
class Query(graphene.ObjectType):
|
||||
users = graphene.List(User,
|
||||
fullName=graphene.String(),
|
||||
sortKey=graphene.String(),
|
||||
sortDirection=graphene.String())
|
||||
fullName=graphene.String(),
|
||||
sortKey=graphene.String(),
|
||||
sortDirection=graphene.String())
|
||||
user = relay.NodeField(User)
|
||||
viewer = graphene.Field('self') # needed for Relay
|
||||
|
||||
viewer = graphene.Field('self') # needed for Relay
|
||||
|
||||
# args will be a dict with 'fullName', 'sortKey' and 'sortDirection'
|
||||
# info is an object with information about the query being sent
|
||||
def resolve_users(self, args, info):
|
||||
|
@ -138,7 +148,9 @@ Then, we can start querying our schema:
|
|||
```python
|
||||
result = schema.execute('query { users { fullName } }')
|
||||
|
||||
# result.data should be {'users': [{fullName: 'Peter Cabbage'}, {fullName: 'Lukas Chart'}, {fullName: 'Marie Cauliflower'}]}
|
||||
# result.data should be
|
||||
# {'users': [{fullName: 'Peter Cabbage'}, {fullName: 'Lukas Chart'},
|
||||
# {fullName: 'Marie Cauliflower'}]}
|
||||
users = result.data['users']
|
||||
|
||||
print(users)
|
||||
|
|
Loading…
Reference in New Issue
Block a user