是否可以返回带有Google函数端点的graphql gui?

问题描述 投票:0回答:1

当我到达它的端点时,我想在Google Function中查看graphql gui。通常我会使用这样的东西:

app.add_url_rule(
    '/graphql',
    view_func=GraphQLView.as_view(
        'graphql',
        schema=schema,
        graphiql=True, # for having the GraphiQL interface
        context=None
    )
)

不确定这是否有可能,但想知道是否有人尝试过并取得了成功。

python flask graphql graphene-python
1个回答
0
投票

终于想通了。有点愚蠢,但是,它仍然有效!您必须在要部署的google函数中调用view函数。这使我可以托管无服务器的graphql api,非常简洁。

def gql(request):
    # Response Headers
    responseHeaders = {
        'Content-Type': 'application/json',
        'Access-Control-Allow-Origin': '*',
        'Access-Control-Allow-Methods': 'POST, OPTIONS',
        'Access-Control-Allow-Headers': 'Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, Origin, X-Requested-With',
    }

    # Return Options
    if request.method == 'OPTIONS':
        return {
          'statusCode': 200,
          'headers': responseHeaders,
          'body': ''
        }

    # Check Authorization Request
    person_auth_response = check_person_auth(request)

    # Return Graphql View/Results
    graphql_view_instance = GraphQLView(schema=schema, graphiql=True, get_context=lambda: person_auth_response)
    return graphql_view_instance.dispatch_request()



if __name__ == '__main__':
    app = Flask(__name__)
    CORS(app)
    app.debug = True
    app.route('/gql', methods=['POST', 'OPTIONS'])(lambda: gql(request))
    app.run()
© www.soinside.com 2019 - 2024. All rights reserved.