Created
April 6, 2016 06:49
-
-
Save syrusakbary/e0f19549843a40fd738e8f2183e57ece to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import graphene | |
| from graphene import relay | |
| from graphene.core.classtypes import UnionType | |
| schema = graphene.Schema(name='Starwars Relay Schema') | |
| class Adjective(relay.Node): | |
| '''An adjective''' | |
| string = graphene.String(description='The adjective string') | |
| def get_node(self, args, info): | |
| pass | |
| class Verb(relay.Node): | |
| '''A verb''' | |
| string = graphene.String(description='The verb string') | |
| def get_node(self, args, info): | |
| pass | |
| class AdjectiveOrVerb(UnionType): | |
| class Meta: | |
| types = [Adjective, Verb] | |
| class GenericWord(relay.Node): | |
| word = graphene.Field(AdjectiveOrVerb) | |
| def get_node(self, args, info): | |
| pass | |
| class Query(graphene.ObjectType): | |
| # Using a UnionType is not supported by the Relay spec | |
| # as it requires a ObjectType | |
| # https://github.com/graphql/graphql-relay-js/issues/41 | |
| # so we will use a GenericWord wrapper around it | |
| # things_to_learn = relay.ConnectionField(AdjectiveOrVerb) | |
| things_to_learn = relay.ConnectionField(GenericWord) | |
| node = relay.NodeField() | |
| def resolve_things_to_learn(self, args, info): | |
| return [GenericWord(word=Adjective(string='warm')), GenericWord(word=Verb(string='look'))] | |
| schema.query = Query | |
| def test_query(): | |
| query = ''' | |
| query { | |
| thingsToLearn { | |
| edges { | |
| node { | |
| word { | |
| ... on Adjective { | |
| string | |
| } | |
| ... on Verb { | |
| string | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| ''' | |
| expected = { | |
| 'thingsToLearn': { | |
| 'edges': [ | |
| { | |
| 'node': { | |
| 'word': { | |
| 'string': 'warm' | |
| } | |
| } | |
| }, | |
| { | |
| 'node': { | |
| 'word': { | |
| 'string': 'look' | |
| } | |
| } | |
| } | |
| ] | |
| } | |
| } | |
| result = schema.execute(query) | |
| assert not result.errors | |
| assert result.data == expected | |
| print result.data | |
| if __name__ == '__main__': | |
| test_query() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment