Esempio n. 1
0
    },
)

InboxType = GraphQLObjectType(
    "Inbox",
    {
        "total": GraphQLField(
            GraphQLInt, resolve=lambda inbox, _info: len(inbox["emails"])
        ),
        "unread": GraphQLField(
            GraphQLInt,
            resolve=lambda inbox, _info: sum(
                1 for email in inbox["emails"] if email["unread"]
            ),
        ),
        "emails": GraphQLField(GraphQLList(EmailType)),
    },
)

QueryType = GraphQLObjectType("Query", {"inbox": GraphQLField(InboxType)})

EmailEventType = GraphQLObjectType(
    "EmailEvent", {"email": GraphQLField(EmailType), "inbox": GraphQLField(InboxType)}
)


async def anext(iterable):
    """Return the next item from an async iterator."""
    return await iterable.__anext__()

 def returns_true_for_a_list_wrapped_type():
     assert is_list_type(GraphQLList(ObjectType)) is True
     assert_list_type(GraphQLList(ObjectType))
 def returns_false_for_a_wrapped_input_type():
     _assert_non_output_type(GraphQLList(InputObjectType))
     _assert_non_output_type(GraphQLNonNull(InputObjectType))
 def returns_false_for_wrapped_interface_type():
     assert is_interface_type(GraphQLList(InterfaceType)) is False
     with raises(TypeError):
         assert_interface_type(GraphQLList(InterfaceType))
 def returns_false_for_wrapped_enum_type():
     assert is_enum_type(GraphQLList(EnumType)) is False
     with raises(TypeError):
         assert_enum_type(GraphQLList(EnumType))
Esempio n. 6
0
 def unwraps_wrapper_types():
     assert get_named_type(GraphQLNonNull(ObjectType)) is ObjectType
     assert get_named_type(GraphQLList(ObjectType)) is ObjectType
 def returns_false_for_wrapped_scalar():
     assert is_scalar_type(GraphQLList(ScalarType)) is False
     with raises(TypeError):
         assert_scalar_type(GraphQLList(ScalarType))
Esempio n. 8
0
#     secretBackstory: String

human_type: GraphQLObjectType
droid_type: GraphQLObjectType

character_interface: GraphQLInterfaceType = 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),
            description="The friends of the character,"
            " or an empty list if they have none.",
        ),
        "appearsIn":
        GraphQLField(GraphQLList(episode_enum),
                     description="Which movies they appear in."),
        "secretBackstory":
        GraphQLField(GraphQLString,
                     description="All secrets about their past."),
    },
    resolve_type=lambda character, _info, _type: {
        "Human": human_type,
        "Droid": droid_type,
    }[character.type],
    description="A character in the Star Wars Trilogy",
