Использование одного и того же соединения с несколькими резольверами в графене - PullRequest
0 голосов
/ 01 декабря 2019

У меня есть такой код,

# 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))

, который использует flask-graphql и графен для Python graphql. Код работает абсолютно нормально, но я думаю, что здесь чего-то не хватает, потому что мне нужно открывать новые соединения в каждом распознавателе, и мне нужно снова написать один и тот же запрос, чтобы было дублирование данных, так что это правильный способ или ячто-то упустил?

Любая помощь будет высоко ценится! Заранее спасибо.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...