【问题标题】:Graphql with Azure functions带有 Azure 函数的 Graphql
【发布时间】:2020-11-02 09:09:29
【问题描述】:

有没有办法通过 azure 函数和 nodejs 实现 Graphql。例如,像 - https://www.npmjs.com/package/graphql-server-lambda

【问题讨论】:

标签: azure graphql azure-functions


【解决方案1】:

Apollo 为 GraphQL 提供 Azure 函数集成:

apollo-server-azure-functions

这是他们的 github 存储库中提供的示例:

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);
  }
};

【讨论】:

  • 我最近在 Apollo 网站上发现了这个。我创建了一个 Azure 函数,将代码粘贴进去,但不知道如何真正让它工作。文档中没有 nothing 解释了如何使其真正发挥作用。如果有一篇博文说明如何设置,我很乐意阅读!
  • 我猜只是创建一个 azure 函数并将代码粘贴到编辑器中
  • 恐怕只是将代码粘贴到 Azure 函数中是行不通的。我错过了一些东西。
  • 你是否在 packages.config 文件中添加了 npm 包?
【解决方案2】:

所以我使用 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-coreapollo-server-module-grapiql 作为依赖项。

【讨论】:

  • 您有可以分享的示例项目吗?还是一个好的教程?
【解决方案3】:

没有对此的本机支持,但没有什么可以阻止您创建调用 Azure Functions 的 GraphQL 架构。

但是,该领域有一些社区产品,例如 scaphold,它们可以将 Azure Functions/无服务器提供程序与 GraphQL 集成:

https://docs.scaphold.io/custom-logic/

【讨论】:

  • 链接已修复。
【解决方案4】:

对于希望使用 Typescript 和 Azure Functions 开发 GraphQL API 的任何人,您可以考虑使用以下初始存储库:azure-function-graphql-typescript-starter免责声明:我是作者

它建立在:

【讨论】:

    猜你喜欢
    • 2021-10-18
    • 2020-06-18
    • 2018-09-19
    • 2019-10-29
    • 2020-01-08
    • 2020-11-23
    • 1970-01-01
    • 1970-01-01
    • 2022-10-25
    相关资源
    最近更新 更多