Ejemplo n.º 1
0
def test_graphql_argument_repr():
    arg = GraphQLArgument(name="Name", gql_type="Test",
                          default_value=42, description="description")

    assert arg.__repr__() == "GraphQLArgument(name='Name', gql_type='Test', " \
                             "default_value=42, description='description')"
    assert arg == eval(repr(arg))
Ejemplo n.º 2
0
def test_graphql_input_object_init():
    input_object = GraphQLInputObjectType(name="Name",
                                          fields=OrderedDict([
                                               ("test", GraphQLArgument(name="arg", gql_type="Int")),
                                               ("another", GraphQLArgument(name="arg", gql_type="String")),
                                           ]),
                                           description="description")

    assert input_object.name == "Name"
    assert input_object.fields == OrderedDict([
        ("test", GraphQLArgument(name="arg", gql_type="Int")),
        ("another", GraphQLArgument(name="arg", gql_type="String")),
    ])
    assert input_object.description == "description"
Ejemplo n.º 3
0
def test_graphql_input_object_eq():
    input_object = GraphQLInputObjectType(
        name="Name",
        fields=OrderedDict([
            ("test", GraphQLArgument(name="arg", gql_type="Int")),
            ("another", GraphQLArgument(name="arg", gql_type="String")),
        ]),
        description="description",
    )

    ## Same
    assert input_object == input_object
    assert input_object == GraphQLInputObjectType(
        name="Name",
        fields=OrderedDict([
            ("test", GraphQLArgument(name="arg", gql_type="Int")),
            ("another", GraphQLArgument(name="arg", gql_type="String")),
        ]),
        description="description",
    )
    # Currently we ignore the description in comparing
    assert input_object == GraphQLInputObjectType(
        name="Name",
        fields=OrderedDict([
            ("test", GraphQLArgument(name="arg", gql_type="Int")),
            ("another", GraphQLArgument(name="arg", gql_type="String")),
        ]),
    )

    ## Different
    assert input_object != GraphQLInputObjectType(
        name="Name",
        fields=OrderedDict([
            ("another", GraphQLArgument(name="arg", gql_type="String")),
            ("test", GraphQLArgument(name="arg", gql_type="Int")),
            # We reversed the order of arguments
        ]),
    )
    assert input_object != GraphQLInputObjectType(name="Name",
                                                  fields=OrderedDict())
    assert input_object != GraphQLInputObjectType(
        name="OtherName",
        fields=OrderedDict([
            ("another", GraphQLArgument(name="arg", gql_type="String")),
            ("test", GraphQLArgument(name="arg", gql_type="Int")),
            # We reversed the order of arguments
        ]),
    )
Ejemplo n.º 4
0
 def input_value_definition(self, tree: Tree):
     # TODO: Add directives
     description = None
     name = None
     gql_type = None
     default_value = None
     for child in tree.children:
         if child.type == 'description':
             description = child.value
         elif child.type == 'IDENT':
             name = String(str(child.value), ast_node=child)
         elif child.type == 'type':
             gql_type = child.value
         elif child.type == 'value':
             default_value = child.value
         elif child.type == 'discard':
             pass
         else:
             raise UnexpectedASTNode(
                 "Unexpected AST node `{}`, type `{}`".format(
                     child, child.__class__.__name__))
     return GraphQLArgument(
         name=name,
         gql_type=gql_type,
         default_value=default_value,
         description=description,
     )
Ejemplo n.º 5
0
 def input_value_definition(self, tree: Tree) -> GraphQLArgument:
     # TODO: Add directives
     description = None
     name = None
     gql_type = None
     default_value = 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 == "type":
             gql_type = child.value
         elif child.type == "value":
             default_value = child.value
         elif child.type == "discard":
             pass
         elif child.type == "directives":
             directives = child.value
         else:
             raise UnexpectedASTNode(
                 "Unexpected AST node `{}`, type `{}`".format(
                     child, child.__class__.__name__))
     return GraphQLArgument(
         name=name,
         gql_type=gql_type,
         default_value=default_value,
         description=description,
         directives=directives,
     )