Esempio n. 9
0
def test_synchronous_error_nulls_out_error_subtrees():
    # type: () -> None
    ast = parse("""
    {
        sync
        syncError
        syncReturnError
        syncReturnErrorList
        asyncBasic
        asyncReject
        asyncEmptyReject
        asyncReturnError
    }
    """)

    class Data:
        def sync(self):
            # type: () -> str
            return "sync"

        def syncError(self):
            # type: () -> NoReturn
            raise Exception("Error getting syncError")

        def syncReturnError(self):
            # type: () -> Exception
            return Exception("Error getting syncReturnError")

        def syncReturnErrorList(self):
            # type: () -> List[Union[Exception, str]]
            return [
                "sync0",
                Exception("Error getting syncReturnErrorList1"),
                "sync2",
                Exception("Error getting syncReturnErrorList3"),
            ]

        def asyncBasic(self):
            # type: () -> Promise
            return resolved("async")

        def asyncReject(self):
            # type: () -> Promise
            return rejected(Exception("Error getting asyncReject"))

        def asyncEmptyReject(self):
            # type: () -> Promise
            return rejected(Exception("An unknown error occurred."))

        def asyncReturnError(self):
            # type: () -> Promise
            return resolved(Exception("Error getting asyncReturnError"))

    schema = GraphQLSchema(query=GraphQLObjectType(
        name="Type",
        fields={
            "sync": GraphQLField(GraphQLString),
            "syncError": GraphQLField(GraphQLString),
            "syncReturnError": GraphQLField(GraphQLString),
            "syncReturnErrorList": GraphQLField(GraphQLList(GraphQLString)),
            "asyncBasic": GraphQLField(GraphQLString),
            "asyncReject": GraphQLField(GraphQLString),
            "asyncEmptyReject": GraphQLField(GraphQLString),
            "asyncReturnError": GraphQLField(GraphQLString),
        },
    ))

    def sort_key(item):
        # type: (Dict[str, Any]) -> Tuple[int, int]
        locations = item["locations"][0]
        return (locations["line"], locations["column"])

    def handle_results(result):
        # type: (ExecutionResult) -> None
        assert result.data == {
            "asyncBasic": "async",
            "asyncEmptyReject": None,
            "asyncReject": None,
            "asyncReturnError": None,
            "sync": "sync",
            "syncError": None,
            "syncReturnError": None,
            "syncReturnErrorList": ["sync0", None, "sync2", None],
        }
        assert sorted(list(map(
            format_error, result.errors)), key=sort_key) == sorted(
                [
                    {
                        "locations": [{
                            "line": 4,
                            "column": 9
                        }],
                        "path": ["syncError"],
                        "message": "Error getting syncError",
                    },
                    {
                        "locations": [{
                            "line": 5,
                            "column": 9
                        }],
                        "path": ["syncReturnError"],
                        "message": "Error getting syncReturnError",
                    },
                    {
                        "locations": [{
                            "line": 6,
                            "column": 9
                        }],
                        "path": ["syncReturnErrorList", 1],
                        "message": "Error getting syncReturnErrorList1",
                    },
                    {
                        "locations": [{
                            "line": 6,
                            "column": 9
                        }],
                        "path": ["syncReturnErrorList", 3],
                        "message": "Error getting syncReturnErrorList3",
                    },
                    {
                        "locations": [{
                            "line": 8,
                            "column": 9
                        }],
                        "path": ["asyncReject"],
                        "message": "Error getting asyncReject",
                    },
                    {
                        "locations": [{
                            "line": 9,
                            "column": 9
                        }],
                        "path": ["asyncEmptyReject"],
                        "message": "An unknown error occurred.",
                    },
                    {
                        "locations": [{
                            "line": 10,
                            "column": 9
                        }],
                        "path": ["asyncReturnError"],
                        "message": "Error getting asyncReturnError",
                    },
                ],
                key=sort_key,
            )

    handle_results(execute(schema, ast, Data(), executor=ThreadExecutor()))
 def converts_list_singletons():
     assert ast_from_value(
         'FOO', GraphQLList(GraphQLString)) == StringValueNode(value='FOO')
SomeInterfaceType = GraphQLInterfaceType(
    name="SomeInterface",
    fields=lambda: {
        "name": GraphQLField(GraphQLString),
        "some": GraphQLField(SomeInterfaceType),
    },
)

FooType = GraphQLObjectType(
    name="Foo",
    interfaces=[SomeInterfaceType],
    fields=lambda: {
        "name": GraphQLField(GraphQLString),
        "some": GraphQLField(SomeInterfaceType),
        "tree": GraphQLField(GraphQLNonNull(GraphQLList(FooType))),
    },
)

BarType = GraphQLObjectType(
    name="Bar",
    interfaces=[SomeInterfaceType],
    fields=lambda: {
        "name": GraphQLField(GraphQLString),
        "some": GraphQLField(SomeInterfaceType),
        "foo": GraphQLField(FooType),
    },
)

BizType = GraphQLObjectType(
    name="Biz", fields=lambda: {"fizz": GraphQLField(GraphQLString)})
