【问题标题】:Can graphQL be set up as a callable function in Firebase (and how?)?可以将 graphQL 设置为 Firebase 中的可调用函数(以及如何设置?)?
【发布时间】:2021-10-03 06:38:32
【问题描述】:

可以在 Firebase 中将 graphQL 设置为可调用函数吗?互联网上的所有示例我都能找到将 graphQL 设置为 onRequest HTTP 函数。是否可以将onCall 函数用于graphQL?怎么做?

我更喜欢这样做的原因是身份验证 - 可调用函数提供包含所有相关用户数据的上下文对象(或者至少我是这样理解的),因此您无需费心处理令牌。

这是一个使用 express-graphqlonRequest 函数内的 graphQL 服务器的简单模板:

const functions = require("firebase-functions");
const express = require('express');
const { graphqlHTTP } = require('express-graphql');
const {
    GraphQLObjectType,
    GraphQLString,
    GraphQLSchema,
    GraphQLID
} = require('graphql');

const app = express();

const RootQuery = new GraphQLObjectType({
    name: 'Query',
    fields: {
        tournament: {
            type: GraphQLString,
            args: {
                id: { type: GraphQLNonNull(GraphQLID) }
            },
            resolve(parentValue, args) {
                return 'Some result for id: ' + args.id
            }
        },
    }
})

const schema = new GraphQLSchema({
    query: RootQuery
})

app.use(
    '/',
    graphqlHTTP({
        schema, 
        rootValue: root, // contents not relevant to the question
        graphiql: true,
    })
);

exports.graphql = functions.https.onRequest(app);
  • 如何将此模板转化为onCall函数?
  • 如何使用httpsCallable()方法从客户端调用转换后的函数,以便正确传递查询/端点名称和args

【问题讨论】:

    标签: node.js firebase graphql google-cloud-functions


    【解决方案1】:

    您可以在“简单”HTTP Cloud Function 中完成的操作可以在 Callable Cloud Function 中完成。

    实际上,Callable Cloud Functions 是具有特定请求和响应格式的 HTTP Cloud Functions,请参阅Protocol specification

    【讨论】:

    • 好吧,那么究竟如何将 graphQL 包装在一个可调用函数中呢?
    • "如何准确地将 graphQL 包装在可调用函数中" => 您需要共享一些代码,因为这是一个相当广泛的问题。您是否尝试调整您在问题中提到的 HTTP Cloud Function 示例?
    • 我稍后会扩展我的问题。但这是一个关于最小模板的问题,而不是我的具体代码。我会将 onRequest 函数的模板 GQL 添加到我的问题中。
    • 我已经用模板代码扩展了我的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-15
    • 2021-10-23
    • 2018-11-15
    • 1970-01-01
    • 2010-12-19
    • 2020-08-25
    • 1970-01-01
    相关资源
    最近更新 更多