GraphQL query returns null : flask-graphene

225 Views Asked by At

I'm trying to expose spark aggregation results using a graphql api. The query returns null everytime though,

from flask import Flask, request
from graphene import ObjectType, Schema, String, Int, Field, Float, List
from flask_graphql import GraphQL
import json
from pyspark import SparkContext, SparkConf
from pyspark.sql import SQLContext
import pyspark.sql.functions as func
from pyspark.sql.functions import col

app = Flask(__name__)

conf = SparkConf().setAppName('csv viewer')
sc = SparkContext(conf=conf)
sqlContext = SQLContext(sc)
csv_file = '/path/to/file'
df = sqlContext.read.format('com.databricks.spark.csv').options(header='true',inferschema='true').load(csv_file)

class Aggr(ObjectType):
    country = String()
    percentile = Int()


class Query(ObjectType):
    aggr = List(of_type= Aggr)

    def resolve_aggr(self,info):
        df1 = df.where(col('country') != 'null')
        result = df1.groupby('country').agg(
            func.expr('percentile_approx(responseTime, array(0.95))')[0].alias('percentile')).collect()
        return result

schema = Schema(query = Query)


@app.route("/spark", methods = ['POST'])
def aggr():
    print(request.data) 
    data = json.loads(request.data)
    print(json.loads(request.data)['query'])
    return json.dumps(schema.execute(data["query"]).data)

The response I get is null when I send a POST request with body containing {"query": "{aggr}"} on postman. I'm very new to GraphQL and graphene, any idea where I'm going wrong here?

0

There are 0 best solutions below