Esempio n. 12
0
    async def resolve_is_type_of_in_parallel():
        FooType = GraphQLInterfaceType("Foo",
                                       {"foo": GraphQLField(GraphQLString)})

        barrier = Barrier(4)

        async def is_type_of_bar(obj, *_args):
            await barrier.wait()
            return obj["foo"] == "bar"

        BarType = GraphQLObjectType(
            "Bar",
            {
                "foo": GraphQLField(GraphQLString),
                "foobar": GraphQLField(GraphQLInt)
            },
            interfaces=[FooType],
            is_type_of=is_type_of_bar,
        )

        async def is_type_of_baz(obj, *_args):
            await barrier.wait()
            return obj["foo"] == "baz"

        BazType = GraphQLObjectType(
            "Baz",
            {
                "foo": GraphQLField(GraphQLString),
                "foobaz": GraphQLField(GraphQLInt)
            },
            interfaces=[FooType],
            is_type_of=is_type_of_baz,
        )

        schema = GraphQLSchema(
            GraphQLObjectType(
                "Query",
                {
                    "foo":
                    GraphQLField(
                        GraphQLList(FooType),
                        resolve=lambda *_args: [
                            {
                                "foo": "bar",
                                "foobar": 1
                            },
                            {
                                "foo": "baz",
                                "foobaz": 2
                            },
                        ],
                    )
                },
            ),
            types=[BarType, BazType],
        )

        ast = parse("""
            {
              foo {
                foo
                ... on Bar { foobar }
                ... on Baz { foobaz }
              }
            }
            """)

        # raises TimeoutError if not parallel
        result = await asyncio.wait_for(execute(schema, ast), 1.0)

        assert result == (
            {
                "foo": [{
                    "foo": "bar",
                    "foobar": 1
                }, {
                    "foo": "baz",
                    "foobaz": 2
                }]
            },
            None,
        )
    def define_sample_schema():
        BlogImage = GraphQLObjectType(
            "Image",
            {
                "url": GraphQLField(GraphQLString),
                "width": GraphQLField(GraphQLInt),
                "height": GraphQLField(GraphQLInt),
            },
        )

        BlogArticle: GraphQLObjectType

        BlogAuthor = GraphQLObjectType(
            "Author",
            lambda: {
                "id": GraphQLField(GraphQLString),
                "name": GraphQLField(GraphQLString),
                "pic": GraphQLField(
                    BlogImage,
                    args={
                        "width": GraphQLArgument(GraphQLInt),
                        "height": GraphQLArgument(GraphQLInt),
                    },
                ),
                "recentArticle": GraphQLField(BlogArticle),
            },
        )

        BlogArticle = GraphQLObjectType(
            "Article",
            lambda: {
                "id": GraphQLField(GraphQLString),
                "isPublished": GraphQLField(GraphQLBoolean),
                "author": GraphQLField(BlogAuthor),
                "title": GraphQLField(GraphQLString),
                "body": GraphQLField(GraphQLString),
            },
        )

        BlogQuery = GraphQLObjectType(
            "Query",
            {
                "article": GraphQLField(
                    BlogArticle, args={"id": GraphQLArgument(GraphQLString)}
                ),
                "feed": GraphQLField(GraphQLList(BlogArticle)),
            },
        )

        BlogMutation = GraphQLObjectType(
            "Mutation", {"writeArticle": GraphQLField(BlogArticle)}
        )

        BlogSubscription = GraphQLObjectType(
            "Subscription",
            {
                "articleSubscribe": GraphQLField(
                    args={"id": GraphQLArgument(GraphQLString)}, type_=BlogArticle
                )
            },
        )

        schema = GraphQLSchema(BlogQuery, BlogMutation, BlogSubscription)

        kwargs = schema.to_kwargs()
        types = kwargs.pop("types")
        assert types == list(schema.type_map.values())
        assert kwargs == {
            "query": BlogQuery,
            "mutation": BlogMutation,
            "subscription": BlogSubscription,
            "directives": specified_directives,
            "ast_node": None,
            "extensions": None,
            "extension_ast_nodes": None,
            "assume_valid": False,
        }

        assert print_schema(schema) == dedent(
            """
            type Article {
              id: String
              isPublished: Boolean
              author: Author
              title: String
              body: String
            }

            type Author {
              id: String
              name: String
              pic(width: Int, height: Int): Image
              recentArticle: Article
            }

            type Image {
              url: String
              width: Int
              height: Int
            }

            type Mutation {
              writeArticle: Article
            }

            type Query {
              article(id: String): Article
              feed: [Article]
            }

            type Subscription {
              articleSubscribe(id: String): Article
            }
            """
        )
    def describe_return_types_must_be_unambiguous():

        SomeBox = GraphQLInterfaceType(
            "SomeBox",
            lambda: {
                "deepBox": GraphQLField(SomeBox),
                "unrelatedField": GraphQLField(GraphQLString),
            },
        )

        StringBox = GraphQLObjectType(
            "StringBox",
            lambda: {
                "scalar": GraphQLField(GraphQLString),
                "deepBox": GraphQLField(StringBox),
                "unrelatedField": GraphQLField(GraphQLString),
                "listStringBox": GraphQLField(GraphQLList(StringBox)),
                "stringBox": GraphQLField(StringBox),
                "intBox": GraphQLField(IntBox),
            },
            interfaces=[SomeBox],
        )

        IntBox = GraphQLObjectType(
            "IntBox",
            lambda: {
                "scalar": GraphQLField(GraphQLInt),
                "deepBox": GraphQLField(IntBox),
                "unrelatedField": GraphQLField(GraphQLString),
                "listStringBox": GraphQLField(GraphQLList(StringBox)),
                "stringBox": GraphQLField(StringBox),
                "intBox": GraphQLField(IntBox),
            },
            interfaces=[SomeBox],
        )

        NonNullStringBox1 = GraphQLInterfaceType(
            "NonNullStringBox1",
            {"scalar": GraphQLField(GraphQLNonNull(GraphQLString))})

        NonNullStringBox1Impl = GraphQLObjectType(
            "NonNullStringBox1Impl",
            {
                "scalar": GraphQLField(GraphQLNonNull(GraphQLString)),
                "deepBox": GraphQLField(StringBox),
                "unrelatedField": GraphQLField(GraphQLString),
            },
            interfaces=[SomeBox, NonNullStringBox1],
        )

        NonNullStringBox2 = GraphQLInterfaceType(
            "NonNullStringBox2",
            {"scalar": GraphQLField(GraphQLNonNull(GraphQLString))})

        NonNullStringBox2Impl = GraphQLObjectType(
            "NonNullStringBox2Impl",
            {
                "scalar": GraphQLField(GraphQLNonNull(GraphQLString)),
                "unrelatedField": GraphQLField(GraphQLString),
                "deepBox": GraphQLField(StringBox),
            },
            interfaces=[SomeBox, NonNullStringBox2],
        )

        Connection = GraphQLObjectType(
            "Connection",
            {
                "edges":
                GraphQLField(
                    GraphQLList(
                        GraphQLObjectType(
                            "Edge",
                            {
                                "node":
                                GraphQLField(
                                    GraphQLObjectType(
                                        "Node",
                                        {
                                            "id": GraphQLField(GraphQLID),
                                            "name":
                                            GraphQLField(GraphQLString),
                                        },
                                    ))
                            },
                        )))
            },
        )

        schema = GraphQLSchema(
            GraphQLObjectType(
                "QueryRoot",
                {
                    "someBox": GraphQLField(SomeBox),
                    "connection": GraphQLField(Connection),
                },
            ),
            types=[
                IntBox, StringBox, NonNullStringBox1Impl, NonNullStringBox2Impl
            ],
        )

        def conflicting_return_types_which_potentially_overlap():
            # This is invalid since an object could potentially be both the
            # Object type IntBox and the interface type NonNullStringBox1.
            # While that condition does not exist in the current schema, the
            # schema could expand in the future to allow this.
            expect_fails_rule_with_schema(
                schema,
                OverlappingFieldsCanBeMergedRule,
                """
                    {
                      someBox {
                        ...on IntBox {
                          scalar
                        }
                        ...on NonNullStringBox1 {
                          scalar
                        }
                      }
                    }
                    """,
                [{
                    "message":
                    fields_conflict_message(
                        "scalar",
                        "they return conflicting types Int and String!"),
                    "locations": [(5, 27), (8, 27)],
                }],
            )

        def compatible_return_shapes_on_different_return_types():
            # In this case `deepBox` returns `SomeBox` in the first usage, and
            # `StringBox` in the second usage. These types are not the same!
            # However this is valid because the return *shapes* are compatible.
            expect_passes_rule_with_schema(
                schema,
                OverlappingFieldsCanBeMergedRule,
                """
                      {
                        someBox {
                            ... on SomeBox {
                            deepBox {
                              unrelatedField
                            }
                          }
                          ... on StringBox {
                            deepBox {
                              unrelatedField
                            }
                          }
                        }
                      }
                      """,
            )

        def disallows_differing_return_types_despite_no_overlap():
            expect_fails_rule_with_schema(
                schema,
                OverlappingFieldsCanBeMergedRule,
                """
                    {
                      someBox {
                        ... on IntBox {
                          scalar
                        }
                        ... on StringBox {
                          scalar
                        }
                      }
                    }
                    """,
                [{
                    "message":
                    fields_conflict_message(
                        "scalar",
                        "they return conflicting types Int and String"),
                    "locations": [(5, 27), (8, 27)],
                }],
            )

        def reports_correctly_when_a_non_exclusive_follows_an_exclusive():
            expect_fails_rule_with_schema(
                schema,
                OverlappingFieldsCanBeMergedRule,
                """
                    {
                      someBox {
                        ... on IntBox {
                          deepBox {
                            ...X
                          }
                        }
                      }
                      someBox {
                        ... on StringBox {
                          deepBox {
                            ...Y
                          }
                        }
                      }
                      memoed: someBox {
                        ... on IntBox {
                          deepBox {
                            ...X
                          }
                        }
                      }
                      memoed: someBox {
                        ... on StringBox {
                          deepBox {
                            ...Y
                          }
                        }
                      }
                      other: someBox {
                        ...X
                      }
                      other: someBox {
                        ...Y
                      }
                    }
                    fragment X on SomeBox {
                      scalar
                    }
                    fragment Y on SomeBox {
                      scalar: unrelatedField
                    }
                    """,
                [{
                    "message":
                    fields_conflict_message(
                        "other",
                        [(
                            "scalar",
                            "scalar and unrelatedField are different fields",
                        )],
                    ),
                    "locations": [(31, 23), (39, 23), (34, 23), (42, 23)],
                    "path":
                    None,
                }],
            )

        def disallows_differing_return_type_nullability_despite_no_overlap():
            expect_fails_rule_with_schema(
                schema,
                OverlappingFieldsCanBeMergedRule,
                """
                    {
                      someBox {
                        ... on NonNullStringBox1 {
                          scalar
                        }
                        ... on StringBox {
                          scalar
                        }
                      }
                    }
                    """,
                [{
                    "message":
                    fields_conflict_message(
                        "scalar",
                        "they return conflicting types String! and String"),
                    "locations": [(5, 27), (8, 27)],
                }],
            )

        def disallows_differing_return_type_list_despite_no_overlap_1():
            expect_fails_rule_with_schema(
                schema,
                OverlappingFieldsCanBeMergedRule,
                """
                    {
                      someBox {
                        ... on IntBox {
                          box: listStringBox {
                            scalar
                          }
                        }
                        ... on StringBox {
                          box: stringBox {
                            scalar
                          }
                        }
                      }
                    }
                    """,
                [{
                    "message":
                    fields_conflict_message(
                        "box",
                        "they return conflicting types"
                        " [StringBox] and StringBox",
                    ),
                    "locations": [(5, 27), (10, 27)],
                }],
            )

            expect_fails_rule_with_schema(
                schema,
                OverlappingFieldsCanBeMergedRule,
                """
                    {
                      someBox {
                        ... on IntBox {
                          box: stringBox {
                            scalar
                          }
                        }
                        ... on StringBox {
                          box: listStringBox {
                            scalar
                          }
                        }
                      }
                    }
                    """,
                [{
                    "message":
                    fields_conflict_message(
                        "box",
                        "they return conflicting types"
                        " StringBox and [StringBox]",
                    ),
                    "locations": [(5, 27), (10, 27)],
                }],
            )

        def disallows_differing_subfields():
            expect_fails_rule_with_schema(
                schema,
                OverlappingFieldsCanBeMergedRule,
                """
                    {
                      someBox {
                        ... on IntBox {
                          box: stringBox {
                            val: scalar
                            val: unrelatedField
                          }
                        }
                        ... on StringBox {
                          box: stringBox {
                            val: scalar
                          }
                        }
                      }
                    }
                    """,
                [{
                    "message":
                    fields_conflict_message(
                        "val",
                        "scalar and unrelatedField are different fields"),
                    "locations": [(6, 29), (7, 29)],
                }],
            )

        def disallows_differing_deep_return_types_despite_no_overlap():
            expect_fails_rule_with_schema(
                schema,
                OverlappingFieldsCanBeMergedRule,
                """
                    {
                      someBox {
                        ... on IntBox {
                          box: stringBox {
                            scalar
                          }
                        }
                        ... on StringBox {
                          box: intBox {
                            scalar
                          }
                        }
                      }
                    }
                    """,
                [{
                    "message":
                    fields_conflict_message(
                        "box",
                        [(
                            "scalar",
                            "they return conflicting types String and Int",
                        )],
                    ),
                    "locations": [(5, 27), (6, 29), (10, 27), (11, 29)],
                    "path":
                    None,
                }],
            )

        def allows_non_conflicting_overlapping_types():
            expect_passes_rule_with_schema(
                schema,
                OverlappingFieldsCanBeMergedRule,
                """
                    {
                      someBox {
                        ... on IntBox {
                          scalar: unrelatedField
                        }
                        ... on StringBox {
                          scalar
                        }
                      }
                    }
                    """,
            )

        def same_wrapped_scalar_return_types():
            expect_passes_rule_with_schema(
                schema,
                OverlappingFieldsCanBeMergedRule,
                """
                    {
                      someBox {
                        ...on NonNullStringBox1 {
                          scalar
                        }
                        ...on NonNullStringBox2 {
                          scalar
                        }
                      }
                    }
                    """,
            )

        def allows_inline_typeless_fragments():
            expect_passes_rule_with_schema(
                schema,
                OverlappingFieldsCanBeMergedRule,
                """
                    {
                      a
                      ... {
                        a
                      }
                    }
                    """,
            )

        def compares_deep_types_including_list():
            expect_fails_rule_with_schema(
                schema,
                OverlappingFieldsCanBeMergedRule,
                """
                    {
                      connection {
                        ...edgeID
                        edges {
                          node {
                            id: name
                          }
                        }
                      }
                    }

                    fragment edgeID on Connection {
                      edges {
                        node {
                          id
                        }
                      }
                    }
                    """,
                [{
                    "message":
                    fields_conflict_message(
                        "edges",
                        [("node", [("id", "name and id are different fields")])
                         ],
                    ),
                    "locations": [
                        (5, 25),
                        (6, 27),
                        (7, 29),
                        (14, 23),
                        (15, 25),
                        (16, 27),
                    ],
                    "path":
                    None,
                }],
            )

        def ignores_unknown_types():
            expect_passes_rule_with_schema(
                schema,
                OverlappingFieldsCanBeMergedRule,
                """
                    {
                      someBox {
                        ...on UnknownType {
                          scalar
                        }
                        ...on NonNullStringBox2 {
                          scalar
                        }
                      }
                    }
                    """,
            )

        def error_message_contains_hint_for_alias_conflict():
            # The error template should end with a hint for the user to try
            # using different aliases.
            error = fields_conflict_message("x",
                                            "a and b are different fields")
            assert error == (
                "Fields 'x' conflict because a and b are different fields."
                " Use different aliases on the fields to fetch both"
                " if this was intentional.")

        def works_for_field_names_that_are_js_keywords():
            FooType = GraphQLObjectType(
                "Foo", {"constructor": GraphQLField(GraphQLString)})

            schema_with_keywords = GraphQLSchema(
                GraphQLObjectType("query",
                                  lambda: {"foo": GraphQLField(FooType)}))

            expect_passes_rule_with_schema(
                schema_with_keywords,
                OverlappingFieldsCanBeMergedRule,
                """
                    {
                      foo {
                        constructor
                      }
                    }
                    """,
            )

        def works_for_field_names_that_are_python_keywords():
            FooType = GraphQLObjectType("Foo",
                                        {"class": GraphQLField(GraphQLString)})

            schema_with_keywords = GraphQLSchema(
                GraphQLObjectType("query",
                                  lambda: {"foo": GraphQLField(FooType)}))

            expect_passes_rule_with_schema(
                schema_with_keywords,
                OverlappingFieldsCanBeMergedRule,
                """
                    {
                      foo {
                        class
                      }
                    }
                    """,
            )