Ejemplo n.º 6
0
def parse_input_value_definition(
    input_value_definition_node: "InputValueDefinitionNode",
    schema: "GraphQLSchema",
    as_argument_definition: bool = False,
) -> Optional[Union["GraphQLArgument", "GraphQLInputField"]]:
    """
    Computes an AST input value definition node into a GraphQLArgument or
    GraphQLInputField instance.
    :param input_value_definition_node: AST input value definition node to
    treat
    :param schema: the GraphQLSchema instance linked to the engine
    :param as_argument_definition: determines whether or not the return type
    should be a GraphQLArgument or a GraphQLInputField
    :type input_value_definition_node: InputValueDefinitionNode
    :type schema: GraphQLSchema
    :type as_argument_definition: bool
    :return: the computed GraphQLArgument or GraphQLInputField instance
    :rtype: Optional[Union[GraphQLArgument, GraphQLInputField]]
    """
    if not input_value_definition_node:
        return None

    init_kwargs = {
        "name": parse_name(input_value_definition_node.name, schema),
        "description": parse_name(input_value_definition_node.description,
                                  schema),
        "gql_type": parse_type(input_value_definition_node.type, schema),
        "default_value": input_value_definition_node.default_value,
        "directives": input_value_definition_node.directives,
    }

    if not as_argument_definition:
        return GraphQLInputField(**init_kwargs)
    return GraphQLArgument(**init_kwargs,
                           definition=input_value_definition_node)
Ejemplo n.º 7
0
def test_graphql_input_object_repr():
    input_object = GraphQLInputObjectType(
        name="Name",
        fields=OrderedDict([
            ("test", GraphQLArgument(name="arg", gql_type="Int")),
            ("another", GraphQLArgument(name="arg", gql_type="String")),
        ]),
        description="description",
    )

    assert (
        input_object.__repr__() == "GraphQLInputObjectType(name='Name', "
        "fields=OrderedDict(["
        "('test', GraphQLArgument(name='arg', gql_type='Int', default_value=None, description=None, directives=None)), "
        "('another', GraphQLArgument(name='arg', gql_type='String', default_value=None, description=None, directives=None))"
        "]), description='description')")
    assert input_object == eval(repr(input_object))
Ejemplo n.º 8
0
def test_graphql_argument_init():
    arg = GraphQLArgument(name="Name", gql_type="Test",
                    default_value=42, description="description")

    assert arg.name == "Name"
    assert arg.gql_type == "Test"
    assert arg.default_value == 42
    assert arg.description == "description"
Ejemplo n.º 9
0
def test_graphql_argument_eq():
    arg = GraphQLArgument(
        name="Name",
        gql_type="Test",
        default_value=42,
        description="description",
    )

    ## Same
    assert arg == arg
    assert arg == GraphQLArgument(
        name="Name",
        gql_type="Test",
        default_value=42,
        description="description",
    )
    # Currently we ignore the description in comparing
    assert arg == GraphQLArgument(name="Name",
                                  gql_type="Test",
                                  default_value=42)
    ## Different
    assert arg != GraphQLArgument(
        name="Name", gql_type="Test", default_value=24)
    assert arg != GraphQLArgument(name="Name", gql_type="Test")
    assert arg != GraphQLArgument(
        name="Name", gql_type="NotTest", default_value=42)
    assert arg != GraphQLArgument(
        name="OtherName", gql_type="Test", default_value=42)
Ejemplo n.º 10
0
def prepare_type_root_field(schema: "GraphQLSchema") -> "GraphQLField":
    return GraphQLField(
        name="__type",
        description="Request the type information of a single type.",
        arguments={
            "name": GraphQLArgument(
                name="name",
                gql_type=GraphQLNonNull("String", schema=schema),
                schema=schema,
            )
        },
        gql_type="__Type",
        resolver=__type_resolver,
        schema=schema,
    )
Ejemplo n.º 11
0
def prepare_type_root_field(schema: "GraphQLSchema") -> "GraphQLField":
    """
    Factory to generate a `__type` field.
    :param schema: the GraphQLSchema instance linked to the engine
    :type schema: GraphQLSchema
    :return: the `__type` field
    :rtype: GraphQLField
    """
    return GraphQLField(
        name="__type",
        description="Request the type information of a single type.",
        arguments={
            "name":
            GraphQLArgument(name="name",
                            gql_type=GraphQLNonNull("String", schema=schema))
        },
        gql_type="__Type",
        resolver=__type_resolver,
    )
Ejemplo n.º 12
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()
Ejemplo n.º 13
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
         ])
     ],
 ),
 (
     """
     input UserProfileInput {
         fullName: String
     }
     """,
     [
         Tree('type_definition', [
             GraphQLInputObjectType(name="UserProfileInput",
                                    fields=OrderedDict([
                                        ("fullName",
                                         GraphQLArgument(
                                             name="fullName",
                                             gql_type="String",
                                         )),
                                    ])),
         ])
     ],
 ),
 (
     """
     type Car implements Vehicle {
         modelName: String
         speedInKmh: Float
     }
     """,
     [
         Tree('type_definition', [
             GraphQLObjectType(