本文介绍了带有 Azure 函数的 Graphql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法通过 azure 函数和 nodejs 实现 Graphql.例如,类似 - https://www.npmjs.com/package/graphql-服务器-lambda

is there a way to implement Graphql via azure functions and nodejs. For example, something like - https://www.npmjs.com/package/graphql-server-lambda

推荐答案

Apollo 为 GraphQL 提供 Azure 函数集成:

Apollo provides Azure Function Integration for GraphQL:

apollo-server-azure-functions

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

Here is the sample provided on their 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);
  }
};

这篇关于带有 Azure 函数的 Graphql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 21:09