Esempio n. 15
0
 def returns_true_for_list_of_non_null_types():
     assert is_nullable_type(GraphQLList(
         GraphQLNonNull(ObjectType))) is True
     assert_nullable_type(GraphQLList(GraphQLNonNull(ObjectType)))
Esempio n. 16
0
def test_executes_arbitary_code():
    # type: () -> None
    class Data(object):
        a = "Apple"
        b = "Banana"
        c = "Cookie"
        d = "Donut"
        e = "Egg"

        @property
        def f(self):
            # type: () -> Promise
            return resolved("Fish")

        def pic(self, size=50):
            # type: (int) -> Promise
            return resolved("Pic of size: {}".format(size))

        def deep(self):
            # type: () -> DeepData
            return DeepData()

        def promise(self):
            # type: () -> Promise
            return resolved(Data())

    class DeepData(object):
        a = "Already Been Done"
        b = "Boring"
        c = ["Contrived", None, resolved("Confusing")]

        def deeper(self):
            # type: () -> List[Union[None, Data, Promise]]
            return [Data(), None, resolved(Data())]

    ast = parse("""
        query Example($size: Int) {
            a,
            b,
            x: c
            ...c
            f
            ...on DataType {
                pic(size: $size)
                promise {
                    a
                }
            }
            deep {
                a
                b
                c
                deeper {
                    a
                    b
                }
            }
        }
        fragment c on DataType {
            d
            e
        }
    """)

    expected = {
        "a": "Apple",
        "b": "Banana",
        "x": "Cookie",
        "d": "Donut",
        "e": "Egg",
        "f": "Fish",
        "pic": "Pic of size: 100",
        "promise": {
            "a": "Apple"
        },
        "deep": {
            "a":
            "Already Been Done",
            "b":
            "Boring",
            "c": ["Contrived", None, "Confusing"],
            "deeper": [
                {
                    "a": "Apple",
                    "b": "Banana"
                },
                None,
                {
                    "a": "Apple",
                    "b": "Banana"
                },
            ],
        },
    }

    DataType = GraphQLObjectType(
        "DataType",
        lambda: {
            "a":
            GraphQLField(GraphQLString),
            "b":
            GraphQLField(GraphQLString),
            "c":
            GraphQLField(GraphQLString),
            "d":
            GraphQLField(GraphQLString),
            "e":
            GraphQLField(GraphQLString),
            "f":
            GraphQLField(GraphQLString),
            "pic":
            GraphQLField(
                args={"size": GraphQLArgument(GraphQLInt)},
                type=GraphQLString,
                resolver=lambda obj, info, **args: obj.pic(args["size"]),
            ),
            "deep":
            GraphQLField(DeepDataType),
            "promise":
            GraphQLField(DataType),
        },
    )

    DeepDataType = GraphQLObjectType(
        "DeepDataType",
        {
            "a": GraphQLField(GraphQLString),
            "b": GraphQLField(GraphQLString),
            "c": GraphQLField(GraphQLList(GraphQLString)),
            "deeper": GraphQLField(GraphQLList(DataType)),
        },
    )

    schema = GraphQLSchema(query=DataType)

    def handle_result(result):
        # type: (ExecutionResult) -> None
        assert not result.errors
        assert result.data == expected

    handle_result(
        execute(
            schema,
            ast,
            Data(),
            variable_values={"size": 100},
            operation_name="Example",
            executor=ThreadExecutor(),
        ))
    handle_result(
        execute(schema,
                ast,
                Data(),
                variable_values={"size": 100},
                operation_name="Example"))
