Using same connection with multiple resolvers in graphene

643 Views Asked by At

I have a code like this,

# SWAMI KARUPPASWAMI THUNNAI

import jwt
import graphene
from flask import request
from auth.helper import medease_token
from database.get_connection import get_connection
from flask_graphql import GraphQLView


class CredentialInformation(graphene.ObjectType):
    """
    graphene object type to get the personal information about the user
    """

    country_code = graphene.String()
    phone = graphene.String()
    verified = graphene.Int()

    @medease_token
    def resolve_country_code(self, root):
        customer_token = request.headers["x-access-token"]
        decoded_token = jwt.decode(customer_token, verify=False)
        customer_id = decoded_token["customer_id"]
        try:
            connection = get_connection()
            cursor = connection.cursor()
            cursor.execute("select country_code from customer_credential where id=%s limit 1", (customer_id, ))
            result = cursor.fetchone()
            return result["country_code"]
        finally:
            cursor.close()
            connection.close()

    @medease_token
    def resolve_phone(self, root):
        customer_token = request.headers["x-access-token"]
        decoded_token = jwt.decode(customer_token, verify=False)
        customer_id = decoded_token["customer_id"]
        try:
            connection = get_connection()
            cursor = connection.cursor()
            cursor.execute("select phone from customer_credential where id=%s limit 1", (customer_id, ))
            result = cursor.fetchone()
            return result["phone"]
        finally:
            cursor.close()
            connection.close()

    @medease_token
    def resolve_verified(self, root):
        customer_token = request.headers["x-access-token"]
        decoded_token = jwt.decode(customer_token, verify=False)
        customer_id = decoded_token["customer_id"]
        try:
            connection = get_connection()
            cursor = connection.cursor()
            cursor.execute("select verified from customer_credential where id=%s limit 1", (customer_id,))
            result = cursor.fetchone()
            return result["verified"]
        finally:
            cursor.close()
            connection.close()


def credential_information_wrapper():
    return GraphQLView.as_view("graphql", schema=graphene.Schema(query=CredentialInformation))

which uses flask-graphql and graphene for Python graphql. The code works absolutely fine but I think I am missing something here because I need to open new connections in every resolver, and I need to write same query again so there is a data duplication, so is this the right way of doing or am I missing something?

Any help would be highly appreciated! Thank you in advance.

1

There are 1 best solutions below

0
On

Opening a new connection on every query (resolver in this case) is okay. You could also set a connection pool to minimize the cost of opening connections.

Just curious, what database adapter or ORM are you using? Would be great if you could refactor the connection and cursor steps into a single function to call on every resolver and only have to pass the SQL query string.