Skip to content

Commit

Permalink
added graphql server
Browse files Browse the repository at this point in the history
  • Loading branch information
niklastheman committed Dec 20, 2024
1 parent 8e84607 commit 2710e6e
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
33 changes: 31 additions & 2 deletions fedn/network/api/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
from flask import Flask, jsonify, request

from fedn.common.config import get_controller_config
from fedn.network.api import gunicorn_app
from fedn.network.api.auth import jwt_auth_required
from fedn.network.api.interface import API
from fedn.network.api.shared import control, statestore
from fedn.network.api.v1 import _routes
from fedn.network.api import gunicorn_app
from fedn.network.api.v1.graphql.schema import schema

custom_url_prefix = os.environ.get("FEDN_CUSTOM_URL_PREFIX", False)
# statestore_config,modelstorage_config,network_id,control=set_statestore_config()
Expand All @@ -28,6 +29,32 @@ def health_check():
app.add_url_rule(f"{custom_url_prefix}/health", view_func=health_check, methods=["GET"])


@app.route("/api/v1/graphql", methods=["POST"])
def graphql_endpoint():
data = request.get_json()

if not data or "query" not in data:
return jsonify({"error": "Missing query in request"}), 400

# Execute the GraphQL query
result = schema.execute(
data["query"],
variables=data.get("variables"),
context_value={"request": request}, # Pass Flask request object as context if needed
)

# Format the result as a JSON response
response = {"data": result.data}
if result.errors:
response["errors"] = [str(error) for error in result.errors]

return jsonify(response)


if custom_url_prefix:
app.add_url_rule(f"{custom_url_prefix}/api/v1/graphql", view_func=graphql_endpoint, methods=["POST"])


@app.route("/get_model_trail", methods=["GET"])
@jwt_auth_required(role="admin")
def get_model_trail():
Expand Down Expand Up @@ -638,7 +665,9 @@ def start_server_api():
if debug:
app.run(debug=debug, port=port, host=host)
else:
workers=os.cpu_count()
workers = os.cpu_count()
gunicorn_app.run_gunicorn(app, host, port, workers)


if __name__ == "__main__":
start_server_api()
26 changes: 26 additions & 0 deletions fedn/network/api/v1/graphql/schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import graphene


class SessionType(graphene.ObjectType):
id = graphene.String()
name = graphene.String()


class Query(graphene.ObjectType):
session = graphene.List(
SessionType,
id=graphene.Int(),
name=graphene.String(),
)

def resolve_hello(root, info):
return "Hello, GraphQL!"

def resolve_session(root, info, id=None, name=None, pet_name=None):
return [
{"id": "1", "name": "Session 1"},
{"id": "2", "name": "Session 2"},
]


schema = graphene.Schema(query=Query)
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ dependencies = [
"plotly",
"virtualenv",
"tenacity!=8.4.0",
"graphene>=3.1"
]

[project.urls]
Expand Down

0 comments on commit 2710e6e

Please sign in to comment.