异常:TypeError:将Apollo用于Azure函数时,graphqlAzureFunctions不是函数

时间:2018-12-04 19:25:10

标签: node.js azure azure-functions apollo apollo-server

我的package.json:

{
    "name": "azurejs",
    "version": "0.1.0",
    "private": true,
    "dependencies": {
        "apollo-server-azure-functions": "^2.2.6",
        "graphql": "^14.0.2",
        "graphql-tools": "^4.0.3"
    },
    "scripts": {},
    "devDependencies": {}
}

直接从阿波罗示例中剥离代码:

const { graphqlAzureFunctions } = require('apollo-server-azure-functions');
const { makeExecutableSchema } = 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 = makeExecutableSchema({
  typeDefs,
  resolvers,
});

module.exports = function run(context, request) {
  graphqlAzureFunctions({ schema })(context, request);
};

https://www.apollographql.com/docs/apollo-server/v1/servers/azure-functions.html

如果我导航到端点,则收到错误消息“ graphqlAzureFunctions不是函数”,我对此进行了搜索,但实际上找不到该函数。

1 个答案:

答案 0 :(得分:1)

您已使用v1教程中的代码安装了v2阿波罗服务器软件包。

按照v2 tutorial,安装apollo-server-azure-functions@alphagraphql,我们可能会继续介绍v2代码。

const { gql, ApolloServer } = require("apollo-server-azure-functions");

// Construct a schema, using GraphQL schema language
const typeDefs = gql`
  type Query {
    hello: String
  }
`;

// A map of functions which return data for the schema.
const resolvers = {
  Query: {
    hello: () => "world"
  }
};

const server = new ApolloServer({ typeDefs, resolvers });

module.exports = server.createHandler();

请注意,在function.json中,我们需要"name": "$return",而模板默认情况下使用"name": "res"

{
  "disabled": false,
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "$return"
    }
  ]
}