有没有办法通过azure函数和nodejs实现Graphql。例如,类似于 - https://www.npmjs.com/package/graphql-server-lambda
答案 0 :(得分:6)
Apollo为GraphQL提供Azure功能集成:
以下是github repo上提供的示例:
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, request) {
if (request.method === "POST") {
server.graphqlAzureFunctions({
endpointURL: '/api/graphql'
})(context, request);
} else if (request.method === "GET") {
return server.graphiqlAzureFunctions({
endpointURL: '/api/graphql'
})(context, request);
}
};
答案 1 :(得分:4)
所以我使用Apollo和Azure Functions进行了这项工作。 apollo-server-azure-functions
的示例中存在错误,并且该包装器库中的一个小错误返回字符串而不是JSON数据。您还需要单独安装graphql-tools
。
在示例代码中,创建了schema对象,但未将其添加到传递给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
作为依赖项。
答案 2 :(得分:3)
没有本机支持,但没有什么能阻止您创建调用Azure Functions的GraphQL架构。
但是,像scaphold这样的空间中有一些社区产品可用于将Azure Functions /无服务器提供程序与GraphQL集成:
答案 3 :(得分:0)
对于希望使用Typescript和Azure函数开发GraphQL API的任何人,您可以考虑以下入门回购:azure-function-graphql-typescript-starter(免责声明:我是作者)
它建立在以下基础之上: