Example #1
0
def test_graphql_union_eq():
    union = GraphQLUnionType(name="Name",
                             gql_types=["First", "Second"],
                             description="description")

    ## Same
    assert union == union
    assert union == GraphQLUnionType(name="Name",
                                     gql_types=["First", "Second"],
                                     description="description")
    # Currently we ignore the description in comparing
    assert union == GraphQLUnionType(name="Name",
                                     gql_types=["First", "Second"])

    ## Different
    assert union != GraphQLUnionType(
        name="Name",
        gql_types=[
            "Second",
            "First",
            # We changed the order of types
        ],
    )

    assert union != GraphQLUnionType(name="OtherName",
                                     gql_types=["First", "Second"])
Example #2
0
def test_graphql_union_repr():
    union = GraphQLUnionType(name="Name",
                             gql_types=["First", "Second"],
                             description="description")

    assert (union.__repr__() == "GraphQLUnionType(name='Name', "
            "gql_types=['First', 'Second'], "
            "description='description')")
    assert union == eval(repr(union))
 def union_type_definition(self, tree: Tree) -> GraphQLUnionType:
     # TODO: Add directives
     description = None
     ast_node = None  # TODO: Should we discard it or keep it ?
     name = None
     members = None
     for child in tree.children:
         if child.type == 'description':
             description = child.value
         elif child.type == 'UNION':
             ast_node = child
         elif child.type == 'IDENT':
             name = String(str(child.value), ast_node=child)
         elif child.type == 'union_members':
             members = child.value
         elif child.type == 'discard':
             pass
         else:
             raise UnexpectedASTNode(
                 "Unexpected AST node `{}`, type `{}`".format(
                     child, child.__class__.__name__))
     return GraphQLUnionType(
         name=name,
         gql_types=members,
         description=description,
     )
 def union_type_definition(self, tree: Tree) -> GraphQLUnionType:
     description = None
     name = None
     members = None
     directives = None
     for child in tree.children:
         if child.type == "description":
             description = child.value
         elif child.type == "IDENT":
             name = child.value
         elif child.type == "union_members":
             members = child.value
         elif child.type == "directives":
             directives = child.value
         elif child.type == "discard" or child.type == "UNION":
             pass
         else:
             raise UnexpectedASTNode(
                 "Unexpected AST node `{}`, type `{}`".format(
                     child, child.__class__.__name__))
     return GraphQLUnionType(
         name=name,
         gql_types=members,
         description=description,
         schema=self._schema,
         directives=directives,
     )
Example #5
0
def parse_union_type_definition(
    union_type_definition_node: "UnionTypeDefinitionNode",
    schema: "GraphQLSchema",
) -> Optional["GraphQLUnionType"]:
    """
    Computes an AST union type definition node into a GraphQLUnionType
    instance.
    :param union_type_definition_node: AST union type definition node to treat
    :param schema: the GraphQLSchema instance linked to the engine
    :type union_type_definition_node: UnionTypeDefinitionNode
    :type schema: GraphQLSchema
    :return: the GraphQLUnionType instance
    :rtype: Optional[GraphQLUnionType]
    """
    if not union_type_definition_node:
        return None

    union_type = GraphQLUnionType(
        name=parse_name(union_type_definition_node.name, schema),
        description=parse_name(union_type_definition_node.description, schema),
        types=parse_union_member_types(union_type_definition_node.types,
                                       schema),
        directives=union_type_definition_node.directives,
    )
    schema.add_type_definition(union_type)
    return union_type
Example #6
0
def test_graphql_union_init():
    union = GraphQLUnionType(name="Name",
                             gql_types=["First", "Second"],
                             description="description")

    assert union.name == "Name"
    assert union.gql_types == ["First", "Second"]
    assert union.description == "description"
Example #7
0
async def test_build_schema(monkeypatch, clean_registry):
    from tartiflette.resolver.factory import ResolverExecutorFactory
    from tartiflette.engine import _import_builtins

    resolver_excutor = Mock()
    monkeypatch.setattr(
        ResolverExecutorFactory,
        "get_resolver_executor",
        Mock(return_value=resolver_excutor),
    )

    from tartiflette.schema.bakery import SchemaBakery
    from tartiflette.types.argument import GraphQLArgument
    from tartiflette.types.field import GraphQLField
    from tartiflette.types.input_object import GraphQLInputObjectType
    from tartiflette.types.interface import GraphQLInterfaceType
    from tartiflette.types.list import GraphQLList
    from tartiflette.types.non_null import GraphQLNonNull
    from tartiflette.types.object import GraphQLObjectType
    from tartiflette.types.union import GraphQLUnionType

    schema_sdl = """
    schema @enable_cache {
        query: RootQuery
        mutation: RootMutation
        subscription: RootSubscription
    }

    union Group = Foo | Bar | Baz

    interface Something {
        oneField: [Int]
        anotherField: [String]
        aLastOne: [[Date!]]!
    }

    input UserInfo {
        name: String
        dateOfBirth: [Date]
        graphQLFan: Boolean!
    }

    # directive @partner(goo: Anything) on ENUM_VALUE

    \"\"\"
    This is a docstring for the Test Object Type.
    \"\"\"
    type Test implements Unknown & Empty{
        \"\"\"
        This is a field description :D
        \"\"\"
        field(input: InputObject): String!
        anotherField: [Int]
        fieldWithDefaultValueArg(test: String = "default"): ID
        simpleField: Date
    }
    """
    _, schema_sdl = await _import_builtins([], schema_sdl, "G")
    _, schema_E = await _import_builtins([], "", "E")
    clean_registry.register_sdl("G", schema_sdl)
    clean_registry.register_sdl("E", schema_E)
    generated_schema = SchemaBakery._preheat("G")
    expected_schema = SchemaBakery._preheat("E")
    expected_schema.query_type = "RootQuery"
    expected_schema.mutation_type = "RootMutation"
    expected_schema.subscription_type = "RootSubscription"

    expected_schema.add_definition(
        GraphQLUnionType(name="Group", gql_types=["Foo", "Bar", "Baz"]))
    expected_schema.add_definition(
        GraphQLInterfaceType(
            name="Something",
            fields=OrderedDict(
                oneField=GraphQLField(name="oneField",
                                      gql_type=GraphQLList(gql_type="Int")),
                anotherField=GraphQLField(
                    name="anotherField",
                    gql_type=GraphQLList(gql_type="String"),
                ),
                aLastOne=GraphQLField(
                    name="aLastOne",
                    gql_type=GraphQLNonNull(gql_type=GraphQLList(
                        gql_type=GraphQLList(gql_type=GraphQLNonNull(
                            gql_type="Date")))),
                ),
            ),
        ))
    expected_schema.add_definition(
        GraphQLInputObjectType(
            name="UserInfo",
            fields=OrderedDict([
                ("name", GraphQLArgument(name="name", gql_type="String")),
                (
                    "dateOfBirth",
                    GraphQLArgument(
                        name="dateOfBirth",
                        gql_type=GraphQLList(gql_type="Date"),
                    ),
                ),
                (
                    "graphQLFan",
                    GraphQLArgument(
                        name="graphQLFan",
                        gql_type=GraphQLNonNull(gql_type="Boolean"),
                    ),
                ),
            ]),
        ))
    expected_schema.add_definition(
        GraphQLObjectType(
            name="Test",
            fields=OrderedDict([
                (
                    "field",
                    GraphQLField(
                        name="field",
                        gql_type=GraphQLNonNull(gql_type="String"),
                        arguments=OrderedDict(input=GraphQLArgument(
                            name="input", gql_type="InputObject")),
                    ),
                ),
                (
                    "anotherField",
                    GraphQLField(
                        name="anotherField",
                        gql_type=GraphQLList(gql_type="Int"),
                    ),
                ),
                (
                    "fieldWithDefaultValueArg",
                    GraphQLField(
                        name="fieldWithDefaultValueArg",
                        gql_type="ID",
                        arguments=OrderedDict(test=GraphQLArgument(
                            name="test",
                            gql_type="String",
                            default_value="default",
                        )),
                    ),
                ),
                (
                    "simpleField",
                    GraphQLField(name="simpleField", gql_type="Date"),
                ),
            ]),
            interfaces=["Unknown", "Empty"],
        ))

    assert 5 < len(generated_schema._gql_types)
    assert len(expected_schema._gql_types) == len(generated_schema._gql_types)

    monkeypatch.undo()
