简体   繁体   English

能否将 graphQL 设置为 Firebase 中的可调用函数(以及如何设置?)?

[英]Can graphQL be set up as a callable function in Firebase (and how?)?

Can graphQL be set up as a callable function in Firebase?可以将 graphQL 设置为 Firebase 中的可调用函数吗? All the examples in internet I was able to find set graphQL as onRequest HTTP function.互联网上的所有示例我都能找到将 graphQL 设置为onRequest HTTP 函数。 Would it be possible to use the onCall function for graphQL?是否可以将onCall函数用于 graphQL? How to do it?怎么做?

The reason I'd prefer to do it this way is authentication - callable functions offer the context object containing all relevant user data (or at least that's how I understand them), so you don't need to bother with handling the tokens.我更喜欢这样做的原因是身份验证 - 可调用函数提供包含所有相关用户数据的上下文对象(或者至少我是这样理解它们的),因此您无需费心处理令牌。

Here's a simple template of a graphQL server inside an onRequest function using express-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);
  • How to transform this template into onCall function?如何将此模板转换为onCall函数?
  • How to call the transformed function from the client using httpsCallable() method, so that the query/endpoint name and args are passed properly?如何使用httpsCallable()方法从客户端调用转换后的函数,以便正确传递查询/端点名称和args

What you can do in a "simple" HTTP Cloud Function can be done in a Callable Cloud Function .您可以在“简单” HTTP Cloud Function中执行的操作可以在Callable Cloud Function 中完成

Actually, Callable Cloud Functions are HTTP Cloud Functions with specific request and response formats, see the Protocol specification .实际上,Callable Cloud Functions 是具有特定请求和响应格式的 HTTP Cloud Functions,请参阅 协议规范

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM