コード例 #1
0
    def test_simple_query_with_enums_default_value(self):
        """
            enum LengthUnit {
              METER
              KM
            }

            type Starship {
              id: ID!
              name: String!
              length(unit: LengthUnit = METER): Float
            }

            type Query {
                ship(id: String!): Starship
            }
        """

        length_unit_enum = GraphQLEnumType(
            "LengthUnit",
            {
                "METER": GraphQLEnumValue("METER"),
                "KM": GraphQLEnumValue("KM")
            },
            description="One of the films in the Star Wars Trilogy",
        )

        starship_type = GraphQLObjectType(
            "Starship",
            lambda: {
                "id":
                GraphQLField(GraphQLNonNull(GraphQLString),
                             description="The id of the ship."),
                "name":
                GraphQLField(GraphQLString,
                             description="The name of the ship."),
                "length":
                GraphQLField(
                    GraphQLInt,
                    args={
                        "unit":
                        GraphQLArgument(
                            GraphQLNonNull(length_unit_enum),
                            default_value="METER",
                            description="id of the droid",
                        )
                    },
                ),
            },
        )

        query_type = GraphQLObjectType(
            "Query",
            lambda: {
                "ship":
                GraphQLField(
                    starship_type,
                    args={
                        "id":
                        GraphQLArgument(GraphQLNonNull(GraphQLString),
                                        description="id of the ship")
                    },
                )
            },
        )

        schema = GraphQLSchema(query_type,
                               types=[length_unit_enum, starship_type])

        query = """
            query GetStarship {
                ship(id: "Enterprise") {
                    id
                    name
                    length(unit: METER)
                }
            }
        """
        query_parser = QueryParser(schema)
        query_renderer = DataclassesRenderer(schema)
        parsed = query_parser.parse(query)

        rendered_enums = query_renderer.render_enums(parsed)
        for rendered_enum in rendered_enums:
            self.load_module(rendered_enum)

        rendered = query_renderer.render(parsed)
        m = self.load_module(rendered)

        response = m.GetStarship.from_json("""
            {
                "data": {
                    "ship": {
                        "id": "Enterprise",
                        "name": "Enterprise",
                        "length": 100
                    }
                }
            }
        """)

        assert response

        ship = response.data.ship
        assert ship
        assert ship.id == "Enterprise"
        assert ship.name == "Enterprise"
        assert ship.length == 100
コード例 #2
0
 def from_enum_value(self, enum_value: EnumValue) -> GraphQLEnumValue:
     return GraphQLEnumValue(enum_value.value)
コード例 #3
0
ファイル: app.py プロジェクト: jono-allen/sanicql
def get_droid(id):
    return Droid(id='2001',
                 name='R2-D2',
                 friends=['1000', '1002', '1003'],
                 appearsIn=[4, 5, 6],
                 primaryFunction='Astromech')


def get_secret_backstory(character):
    return None


episode_enum = GraphQLEnumType(
    'Episode', {
        'NEWHOPE': GraphQLEnumValue(4, description='Released in 1977.'),
        'EMPIRE': GraphQLEnumValue(5, description='Released in 1980.'),
        'JEDI': GraphQLEnumValue(6, description='Released in 1983.')
    },
    description='One of the films in the Star Wars Trilogy')