Esempio n. 17
0
 def returns_self_for_a_nullable_type():
     assert get_nullable_type(ObjectType) is ObjectType
     list_of_obj = GraphQLList(ObjectType)
     assert get_nullable_type(list_of_obj) is list_of_obj
Esempio n. 18
0
    name="ComplexScalar",
    serialize=lambda value: "SerializedValue" if value == "DeserializedValue" else None,
    parse_value=lambda value: "DeserializedValue"
    if value == "SerializedValue"
    else None,
    parse_literal=lambda ast, _variables=None: "DeserializedValue"
    if ast.value == "SerializedValue"
    else None,
)


TestInputObject = GraphQLInputObjectType(
    "TestInputObject",
    {
        "a": GraphQLInputField(GraphQLString),
        "b": GraphQLInputField(GraphQLList(GraphQLString)),
        "c": GraphQLInputField(GraphQLNonNull(GraphQLString)),
        "d": GraphQLInputField(TestComplexScalar),
    },
)


TestNestedInputObject = GraphQLInputObjectType(
    "TestNestedInputObject",
    {
        "na": GraphQLInputField(GraphQLNonNull(TestInputObject)),
        "nb": GraphQLInputField(GraphQLNonNull(GraphQLString)),
    },
)

Esempio n. 19
0
 def unwraps_deeply_wrapper_types():
     assert (get_named_type(
         GraphQLNonNull(GraphQLList(GraphQLNonNull(ObjectType)))) is
             ObjectType)
