简体   繁体   English

无法从 apollo-server-express 获取函数

[英]Cannot get functions from apollo-server-express

I am following the tutorial and trying to start node server and i cant import this to functions from Apollo package我正在遵循教程并尝试启动节点服务器,但我无法将其导入 Apollo 包中的函数

const {graphqlExpress, graphiqlExpress} = require('apollo-server-express'); // here i importing fucntions
const bodyParser = require('body-parser'); // import parser
const cors = require('cors'); // import cors
const express = require('express'); // import cors
const { makeExecutableSchema } = require('graphql-tools');

const port = 9000; // define port

const schema = makeExecutableSchema({typeDefs, resolvers}); // init shcema

const app = express();
app.use(cors(), bodyParser.json());
app.use('/graphql', graphqlExpress({schema})); // is not a function
app.use('/graphiql', graphiqlExpress({endpointUrl: '/graphql'})); // is not a function
app.listen(port, () => console.log(`Server is running on the port ${port}`));

When i starting the server if fails due to "graphqlExpress is not a function", and when it commented and the server restarted the same thing about graphiqlExpress.当我启动服务器时,如果由于“graphqlExpress 不是函数”而失败,并且当它发表评论并且服务器重新启动关于graphiqlExpress 的相同事情时。 Maybe the tutorial i am following is outdated and apollo-server-express doesnt provide such functions anymore?也许我正在遵循的教程已经过时并且 apollo-server-express 不再提供此类功能?

Apollo Server 2.0 introduced a number of breaking changes with the intention of simplifying setup. Apollo Server 2.0 引入了许多重大更改,旨在简化设置。 There's a migration guide in the documentation that outlines the changes.文档中有一个迁移指南,概述了这些更改。 If all you need is a GraphQL server, getting started can be as simple as this:如果您只需要一个 GraphQL 服务器,那么入门可以像这样简单:

const { ApolloServer, gql } = require('apollo-server');

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

Note that the above simply uses the apollo-server package.请注意,上面只是使用了apollo-server包。 apollo-server-express still exists if you want to continue to utilize Apollo as express middleware instead of running Apollo as a "standalone" server.如果您想继续使用 Apollo 作为 express 中间件而不是将 Apollo 作为“独立”服务器运行,那么apollo-server-express仍然存在。

const { ApolloServer, gql } = require('apollo-server-express');
const app = require('express')();

const server = new ApolloServer({ typeDefs, resolvers });
server.applyMiddleware({ app });
app.listen({ port: 3000 })

The new API eliminates the need to separately import and implement additional middleware like body-parser or cors .新的 API 无需单独导入和实现额外的中间件,如body-parsercors Read the docs for more information about how to configure your Apollo Server instance. 阅读文档以获取有关如何配置 Apollo Server 实例的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM