[Django]-Exposing GraphQL based APIs

3👍

Django could be a (good but heavier) solution, but here is simpler solution using Flask:

from flask import Flask, jsonify

app = Flask(__name__)

class Job:
    def __init__(self, name, key, starttime, endtime):
        self.name = name 
        self.key = key
        self.starttime = starttime
        self.endtime = endtime

@app.route("/get", methods=['GET'])
def get_jobs(starttime, endtime):
    ''' Reads and returns jobs that ran between starttime and endtime interval '''
    jobs = read_data(starttime, endtime) # your read_data() method
    return jsonify({'jobs': jobs})

@app.route("/put", methods=['POST'])   # or methods=['PUT']
def put_job(request):

    # access your data trough the request object:
    job_name = request.args.get('name', '')
    job_key = request.args.get('key', '')

    # or get it in json
    job_data = request.json

    write_data(Job.from_json(job_data))

I used Json here because I’m more confortable with it, but if GraphQL is important for you, I recommend to you the Graphene-Python library.

There is also a project of integration of Graphene with Flask

Leave a comment