Esempio n. 20
0
 def returns_true_for_a_non_list_wrapped_type():
     assert is_list_type(GraphQLNonNull(
         GraphQLList(ObjectType))) is False
     with raises(TypeError):
         assert_list_type(GraphQLNonNull(GraphQLList(ObjectType)))
 def returns_false_for_wrapped_specified_scalar():
     assert is_scalar_type(GraphQLList(GraphQLString)) is False
Esempio n. 22
0
 def returns_true_for_a_wrapped_input_type():
     assert is_input_type(GraphQLList(InputObjectType)) is True
     assert_input_type(GraphQLList(InputObjectType))
     assert is_input_type(GraphQLNonNull(InputObjectType)) is True
     assert_input_type(GraphQLNonNull(InputObjectType))
 def returns_false_for_wrapped_union_type():
     assert is_union_type(GraphQLList(UnionType)) is False
     with raises(TypeError):
         assert_union_type(GraphQLList(UnionType))
Esempio n. 24
0
 def returns_true_for_a_wrapped_output_type():
     assert is_output_type(GraphQLList(ObjectType)) is True
     assert_output_type(GraphQLList(ObjectType))
     assert is_output_type(GraphQLNonNull(ObjectType)) is True
     assert_output_type(GraphQLNonNull(ObjectType))
 def returns_false_for_wrapped_input_object_type():
     assert is_input_object_type(GraphQLList(InputObjectType)) is False
     with raises(TypeError):
         assert_input_object_type(GraphQLList(InputObjectType))
Esempio n. 26
0
 def returns_false_for_wrapped_non_composite_type():
     assert is_composite_type(GraphQLList(InputObjectType)) is False
     with raises(TypeError):
         assert_composite_type(GraphQLList(InputObjectType))
 def returns_false_for_a_not_non_null_wrapped_type():
     assert is_non_null_type(GraphQLList(
         GraphQLNonNull(ObjectType))) is False
     with raises(TypeError):
         assert_non_null_type(GraphQLList(GraphQLNonNull(ObjectType)))
Esempio n. 28
0
 def returns_true_for_list_and_non_null_types():
     assert is_wrapping_type(GraphQLList(ObjectType)) is True
     assert_wrapping_type(GraphQLList(ObjectType))
     assert is_wrapping_type(GraphQLNonNull(ObjectType)) is True
     assert_wrapping_type(GraphQLNonNull(ObjectType))
 def returns_false_for_wrapped_non_leaf_type():
     assert is_leaf_type(GraphQLList(ObjectType)) is False
     with raises(TypeError):
         assert_leaf_type(GraphQLList(ObjectType))
def test_introspects_on_input_object():
    TestInputObject = GraphQLInputObjectType(
        "TestInputObject",
        OrderedDict(
            [
                ("a", GraphQLInputObjectField(GraphQLString, default_value="foo")),
                ("b", GraphQLInputObjectField(GraphQLList(GraphQLString))),
            ]
        ),
    )
    TestType = GraphQLObjectType(
        "TestType",
        {
            "field": GraphQLField(
                type_=GraphQLString,
                args={"complex": GraphQLArgument(TestInputObject)},
                resolver=lambda obj, info, **args: json.dumps(args.get("complex")),
            )
        },
    )
    schema = GraphQLSchema(TestType)
    request = """
      {
        __schema {
          types {
            kind
            name
            inputFields {
              name
              type { ...TypeRef }
              defaultValue
            }
          }
        }
      }
      fragment TypeRef on __Type {
        kind
        name
        ofType {
          kind
          name
          ofType {
            kind
            name
            ofType {
              kind
              name
            }
          }
        }
      }
    """
    result = graphql(schema, request)
    assert not result.errors
    assert {
        "kind": "INPUT_OBJECT",
        "name": "TestInputObject",
        "inputFields": [
            {
                "name": "a",
                "type": {"kind": "SCALAR", "name": "String", "ofType": None},
                "defaultValue": '"foo"',
            },
            {
                "name": "b",
                "type": {
                    "kind": "LIST",
                    "name": None,
                    "ofType": {"kind": "SCALAR", "name": "String", "ofType": None},
                },
                "defaultValue": None,
            },
        ],
    } in result.data["__schema"]["types"]