【发布时间】:2021-10-03 06:38:32
【问题描述】:
可以在 Firebase 中将 graphQL 设置为可调用函数吗?互联网上的所有示例我都能找到将 graphQL 设置为 onRequest HTTP 函数。是否可以将onCall 函数用于graphQL?怎么做?
我更喜欢这样做的原因是身份验证 - 可调用函数提供包含所有相关用户数据的上下文对象(或者至少我是这样理解的),因此您无需费心处理令牌。
这是一个使用 express-graphql 的 onRequest 函数内的 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