所以我使用 Apollo 和 Azure Functions 完成了这项工作。 apollo-server-azure-functions 的示例中有一个错误,并且该包装库中的一个小错误返回字符串而不是 JSON 数据。还需要单独安装graphql-tools。
在示例代码中,模式对象已创建,但未添加到传递给 Apollo 服务器的参数中。工作代码如下。我刚刚将架构添加到传递的选项中。
const server = require("apollo-server-azure-functions");
const graphqlTools = require("graphql-tools");
const typeDefs = `
type Random {
id: Int!
rand: String
}
type Query {
rands: [Random]
rand(id: Int!): Random
}
`;
const rands = [{ id: 1, rand: "random" }, { id: 2, rand: "modnar" }];
const resolvers = {
Query: {
rands: () => rands,
rand: (_, { id }) => rands.find(rand => rand.id === id)
}
};
const schema = graphqlTools.makeExecutableSchema({
typeDefs,
resolvers
});
module.exports = function run(context, req) {
if (req.method === 'POST') {
server.graphqlAzureFunctions({
endpointURL: '/api/graphql',
schema: schema
})(context, req);
} else if (req.method === 'GET') {
return server.graphiqlAzureFunctions({
endpointURL: '/api/graphql',
schema: schema
})(context, req);
}
};
只需进行此更改,您将开始从端点取回数据,但不幸的是它不会是 application/json 类型。为此,需要对apollo-server-azure-functions 进行小改动,以将正文从字符串转换为 JSON。我已经为此提交了 PR,但不确定他们什么时候会收到。
如果您没有耐心,您可以使用下面的代码创建自己的包装函数,该函数将与上面的示例一起使用,并返回 JSON 而不是字符串。
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var apollo_server_core_1 = require("apollo-server-core");
var GraphiQL = require("apollo-server-module-graphiql");
function graphqlAzureFunctions(options) {
if (!options) {
throw new Error('Apollo Server requires options.');
}
if (arguments.length > 1) {
throw new Error("Apollo Server expects exactly one argument, got " + arguments.length);
}
return function (httpContext, request) {
var queryRequest = {
method: request.method,
options: options,
query: request.method === 'POST' ? request.body : request.query,
};
if (queryRequest.query && typeof queryRequest.query === 'string') {
queryRequest.query = JSON.parse(queryRequest.query);
}
return apollo_server_core_1.runHttpQuery([httpContext, request], queryRequest)
.then(function (gqlResponse) {
var result = {
status: 200,
headers: { 'Content-Type': 'application/json' },
body: JSON.parse(gqlResponse),
};
httpContext.res = result;
httpContext.done(null, result);
})
.catch(function (error) {
var result = {
status: error.statusCode,
headers: error.headers,
body: error.message,
};
httpContext.res = result;
httpContext.done(null, result);
});
};
}
exports.graphqlAzureFunctions = graphqlAzureFunctions;
function graphiqlAzureFunctions(options) {
return function (httpContext, request) {
var query = request.query;
GraphiQL.resolveGraphiQLString(query, options, httpContext, request).then(function (graphiqlString) {
httpContext.res = {
status: 200,
headers: {
'Content-Type': 'text/html',
},
body: graphiqlString,
};
httpContext.done(null, httpContext.res);
}, function (error) {
httpContext.res = {
status: 500,
body: error.message,
};
httpContext.done(null, httpContext.res);
});
};
}
exports.graphiqlAzureFunctions = graphiqlAzureFunctions;
为此,您需要通过 npm 安装 apollo-server-core 和 apollo-server-module-grapiql 作为依赖项。