mirror of
https://github.com/graphql-python/graphene.git
synced 2025-09-21 19:32:33 +03:00
star wars with new enum
This commit is contained in:
parent
1db22d5840
commit
de15657dee
|
@ -3,14 +3,14 @@ droid_data = {}
|
|||
|
||||
|
||||
def setup():
|
||||
from .schema import Human, Droid, Episode
|
||||
from .schema import Human, Droid
|
||||
|
||||
global human_data, droid_data
|
||||
luke = Human(
|
||||
id="1000",
|
||||
name="Luke Skywalker",
|
||||
friends=["1002", "1003", "2000", "2001"],
|
||||
appears_in=[Episode.NEWHOPE, Episode.EMPIRE, Episode.JEDI],
|
||||
appears_in=[4, 5, 6],
|
||||
home_planet="Tatooine",
|
||||
)
|
||||
|
||||
|
@ -18,7 +18,7 @@ def setup():
|
|||
id="1001",
|
||||
name="Darth Vader",
|
||||
friends=["1004"],
|
||||
appears_in=[Episode.NEWHOPE, Episode.EMPIRE, Episode.JEDI],
|
||||
appears_in=[4, 5, 6],
|
||||
home_planet="Tatooine",
|
||||
)
|
||||
|
||||
|
@ -26,7 +26,7 @@ def setup():
|
|||
id="1002",
|
||||
name="Han Solo",
|
||||
friends=["1000", "1003", "2001"],
|
||||
appears_in=[Episode.NEWHOPE, Episode.EMPIRE, Episode.JEDI],
|
||||
appears_in=[4, 5, 6],
|
||||
home_planet=None,
|
||||
)
|
||||
|
||||
|
@ -34,7 +34,7 @@ def setup():
|
|||
id="1003",
|
||||
name="Leia Organa",
|
||||
friends=["1000", "1002", "2000", "2001"],
|
||||
appears_in=[Episode.NEWHOPE, Episode.EMPIRE, Episode.JEDI],
|
||||
appears_in=[4, 5, 6],
|
||||
home_planet="Alderaan",
|
||||
)
|
||||
|
||||
|
@ -42,7 +42,7 @@ def setup():
|
|||
id="1004",
|
||||
name="Wilhuff Tarkin",
|
||||
friends=["1001"],
|
||||
appears_in=[Episode.NEWHOPE],
|
||||
appears_in=[4],
|
||||
home_planet=None,
|
||||
)
|
||||
|
||||
|
@ -58,7 +58,7 @@ def setup():
|
|||
id="2000",
|
||||
name="C-3PO",
|
||||
friends=["1000", "1002", "1003", "2001"],
|
||||
appears_in=[Episode.NEWHOPE, Episode.EMPIRE, Episode.JEDI],
|
||||
appears_in=[4, 5, 6],
|
||||
primary_function="Protocol",
|
||||
)
|
||||
|
||||
|
@ -66,7 +66,7 @@ def setup():
|
|||
id="2001",
|
||||
name="R2-D2",
|
||||
friends=["1000", "1002", "1003"],
|
||||
appears_in=[Episode.NEWHOPE, Episode.EMPIRE, Episode.JEDI],
|
||||
appears_in=[4, 5, 6],
|
||||
primary_function="Astromech",
|
||||
)
|
||||
|
||||
|
@ -82,9 +82,7 @@ def get_friends(character):
|
|||
|
||||
|
||||
def get_hero(episode):
|
||||
from .schema import Episode
|
||||
|
||||
if episode == Episode.EMPIRE:
|
||||
if episode == 5:
|
||||
return human_data["1000"]
|
||||
return droid_data["2001"]
|
||||
|
||||
|
|
0
examples/starwars_newenum/__init__.py
Normal file
0
examples/starwars_newenum/__init__.py
Normal file
97
examples/starwars_newenum/data.py
Normal file
97
examples/starwars_newenum/data.py
Normal file
|
@ -0,0 +1,97 @@
|
|||
human_data = {}
|
||||
droid_data = {}
|
||||
|
||||
|
||||
def setup():
|
||||
from .schema import Human, Droid, Episode
|
||||
|
||||
global human_data, droid_data
|
||||
luke = Human(
|
||||
id="1000",
|
||||
name="Luke Skywalker",
|
||||
friends=["1002", "1003", "2000", "2001"],
|
||||
appears_in=[Episode.NEWHOPE, Episode.EMPIRE, Episode.JEDI],
|
||||
home_planet="Tatooine",
|
||||
)
|
||||
|
||||
vader = Human(
|
||||
id="1001",
|
||||
name="Darth Vader",
|
||||
friends=["1004"],
|
||||
appears_in=[Episode.NEWHOPE, Episode.EMPIRE, Episode.JEDI],
|
||||
home_planet="Tatooine",
|
||||
)
|
||||
|
||||
han = Human(
|
||||
id="1002",
|
||||
name="Han Solo",
|
||||
friends=["1000", "1003", "2001"],
|
||||
appears_in=[Episode.NEWHOPE, Episode.EMPIRE, Episode.JEDI],
|
||||
home_planet=None,
|
||||
)
|
||||
|
||||
leia = Human(
|
||||
id="1003",
|
||||
name="Leia Organa",
|
||||
friends=["1000", "1002", "2000", "2001"],
|
||||
appears_in=[Episode.NEWHOPE, Episode.EMPIRE, Episode.JEDI],
|
||||
home_planet="Alderaan",
|
||||
)
|
||||
|
||||
tarkin = Human(
|
||||
id="1004",
|
||||
name="Wilhuff Tarkin",
|
||||
friends=["1001"],
|
||||
appears_in=[Episode.NEWHOPE],
|
||||
home_planet=None,
|
||||
)
|
||||
|
||||
human_data = {
|
||||
"1000": luke,
|
||||
"1001": vader,
|
||||
"1002": han,
|
||||
"1003": leia,
|
||||
"1004": tarkin,
|
||||
}
|
||||
|
||||
c3po = Droid(
|
||||
id="2000",
|
||||
name="C-3PO",
|
||||
friends=["1000", "1002", "1003", "2001"],
|
||||
appears_in=[Episode.NEWHOPE, Episode.EMPIRE, Episode.JEDI],
|
||||
primary_function="Protocol",
|
||||
)
|
||||
|
||||
r2d2 = Droid(
|
||||
id="2001",
|
||||
name="R2-D2",
|
||||
friends=["1000", "1002", "1003"],
|
||||
appears_in=[Episode.NEWHOPE, Episode.EMPIRE, Episode.JEDI],
|
||||
primary_function="Astromech",
|
||||
)
|
||||
|
||||
droid_data = {"2000": c3po, "2001": r2d2}
|
||||
|
||||
|
||||
def get_character(id):
|
||||
return human_data.get(id) or droid_data.get(id)
|
||||
|
||||
|
||||
def get_friends(character):
|
||||
return map(get_character, character.friends)
|
||||
|
||||
|
||||
def get_hero(episode):
|
||||
from .schema import Episode
|
||||
|
||||
if episode == Episode.EMPIRE:
|
||||
return human_data["1000"]
|
||||
return droid_data["2001"]
|
||||
|
||||
|
||||
def get_human(id):
|
||||
return human_data.get(id)
|
||||
|
||||
|
||||
def get_droid(id):
|
||||
return droid_data.get(id)
|
55
examples/starwars_newenum/schema.py
Normal file
55
examples/starwars_newenum/schema.py
Normal file
|
@ -0,0 +1,55 @@
|
|||
from enum import Enum
|
||||
|
||||
import graphene
|
||||
|
||||
from .data import get_character, get_droid, get_hero, get_human
|
||||
|
||||
|
||||
class Episode(Enum):
|
||||
NEWHOPE = 4
|
||||
EMPIRE = 5
|
||||
JEDI = 6
|
||||
|
||||
GrapheneEpisode = graphene.Enum.from_enum(Episode, legacy_enum_resolver=False)
|
||||
|
||||
class Character(graphene.Interface):
|
||||
id = graphene.ID()
|
||||
name = graphene.String()
|
||||
friends = graphene.List(lambda: Character)
|
||||
appears_in = graphene.List(GrapheneEpisode)
|
||||
|
||||
def resolve_friends(self, info):
|
||||
# The character friends is a list of strings
|
||||
return [get_character(f) for f in self.friends]
|
||||
|
||||
|
||||
class Human(graphene.ObjectType):
|
||||
class Meta:
|
||||
interfaces = (Character,)
|
||||
|
||||
home_planet = graphene.String()
|
||||
|
||||
|
||||
class Droid(graphene.ObjectType):
|
||||
class Meta:
|
||||
interfaces = (Character,)
|
||||
|
||||
primary_function = graphene.String()
|
||||
|
||||
|
||||
class Query(graphene.ObjectType):
|
||||
hero = graphene.Field(Character, episode=GrapheneEpisode())
|
||||
human = graphene.Field(Human, id=graphene.String())
|
||||
droid = graphene.Field(Droid, id=graphene.String())
|
||||
|
||||
def resolve_hero(self, info, episode=None):
|
||||
return get_hero(episode)
|
||||
|
||||
def resolve_human(self, info, id):
|
||||
return get_human(id)
|
||||
|
||||
def resolve_droid(self, info, id):
|
||||
return get_droid(id)
|
||||
|
||||
|
||||
schema = graphene.Schema(query=Query)
|
0
examples/starwars_newenum/tests/__init__.py
Normal file
0
examples/starwars_newenum/tests/__init__.py
Normal file
100
examples/starwars_newenum/tests/snapshots/snap_test_query.py
Normal file
100
examples/starwars_newenum/tests/snapshots/snap_test_query.py
Normal file
|
@ -0,0 +1,100 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# snapshottest: v1 - https://goo.gl/zC4yUc
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from snapshottest import Snapshot
|
||||
|
||||
snapshots = Snapshot()
|
||||
|
||||
snapshots["test_hero_name_query 1"] = {"data": {"hero": {"name": "R2-D2"}}}
|
||||
|
||||
snapshots["test_hero_name_and_friends_query 1"] = {
|
||||
"data": {
|
||||
"hero": {
|
||||
"id": "2001",
|
||||
"name": "R2-D2",
|
||||
"friends": [
|
||||
{"name": "Luke Skywalker"},
|
||||
{"name": "Han Solo"},
|
||||
{"name": "Leia Organa"},
|
||||
],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
snapshots["test_nested_query 1"] = {
|
||||
"data": {
|
||||
"hero": {
|
||||
"name": "R2-D2",
|
||||
"friends": [
|
||||
{
|
||||
"name": "Luke Skywalker",
|
||||
"appearsIn": ["NEWHOPE", "EMPIRE", "JEDI"],
|
||||
"friends": [
|
||||
{"name": "Han Solo"},
|
||||
{"name": "Leia Organa"},
|
||||
{"name": "C-3PO"},
|
||||
{"name": "R2-D2"},
|
||||
],
|
||||
},
|
||||
{
|
||||
"name": "Han Solo",
|
||||
"appearsIn": ["NEWHOPE", "EMPIRE", "JEDI"],
|
||||
"friends": [
|
||||
{"name": "Luke Skywalker"},
|
||||
{"name": "Leia Organa"},
|
||||
{"name": "R2-D2"},
|
||||
],
|
||||
},
|
||||
{
|
||||
"name": "Leia Organa",
|
||||
"appearsIn": ["NEWHOPE", "EMPIRE", "JEDI"],
|
||||
"friends": [
|
||||
{"name": "Luke Skywalker"},
|
||||
{"name": "Han Solo"},
|
||||
{"name": "C-3PO"},
|
||||
{"name": "R2-D2"},
|
||||
],
|
||||
},
|
||||
],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
snapshots["test_fetch_luke_query 1"] = {"data": {"human": {"name": "Luke Skywalker"}}}
|
||||
|
||||
snapshots["test_fetch_some_id_query 1"] = {
|
||||
"data": {"human": {"name": "Luke Skywalker"}}
|
||||
}
|
||||
|
||||
snapshots["test_fetch_some_id_query2 1"] = {"data": {"human": {"name": "Han Solo"}}}
|
||||
|
||||
snapshots["test_invalid_id_query 1"] = {"data": {"human": None}}
|
||||
|
||||
snapshots["test_fetch_luke_aliased 1"] = {"data": {"luke": {"name": "Luke Skywalker"}}}
|
||||
|
||||
snapshots["test_fetch_luke_and_leia_aliased 1"] = {
|
||||
"data": {"luke": {"name": "Luke Skywalker"}, "leia": {"name": "Leia Organa"}}
|
||||
}
|
||||
|
||||
snapshots["test_duplicate_fields 1"] = {
|
||||
"data": {
|
||||
"luke": {"name": "Luke Skywalker", "homePlanet": "Tatooine"},
|
||||
"leia": {"name": "Leia Organa", "homePlanet": "Alderaan"},
|
||||
}
|
||||
}
|
||||
|
||||
snapshots["test_use_fragment 1"] = {
|
||||
"data": {
|
||||
"luke": {"name": "Luke Skywalker", "homePlanet": "Tatooine"},
|
||||
"leia": {"name": "Leia Organa", "homePlanet": "Alderaan"},
|
||||
}
|
||||
}
|
||||
|
||||
snapshots["test_check_type_of_r2 1"] = {
|
||||
"data": {"hero": {"__typename": "Droid", "name": "R2-D2"}}
|
||||
}
|
||||
|
||||
snapshots["test_check_type_of_luke 1"] = {
|
||||
"data": {"hero": {"__typename": "Human", "name": "Luke Skywalker"}}
|
||||
}
|
182
examples/starwars_newenum/tests/test_query.py
Normal file
182
examples/starwars_newenum/tests/test_query.py
Normal file
|
@ -0,0 +1,182 @@
|
|||
from graphene.test import Client
|
||||
|
||||
from ..data import setup
|
||||
from ..schema import schema
|
||||
|
||||
setup()
|
||||
|
||||
client = Client(schema)
|
||||
|
||||
|
||||
def test_hero_name_query(snapshot):
|
||||
query = """
|
||||
query HeroNameQuery {
|
||||
hero {
|
||||
name
|
||||
}
|
||||
}
|
||||
"""
|
||||
snapshot.assert_match(client.execute(query))
|
||||
|
||||
|
||||
def test_hero_name_and_friends_query(snapshot):
|
||||
query = """
|
||||
query HeroNameAndFriendsQuery {
|
||||
hero {
|
||||
id
|
||||
name
|
||||
friends {
|
||||
name
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
snapshot.assert_match(client.execute(query))
|
||||
|
||||
|
||||
def test_nested_query(snapshot):
|
||||
query = """
|
||||
query NestedQuery {
|
||||
hero {
|
||||
name
|
||||
friends {
|
||||
name
|
||||
appearsIn
|
||||
friends {
|
||||
name
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
snapshot.assert_match(client.execute(query))
|
||||
|
||||
|
||||
def test_fetch_luke_query(snapshot):
|
||||
query = """
|
||||
query FetchLukeQuery {
|
||||
human(id: "1000") {
|
||||
name
|
||||
}
|
||||
}
|
||||
"""
|
||||
snapshot.assert_match(client.execute(query))
|
||||
|
||||
|
||||
def test_fetch_some_id_query(snapshot):
|
||||
query = """
|
||||
query FetchSomeIDQuery($someId: String!) {
|
||||
human(id: $someId) {
|
||||
name
|
||||
}
|
||||
}
|
||||
"""
|
||||
params = {"someId": "1000"}
|
||||
snapshot.assert_match(client.execute(query, variables=params))
|
||||
|
||||
|
||||
def test_fetch_some_id_query2(snapshot):
|
||||
query = """
|
||||
query FetchSomeIDQuery($someId: String!) {
|
||||
human(id: $someId) {
|
||||
name
|
||||
}
|
||||
}
|
||||
"""
|
||||
params = {"someId": "1002"}
|
||||
snapshot.assert_match(client.execute(query, variables=params))
|
||||
|
||||
|
||||
def test_invalid_id_query(snapshot):
|
||||
query = """
|
||||
query humanQuery($id: String!) {
|
||||
human(id: $id) {
|
||||
name
|
||||
}
|
||||
}
|
||||
"""
|
||||
params = {"id": "not a valid id"}
|
||||
snapshot.assert_match(client.execute(query, variables=params))
|
||||
|
||||
|
||||
def test_fetch_luke_aliased(snapshot):
|
||||
query = """
|
||||
query FetchLukeAliased {
|
||||
luke: human(id: "1000") {
|
||||
name
|
||||
}
|
||||
}
|
||||
"""
|
||||
snapshot.assert_match(client.execute(query))
|
||||
|
||||
|
||||
def test_fetch_luke_and_leia_aliased(snapshot):
|
||||
query = """
|
||||
query FetchLukeAndLeiaAliased {
|
||||
luke: human(id: "1000") {
|
||||
name
|
||||
}
|
||||
leia: human(id: "1003") {
|
||||
name
|
||||
}
|
||||
}
|
||||
"""
|
||||
snapshot.assert_match(client.execute(query))
|
||||
|
||||
|
||||
def test_duplicate_fields(snapshot):
|
||||
query = """
|
||||
query DuplicateFields {
|
||||
luke: human(id: "1000") {
|
||||
name
|
||||
homePlanet
|
||||
}
|
||||
leia: human(id: "1003") {
|
||||
name
|
||||
homePlanet
|
||||
}
|
||||
}
|
||||
"""
|
||||
snapshot.assert_match(client.execute(query))
|
||||
|
||||
|
||||
def test_use_fragment(snapshot):
|
||||
query = """
|
||||
query UseFragment {
|
||||
luke: human(id: "1000") {
|
||||
...HumanFragment
|
||||
}
|
||||
leia: human(id: "1003") {
|
||||
...HumanFragment
|
||||
}
|
||||
}
|
||||
fragment HumanFragment on Human {
|
||||
name
|
||||
homePlanet
|
||||
}
|
||||
"""
|
||||
snapshot.assert_match(client.execute(query))
|
||||
|
||||
|
||||
def test_check_type_of_r2(snapshot):
|
||||
query = """
|
||||
query CheckTypeOfR2 {
|
||||
hero {
|
||||
__typename
|
||||
name
|
||||
}
|
||||
}
|
||||
"""
|
||||
snapshot.assert_match(client.execute(query))
|
||||
|
||||
|
||||
def test_check_type_of_luke(snapshot):
|
||||
query = """
|
||||
query CheckTypeOfLuke {
|
||||
hero(episode: EMPIRE) {
|
||||
__typename
|
||||
name
|
||||
}
|
||||
}
|
||||
"""
|
||||
snapshot.assert_match(client.execute(query))
|
0
examples/starwars_newenum/tests/test_schema.py
Normal file
0
examples/starwars_newenum/tests/test_schema.py
Normal file
Loading…
Reference in New Issue
Block a user