character_interface = GraphQLInterfaceType(
    'Character',
    lambda: {
        'id':
        GraphQLField(GraphQLNonNull(GraphQLString),
                     description='The id of the character.'),
        'name':
        GraphQLField(GraphQLString, description='The name of the character.'),
        'friends':
        GraphQLField(GraphQLList(character_interface),
コード例 #4
0
    GraphQLObjectType,
    GraphQLString,
    GraphQLField,
    GraphQLNonNull,
    GraphQLID,
    GraphQLEnumType,
    GraphQLEnumValue,
    GraphQLList
)

from ...utils.resolver import resolve_with_loader

ContestStatusType = GraphQLEnumType(
    name='ContestStatusType',
    values=OrderedDict([
        ('draft', GraphQLEnumValue('draft')),
        ('published', GraphQLEnumValue('published')),
        ('archived', GraphQLEnumValue('archived')),
    ])
)

ContestType = GraphQLObjectType(
    name='ContestType',
    fields=lambda: {
        'id': GraphQLField(GraphQLID),
        'code': GraphQLField(GraphQLNonNull(GraphQLString)),
        'title': GraphQLField(GraphQLNonNull(GraphQLString)),
        'description': GraphQLField(GraphQLString),
        'status': GraphQLField(GraphQLNonNull(ContestStatusType)),
        'createdAt': GraphQLField(GraphQLNonNull(GraphQLString)),
        'names': GraphQLField(
コード例 #5
0
ファイル: test_enum.py プロジェクト: test-patrick/strawberry
def test_create_enum():
    @strawberry.enum
    class StringTest(Enum):
        A = "c"
        B = "i"
        C = "a"
        D = "o"

    assert StringTest.field

    assert type(StringTest.field) == GraphQLEnumType
    assert StringTest.field.name == "StringTest"

    assert StringTest.field.values == {
        "A": GraphQLEnumValue("c"),
        "B": GraphQLEnumValue("i"),
        "C": GraphQLEnumValue("a"),
        "D": GraphQLEnumValue("o"),
    }

    @strawberry.enum
    class IntTest(Enum):
        A = 1
        B = 2
        C = 3

    assert IntTest.field

    assert type(IntTest.field) == GraphQLEnumType
    assert IntTest.field.name == "IntTest"

    assert IntTest.field.values == {
        "A": GraphQLEnumValue(1),
        "B": GraphQLEnumValue(2),
        "C": GraphQLEnumValue(3),
    }

    @strawberry.enum
    class ComplexTest(Enum):
        MERCURY = (3.303e23, 2.4397e6)
        VENUS = (4.869e24, 6.0518e6)
        EARTH = (5.976e24, 6.37814e6)
        MARS = (6.421e23, 3.3972e6)
        JUPITER = (1.9e27, 7.1492e7)
        SATURN = (5.688e26, 6.0268e7)
        URANUS = (8.686e25, 2.5559e7)
        NEPTUNE = (1.024e26, 2.4746e7)

        def __init__(self, mass, radius):
            self.mass = mass
            self.radius = radius

        @property
        def surface_gravity(self):
            G = 6.67300e-11
            return G * self.mass / (self.radius * self.radius)

    assert ComplexTest.field

    assert type(ComplexTest.field) == GraphQLEnumType
    assert ComplexTest.field.name == "ComplexTest"

    assert ComplexTest.field.values == {
        "MERCURY": GraphQLEnumValue((3.303e23, 2.4397e6)),
        "VENUS": GraphQLEnumValue((4.869e24, 6.0518e6)),
        "EARTH": GraphQLEnumValue((5.976e24, 6.37814e6)),
        "MARS": GraphQLEnumValue((6.421e23, 3.3972e6)),
        "JUPITER": GraphQLEnumValue((1.9e27, 7.1492e7)),
        "SATURN": GraphQLEnumValue((5.688e26, 6.0268e7)),
        "URANUS": GraphQLEnumValue((8.686e25, 2.5559e7)),
        "NEPTUNE": GraphQLEnumValue((1.024e26, 2.4746e7)),
    }
コード例 #6
0
ファイル: graphql.py プロジェクト: iv2500/pystacker
             resolve=resolve_force_worker)
     }),
 subscription=GraphQLObjectType(
     name="RootSubscriptionType",
     fields={
         "runCmd":
         GraphQLField(GraphQLString,
                      args={
                          'id':
                          GraphQLArgument(GraphQLNonNull(GraphQLInt)),
                          'cmd':
                          GraphQLArgument(
                              GraphQLEnumType(
                                  "Cmd",
                                  values={
                                      'up': GraphQLEnumValue('up'),
                                      'down': GraphQLEnumValue('down'),
                                      'pause': GraphQLEnumValue('pause'),
                                      'unpause':
                                      GraphQLEnumValue('unpause'),
                                      'logs': GraphQLEnumValue('logs'),
                                      'destroy': GraphQLEnumValue('destroy')
                                  }))
                      },
                      resolve=lambda x, i, id, cmd: x,
                      subscribe=subscribe_run_cmd),
         "execServiceCmd":
         GraphQLField(GraphQLString,
                      args={
                          'stack_id':
                          GraphQLArgument(GraphQLNonNull(GraphQLInt)),
コード例 #7
0

def get_droid(root, _info, id):
    """Allows us to query for the droid with the given id."""
    return droid_data.get(id)


def get_secret_backstory(_character, _info):
    """Raise an error when attempting to get the secret backstory."""
    raise RuntimeError("secretBackstory is secret.")


episode_enum = GraphQLEnumType(
    "Episode",
    {
        "NEWHOPE": GraphQLEnumValue(4, description="Released in 1977."),
        "EMPIRE": GraphQLEnumValue(5, description="Released in 1980."),
        "JEDI": GraphQLEnumValue(6, description="Released in 1983."),
    },
    description="One of the films in the Star Wars Trilogy",
)


character_interface = GraphQLInterfaceType(
    "Character",
    lambda: {
        "id": GraphQLField(
            GraphQLNonNull(GraphQLString), description="The id of the character."
        ),
        "name": GraphQLField(GraphQLString, description="The name of the character."),
        "friends": GraphQLField(