1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
# https://github.com/graphql-python/graphene/issues/313
import graphene
class Query(graphene.ObjectType):
rand = graphene.String()
class Success(graphene.ObjectType):
yeah = graphene.String()
class Error(graphene.ObjectType):
message = graphene.String()
class CreatePostResult(graphene.Union):
class Meta:
types = [Success, Error]
class CreatePost(graphene.Mutation):
class Arguments:
text = graphene.String(required=True)
result = graphene.Field(CreatePostResult)
def mutate(self, info, text):
result = Success(yeah="yeah")
return CreatePost(result=result)
class Mutations(graphene.ObjectType):
create_post = CreatePost.Field()
# tests.py
def test_create_post():
query_string = """
mutation {
createPost(text: "Try this out") {
result {
__typename
}
}
}
"""
schema = graphene.Schema(query=Query, mutation=Mutations)
result = schema.execute(query_string)
assert not result.errors
assert result.data["createPost"]["result"]["__typename"] == "Success"
|