Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@MarkusH
Last active February 1, 2019 21:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MarkusH/4842a7843ae1934d0fa9aa59ba239662 to your computer and use it in GitHub Desktop.
Save MarkusH/4842a7843ae1934d0fa9aa59ba239662 to your computer and use it in GitHub Desktop.
GraphQL graphene-python pain
from graphene import Argument, InputObjectType, Mutation, String
class ThingInput(InputObjectType):
foo = String(required=True)
class OtherInput(InputObjectType):
bar = String(required=True)
class ComponentsInput(InputObjectType):
thing = Argument(ThingInput, required=True)
other = Argument(OtherInput, required=True)
class ProductInput(InputObjectType):
description = String(required=True)
other = Argument(ComponentsInput, required=True)
class CreateProduct(Mutation):
id = String()
class Arguments:
input = Argument(ProductInput, required=True)
@staticmethod
def mutate(root, info, input):
# Do something
return CreateProduct(id=...)
from graphene import Argument, InputObjectType, Mutation, String
class EditThingInput(InputObjectType):
foo = String()
class EditOtherInput(InputObjectType):
bar = String()
class EditComponentsInput(InputObjectType):
thing = Argument(EditThingInput)
other = Argument(EditOtherInput)
class EditProductInput(InputObjectType):
description = String()
other = Argument(EditComponentsInput)
class EditProduct(Mutation):
id = String()
class Arguments:
id = String(required=True)
patch = Argument(EditProductInput)
@staticmethod
def mutate(root, info, id, patch):
# Fetch product from id
# Do something
return EditProduct(id=...)
from graphene import Field, ObjectType, String
class Thing(ObjectType):
name = String()
class Other(ObjectType):
name = String()
class Components(ObjectType):
thing = Field(Thing)
other = Field(Other)
class Product(ObjectType):
id = String()
description = String()
components = Field(Components)
class Query(ObjectType):
product = Field(Product)
def resolve_product(self, *args, **kwargs):
# Do something
return Product(id=..., description=..., components=...)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment