Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
412 views
in Technique[技术] by (71.8m points)

python-3.x - 在石墨烯中使用具有多个解析器的相同连接(Using same connection with multiple resolvers in graphene)

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.

(它对Python graphql使用flask-graphql和graphene。)

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.

(先感谢您。)

  ask by VISWESWARAN NAGASIVAM translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...