Example #8
0
def test_build_schema():
    schema_sdl = """
    schema @enable_cache {
        query: RootQuery
        mutation: RootMutation
        subscription: RootSubscription
    }
    
    scalar Date
    
    union Group = Foo | Bar | Baz
    
    interface Something {
        oneField: [Int]
        anotherField: [String]
        aLastOne: [[Date!]]!
    }
    
    input UserInfo {
        name: String
        dateOfBirth: [Date]
        graphQLFan: Boolean!
    }
    
    # directive @partner(goo: Anything) on ENUM_VALUE
    
    \"\"\"
    This is a docstring for the Test Object Type.
    \"\"\"
    type Test implements Unknown & Empty @enable_cache {
        \"\"\"
        This is a field description :D
        \"\"\"
        field(input: InputObject): String! @deprecated(reason: "Useless field")
        anotherField: [Int] @something(
            lst: ["about" "stuff"]
            obj: {some: [4, 8, 16], complex: {about: 19.4}, another: EnumVal}
        )
        fieldWithDefaultValueArg(test: String = "default"): ID
        simpleField: Date
    }
    """

    generated_schema = build_graphql_schema_from_sdl(schema_sdl,
                                                     schema=GraphQLSchema())

    expected_schema = GraphQLSchema()
    expected_schema.query_type = "RootQuery"
    expected_schema.mutation_type = "RootMutation"
    expected_schema.subscription_type = "RootSubscription"

    expected_schema.add_definition(GraphQLScalarType(name="Date"))
    expected_schema.add_definition(GraphQLUnionType(name="Group", gql_types=[
        "Foo",
        "Bar",
        "Baz",
    ]))
    expected_schema.add_definition(GraphQLInterfaceType(name="Something", fields=OrderedDict(
        oneField=GraphQLField(name="oneField", gql_type=GraphQLList(gql_type="Int")),
        anotherField=GraphQLField(name="anotherField", gql_type=GraphQLList(gql_type="String")),
        aLastOne=GraphQLField(name="aLastOne", gql_type=GraphQLNonNull(gql_type=GraphQLList(gql_type=GraphQLList(gql_type=GraphQLNonNull(gql_type="Date"))))),
    )))
    expected_schema.add_definition(GraphQLInputObjectType(name="UserInfo", fields=OrderedDict([
        ('name', GraphQLArgument(name="name", gql_type="String")),
        ('dateOfBirth', GraphQLArgument(name="dateOfBirth", gql_type=GraphQLList(gql_type="Date"))),
        ('graphQLFan', GraphQLArgument(name="graphQLFan", gql_type=GraphQLNonNull(gql_type="Boolean"))),
    ])))
    expected_schema.add_definition(GraphQLObjectType(name="Test", fields=OrderedDict([
        ('field', GraphQLField(name="field", gql_type=GraphQLNonNull(gql_type="String"), arguments=OrderedDict(
            input=GraphQLArgument(name="input", gql_type="InputObject"),
        ))),
        ('anotherField', GraphQLField(name="anotherField", gql_type=GraphQLList(gql_type="Int"))),
        ('fieldWithDefaultValueArg', GraphQLField(name="fieldWithDefaultValueArg", gql_type="ID", arguments=OrderedDict(
            test=GraphQLArgument(name="test", gql_type="String", default_value="default"),
        ))),
        ('simpleField', GraphQLField(name="simpleField", gql_type="Date")),
    ]),
                                                               interfaces=[
                                                                   "Unknown",
                                                                   "Empty",
                                                               ]))

    assert expected_schema == generated_schema
    assert len(generated_schema._gql_types) > 5
    assert len(generated_schema._gql_types) == len(expected_schema._gql_types)
    # Only the default types should be in the schema
    assert len(DefaultGraphQLSchema._gql_types) == 5
                               "very convenient\n        "),
         ])
     ],
 ),
 (
     """
     \"\"\"
     DateOrTime scalar for storing Date or Time, very convenient also
     \"\"\"
     union DateOrTime = Date | Time
     """,
     [
         Tree('type_definition', [
             GraphQLUnionType(name="DateOrTime",
                              gql_types=["Date", "Time"],
                              description="\n        DateOrTime scalar for "
                              "storing Date or Time, very "
                              "convenient also\n        "),
         ])
     ],
 ),
 (
     """
     scalar Date
     union DateOrTime = Date | Time
     """,
     [
         Tree('type_definition', [
             GraphQLScalarType(name="Date"),
         ]),
         Tree('type_definition', [