|
| 1 | +from graphql.core.type import ( |
| 2 | + GraphQLEnumType, |
| 3 | + GraphQLEnumValue, |
| 4 | + GraphQLInterfaceType, |
| 5 | + GraphQLObjectType, |
| 6 | + GraphQLField, |
| 7 | + GraphQLArgument, |
| 8 | + GraphQLList, |
| 9 | + GraphQLNonNull, |
| 10 | + GraphQLSchema, |
| 11 | + GraphQLString, |
| 12 | +) |
| 13 | +import starwars_fixtures |
| 14 | + |
| 15 | +episodeEnum = GraphQLEnumType( |
| 16 | + 'Episode', |
| 17 | + description='One of the films in the Star Wars Trilogy', |
| 18 | + values={ |
| 19 | + 'NEWHOPE': GraphQLEnumValue( |
| 20 | + 4, |
| 21 | + description='Released in 1977.', |
| 22 | + ), |
| 23 | + 'EMPIRE': GraphQLEnumValue( |
| 24 | + 5, |
| 25 | + description='Released in 1980.', |
| 26 | + ), |
| 27 | + 'JEDI': GraphQLEnumValue( |
| 28 | + 6, |
| 29 | + description='Released in 1983.', |
| 30 | + ) |
| 31 | + } |
| 32 | +) |
| 33 | + |
| 34 | +characterInterface = GraphQLInterfaceType( |
| 35 | + 'Character', |
| 36 | + description='A character in the Star Wars Trilogy', |
| 37 | + fields=lambda: { |
| 38 | + 'id': GraphQLField( |
| 39 | + GraphQLNonNull(GraphQLString), |
| 40 | + description='The id of the character.' |
| 41 | + ), |
| 42 | + 'name': GraphQLField( |
| 43 | + GraphQLString, |
| 44 | + description='The name of the character.' |
| 45 | + ), |
| 46 | + 'friends': GraphQLField( |
| 47 | + GraphQLList(characterInterface), |
| 48 | + description='The friends of the character, or an empty list if they have none.' |
| 49 | + ), |
| 50 | + 'appearsIn': GraphQLField( |
| 51 | + GraphQLList(episodeEnum), |
| 52 | + description='Which movies they appear in.' |
| 53 | + ), |
| 54 | + }, |
| 55 | + resolve_type=lambda character, *_: humanType if starwars_fixtures.getHuman(character.id) else droidType, |
| 56 | +) |
| 57 | + |
| 58 | +humanType = GraphQLObjectType( |
| 59 | + 'Human', |
| 60 | + description='A humanoid creature in the Star Wars universe.', |
| 61 | + fields=lambda: { |
| 62 | + 'id': GraphQLField( |
| 63 | + GraphQLNonNull(GraphQLString), |
| 64 | + description='The id of the human.', |
| 65 | + ), |
| 66 | + 'name': GraphQLField( |
| 67 | + GraphQLString, |
| 68 | + description='The name of the human.', |
| 69 | + ), |
| 70 | + 'friends': GraphQLField( |
| 71 | + GraphQLList(characterInterface), |
| 72 | + description='The friends of the human, or an empty list if they have none.', |
| 73 | + resolver=lambda human, *_: starwars_fixtures.getFriends(human), |
| 74 | + ), |
| 75 | + 'appearsIn': GraphQLField( |
| 76 | + GraphQLList(episodeEnum), |
| 77 | + description='Which movies they appear in.', |
| 78 | + ), |
| 79 | + 'homePlanet': GraphQLField( |
| 80 | + GraphQLString, |
| 81 | + description='The home planet of the human, or null if unknown.', |
| 82 | + ) |
| 83 | + }, |
| 84 | + interfaces=[characterInterface] |
| 85 | +) |
| 86 | + |
| 87 | +droidType = GraphQLObjectType( |
| 88 | + 'Droid', |
| 89 | + description='A mechanical creature in the Star Wars universe.', |
| 90 | + fields=lambda: { |
| 91 | + 'id': GraphQLField( |
| 92 | + GraphQLNonNull(GraphQLString), |
| 93 | + description='The id of the droid.', |
| 94 | + ), |
| 95 | + 'name': GraphQLField( |
| 96 | + GraphQLString, |
| 97 | + description='The name of the droid.', |
| 98 | + ), |
| 99 | + 'friends': GraphQLField( |
| 100 | + GraphQLList(characterInterface), |
| 101 | + description='The friends of the droid, or an empty list if they have none.', |
| 102 | + resolver=lambda droid, *_: starwars_fixtures.getFriends(droid), |
| 103 | + ), |
| 104 | + 'appearsIn': GraphQLField( |
| 105 | + GraphQLList(episodeEnum), |
| 106 | + description='Which movies they appear in.', |
| 107 | + ), |
| 108 | + 'primaryFunction': GraphQLField( |
| 109 | + GraphQLString, |
| 110 | + description='The primary function of the droid.', |
| 111 | + ) |
| 112 | + }, |
| 113 | + interfaces=[characterInterface] |
| 114 | +) |
| 115 | + |
| 116 | +queryType = GraphQLObjectType( |
| 117 | + 'Query', |
| 118 | + fields=lambda: { |
| 119 | + 'hero': GraphQLField( |
| 120 | + characterInterface, |
| 121 | + args={ |
| 122 | + 'episode': GraphQLArgument( |
| 123 | + description='If omitted, returns the hero of the whole saga. If ' |
| 124 | + 'provided, returns the hero of that particular episode.', |
| 125 | + type=episodeEnum, |
| 126 | + ) |
| 127 | + }, |
| 128 | + resolver=lambda root, args, *_: starwars_fixtures.getHero(args['episode']), |
| 129 | + ), |
| 130 | + 'human': GraphQLField( |
| 131 | + humanType, |
| 132 | + args={ |
| 133 | + 'id': GraphQLArgument( |
| 134 | + description='id of the human', |
| 135 | + type=GraphQLNonNull(GraphQLString), |
| 136 | + ) |
| 137 | + }, |
| 138 | + resolver=lambda root, args, *_: starwars_fixtures.getHuman(args['id']), |
| 139 | + ), |
| 140 | + 'droid': GraphQLField( |
| 141 | + droidType, |
| 142 | + args={ |
| 143 | + 'id': GraphQLArgument( |
| 144 | + description='id of the droid', |
| 145 | + type=GraphQLNonNull(GraphQLString), |
| 146 | + ) |
| 147 | + }, |
| 148 | + resolver=lambda root, args, *_: starwars_fixtures.getDroid(args['id']), |
| 149 | + ), |
| 150 | + } |
| 151 | +) |
| 152 | + |
| 153 | +StarWarsSchema = GraphQLSchema(query=queryType) |
0 commit comments