Пример #1
0
def test_graphql_scalar_eq():
    scalar = GraphQLScalarType(name="Name", description="description")

    ## Same
    assert scalar == scalar
    assert scalar == GraphQLScalarType(name="Name", description="description")
    # Currently we ignore the description in comparing
    assert scalar == GraphQLScalarType(name="Name")

    ## Different
    assert scalar != GraphQLScalarType(name="OtherName")
Пример #2
0
def test_graphql_scalar_init():
    scalar = GraphQLScalarType(name="Name", description="description")

    assert scalar.name == "Name"
    assert scalar.coerce_output is None
    assert scalar.coerce_input is None
    assert scalar.description == "description"
Пример #3
0
 def scalar_type_definition(self, tree: Tree) -> GraphQLScalarType:
     description = None
     name = 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 == "directives":
             directives = child.value
         elif child.type == "discard" or child.type == "SCALAR":
             pass
         else:
             raise UnexpectedASTNode(
                 "Unexpected AST node `{}`, type `{}`".format(
                     child, child.__class__.__name__))
     scalar = GraphQLScalarType(
         name=name,
         description=description,
         schema=self._schema,
         directives=directives,
     )
     self._schema.add_custom_scalar_definition(scalar)
     return scalar
Пример #4
0
def parse_scalar_type_definition(
    scalar_type_definition_node: "ScalarTypeDefinitionNode",
    schema: "GraphQLSchema",
) -> Optional["GraphQLScalarType"]:
    """
    Computes an AST scalar type definition node into a GraphQLScalarType
    instance.
    :param scalar_type_definition_node: AST scalar type definition node to
    treat
    :param schema: the GraphQLSchema instance linked to the engine
    :type scalar_type_definition_node: ScalarTypeDefinitionNode
    :type schema: GraphQLSchema
    :return: the computed GraphQLScalarType instance
    :rtype: Optional[GraphQLScalarType]
    """
    if not scalar_type_definition_node:
        return None

    scalar_type = GraphQLScalarType(
        name=parse_name(scalar_type_definition_node.name, schema),
        description=parse_name(scalar_type_definition_node.description,
                               schema),
        directives=scalar_type_definition_node.directives,
    )
    schema.add_scalar_definition(scalar_type)
    return scalar_type
 def scalar_type_definition(self, tree: Tree) -> GraphQLScalarType:
     # TODO: Add directives
     description = None
     ast_node = None  # TODO: Should we discard it or keep it ?
     name = None
     for child in tree.children:
         if child.type == 'description':
             description = child.value
         elif child.type == 'SCALAR':
             ast_node = child
         elif child.type == 'IDENT':
             name = String(str(child.value), ast_node=child)
         elif child.type == 'discard':
             pass
         else:
             raise UnexpectedASTNode(
                 "Unexpected AST node `{}`, type `{}`".format(
                     child, child.__class__.__name__))
     return GraphQLScalarType(
         name=name,
         description=description,
     )
Пример #6
0
def test_graphql_scalar_repr():
    scalar = GraphQLScalarType(name="Name", description="description")

    assert (scalar.__repr__() == "GraphQLScalarType(name='Name', "
            "description='description')")
    assert scalar == eval(repr(scalar))
Пример #7
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
from tartiflette.types.scalar import GraphQLScalarType
from tartiflette.types.union import GraphQLUnionType


@pytest.mark.parametrize("full_sdl,expected_tree", [
    (
        """
        \"\"\"
        Date scalar for storing Dates, very convenient
        \"\"\"
        scalar Date
        """,
        [
            Tree('type_definition', [
                GraphQLScalarType(name="Date",
                                  description="\n        Date scalar for "
                                  "storing Dates, "
                                  "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"],
Пример #9
0
from tartiflette.types.scalar import GraphQLScalarType

GraphQLBoolean = GraphQLScalarType(name="Boolean", coerce_output=bool, coerce_input=bool)

GraphQLFloat = GraphQLScalarType(name="Float", coerce_output=float, coerce_input=float)

GraphQLID = GraphQLScalarType(name="ID", coerce_output=str, coerce_input=str)

GraphQLInt = GraphQLScalarType(name="Int", coerce_output=int, coerce_input=int)

GraphQLString = GraphQLScalarType(name="String", coerce_output=str, coerce_input=str)