nepito / ideas

0 stars 0 forks source link

FastAPI #7

Open nepito opened 1 year ago

nepito commented 1 year ago
from fastapi import FastAPI
from graphene import ObjectType, String, Schema

class Query(ObjectType):
    hello = String(description='Returns Hello World')

    def resolve_hello(self, info):
        return "Hello World"

app = FastAPI()

@app.post("/graphql")
def graphql_view(request):
    schema = Schema(query=Query)
    result = schema.execute(request.json["query"])
    return {"data": result.data}

Y para las pruebas

import json
from fastapi.testclient import TestClient

def test_graphql_view(app):
    client = TestClient(app)

    # Prepare the query and variables
    query = """
        query {
            hello
        }
    """
    variables = {}
    data = {"query": query, "variables": variables}
    headers = {"content-type": "application/json"}

    # Send the request
    response = client.post("/graphql", data=json.dumps(data), headers=headers)

    # Check the response
    assert response.status_code == 200
    assert response.json() == {"data": {"hello": "Hello World"}}
nepito commented 1 year ago

¿Valdrá la pena pagar por esto?