I have created a Flask server in Python and used Ariadne to bind Python functions to Apollo GraphQL schema. I have followed this guide - Apollo GraphQL with Python
It's working as expected but I want to query only those fields in my underlying data source which the client requests from the GraphQL query. So, I need to access the requested query in the resolver function, which I am unable to figure out how.
This is my Flask /graphQL API:
import json
from api import app
from ariadne.constants import PLAYGROUND_HTML
from flask import request, jsonify
from ariadne import load_schema_from_path, make_executable_schema, snake_case_fallback_resolvers, graphql_sync, ObjectType
from queries import get_asset_resolver
query = ObjectType("Query")
query.set_field("getAsset", get_asset_resolver)
type_defs = load_schema_from_path("./schema.graphql")
schema = make_executable_schema(type_defs, query, snake_case_fallback_resolvers)
@app.route("/graphql", methods=["GET"])
def graphql_playground():
return PLAYGROUND_HTML, 200
@app.route("/graphql", methods=["POST"])
def graphql_server():
data = request.get_json()
print('GraphQL request: ', data)
success, result = graphql_sync(
schema,
data,
context_value=request,
debug=app.debug
)
print('Result from resolver: ', success, result)
status_code = 200 if success else 400
return jsonify(result), status_code
And this is my resolver function:
from ariadne import convert_kwargs_to_snake_case
import sqlquerybuilder
import runquery
@convert_kwargs_to_snake_case
def get_asset_resolver(obj, info, filters):
# I want to fetch the requested query attributes here
query = sqlquerybuilder.build_query_for_assets(filters)
assets = runquery.run_query_for_assets(query)
return assets