Beispiel #1
0
def setup_person_query(tmp_path):
    conn = sqlite.connect(str(tmp_path / "data.db"))
    name = Field("name", fieldtype=str, nullable=False)
    age = Field("age", fieldtype=int, nullable=False)
    person = GraphQLType([name, age], name="person")
    ctx = GraphQLContext([person], conn=conn)
    parser = GraphQLParser(ctx)

    def _setup_fn(*args):
        if args:
            return ctx, parser.parse(args[0])
        else:
            return (
                ctx,
                parser.parse("""
    {
        person(id: 1){
            name
            age
        }

        person(id: 2) {
            age
        }
    }
    """),
            )

    return _setup_fn
Beispiel #2
0
def test_field_validate_function_typeerror():
    """ Field object should raise a TypeError when validating if wrong type is passed to a function """
    def my_func(num: int) -> int:
        return num

    with pytest.raises(TypeError) as exc_info:
        my_field = Field("my_field", func=my_func)
        assert not my_field.validate({"num": "not an int"})
Beispiel #3
0
def test_field_validate_function_null_argument():
    """ Field object should raise an exception when validating a function with a missing argument """
    def my_func(name_no_default: str, num: int = 1) -> str:
        return f"{name_no_default} is #{num}!"

    my_field = Field("my_field", func=my_func)
    assert not my_field.args[0].default
    with pytest.raises(MissingRequiredArgument) as exc_info:
        assert not my_field.validate({"num": 1})
Beispiel #4
0
def test_field_infer_function():
    """ Field object should infer arguments, default values, and return types from type annotations """
    def my_func(name: str = "Simone", num: int = 1) -> str:
        return f"{name} is #{num}!"

    my_field = Field("my_field", func=my_func)
    assert my_field.is_function()
    assert my_field.args[0].type is str
    assert my_field.args[0].default == "Simone"
    assert my_field.args[1].type is int
    assert my_field.args[1].default == 1
Beispiel #5
0
def setup_person_pet_query(tmp_path):
    conn = sqlite.connect(str(tmp_path / "data.db"))
    person_name = Field("name", fieldtype=str, nullable=False)
    person_age = Field("age", fieldtype=int, nullable=False)
    person = GraphQLType([person_name, person_age], name="person")

    pet_name = Field("name", fieldtype=str, nullable=False)
    pet_species = Field("species", fieldtype=str, nullable=False)
    pet_owner = Field("owner", fieldtype=person, nullable=False)
    pet = GraphQLType([pet_name, pet_species, pet_owner], name="pet")

    ctx = GraphQLContext([person, pet], conn=conn)
    parser = GraphQLParser(ctx)

    cursor = conn.cursor()
    cursor.execute("INSERT INTO person VALUES (1, 'Aubrey', 21);")
    cursor.execute("INSERT INTO pet VALUES (1, 'Annabelle', 'cat', 1);")
    conn.commit()

    def _setup_fn(*args):
        if args:
            return ctx, parser.parse(args[0])
        else:
            return (
                ctx,
                parser.parse("""
    {
        pet(id: 1){
            name
            species
            owner {
                name
                age
            }
        }
    }
    """),
            )

    return _setup_fn
Beispiel #6
0
def test_type_validate_null_field():
    """ GraphQLType should raise an exception when validating if a non-nullable field is missing """
    my_field = Field("my_field", fieldtype=int, nullable=False)
    my_type = GraphQLType([my_field])
    with pytest.raises(MissingRequiredField) as exc_info:
        assert not my_type.validate({})
Beispiel #7
0
def test_field_validate_function_not_implemented():
    """ Field object should raise an exception when validating an undefined function """
    with pytest.raises(MethodNotImplemented) as exc_info:
        args = (Argument("name", str), Argument("num", int))
        my_field = Field("my_field", args=args, returntype=str)
        assert not my_field.validate({"name": "Simone", "num": 1})
Beispiel #8
0
def test_field_validate_typeerror():
    """ Field object should raise a TypeError when validating with wrong type """
    with pytest.raises(TypeError) as exc_info:
        my_field = Field("my_field", fieldtype=int)
        assert not my_field.validate("not an int")
Beispiel #9
0
import sqlite3
import json
from flask import Flask, request
from graphi.schema import GraphQLType, Field
from graphi.query import GraphQLContext

app = Flask(__name__)
conn = sqlite3.connect("data.db")

# Define GraphQL types
person_name = Field("name", fieldtype=str, nullable=False)
person_age = Field("age", fieldtype=int, nullable=False)
person = GraphQLType([person_name, person_age], name="person")
pet_name = Field("name", fieldtype=str, nullable=False)
pet_species = Field("species", fieldtype=str, nullable=False)
pet_owner = Field("owner", fieldtype=person, nullable=False)
pet = GraphQLType([pet_name, pet_species, pet_owner], name="pet")

graphql = GraphQLContext([person, pet], conn=conn)


@app.route("/", methods=["POST"])
def execute_graphql_command():
    conn = sqlite3.connect("data.db")
    command = request.data.decode()
    result = graphql.execute(conn, command)
    return json.dumps(result)


if __name__ == "__main__":
    app.run(debug=True)