Slides When Python Meets Graphql Sortinghat
Slides When Python Meets Graphql Sortinghat
GraphQL
Managing contributor identities
in your Open-source project
Software developer...
Tom Riddle
Lord Voldemort
jsmanrique
[email protected]
[email protected]
[email protected]
jsmanrique
02/2005 - 12/2010 CTIC
01/2010 - 12/2012 Andago
01/2013 - 06/2013 TapQuo
07/2013 - 12/2015 freelance (ASOLIF, CENATIC)
07/2013 - now Bitergia
Project manager
Name: Tom
Complete the profile!
Gender: Male
Photo credit: James Seattle
Email: [email protected]
Ensure consistency
Python module
Overfetching / Underfetching
Implement basic
Authentication
queries & mutations
GraphQL
Schema
s
ion
Qu
eri
tat
es
Mu
share this slide! @mghfdez
Schema.py
Models
GraphQL
Schema:
Re
ns
era D
Graphene-Django
so
tio
U
CR
lve
rs
op
share this slide! @mghfdez
It is already a graph Name: Tom
Gender: Male
Email: [email protected]
Profile
Lord Voldemort
Identities
Affiliations
slytherin.edu
return AddOrganization(
organization=org
)
schema.py
share this slide! @mghfdez
(Basic) Recipe for building mutations
@django.db.transaction.atomic def add_organization(name):
def add_organization(name):
validate_field('name', name)
try: organization = Organization(name=name)
org = add_organization_db(name=name)
except ValueError as e: try:
raise InvalidValueError(msg=str(e)) organization.save()
except AlreadyExistsError as exc: except django.db.utils.IntegrityError as exc:
raise exc _handle_integrity_error(Organization, exc)
api.py db.py
identities(first:2 after:$uuid)
It is a property of the connection,
not of the object. identities(first:2 after:$uuidCursor)
Friend B
@classmethod
def create_paginated_result(cls, query, page=1,
page_size=DEFAULT_SIZE):
Django objects paginator = Paginator(query, page_size)
result = paginator.page(page)
Query results
entities = result.object_list
page_info = PaginationType(
page=result.number,
page_size=page_size,
num_pages=paginator.num_pages,
Pagination info has_next=result.has_next(),
has_prev=result.has_previous(),
start_index=result.start_index(),
end_index=result.end_index(),
total_results=len(query)
)
class SortingHatQuery:
def resolve_organizations(...)
(...)
return OrganizationPaginatedType.create_paginated_result(query,
page,
page_size=page_size)
Heads-up!
Configuring the Django CSRF token properly was not trivial
Insomnia app
def setUp(self):
self.user = get_user_model().objects.create(username='test')
self.context_value = RequestFactory().get(GRAPHQL_ENDPOINT)
self.context_value.user = self.user
def test_add_organization(self):
client = graphene.test.Client(schema)
executed = client.execute(self.SH_ADD_ORG, context_value=self.context_value)
class SortingHatQuery:
organizations = graphene.Field(
OrganizationPaginatedType,
page_size=graphene.Int(),
page=graphene.Int(),
filters=OrganizationFilterType(required=False)
)
def resolve_organizations(...):
# Modified resolver
Feedback is welcome!
Email [email protected]
GitHub mafesan