无法从 apollo-server-express 获取函数 [英] Cannot get functions from apollo-server-express
问题描述
我正在遵循教程并尝试启动节点服务器,但我无法将其导入 Apollo 包中的函数
I am following the tutorial and trying to start node server and i cant import this to functions from Apollo package
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}`));
当我由于graphqlExpress 不是函数"而启动服务器失败时,以及当它发表评论并且服务器重新启动有关graphiqlExpress 的相同内容时.也许我正在关注的教程已经过时并且 apollo-server-express 不再提供此类功能?
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. Maybe the tutorial i am following is outdated and apollo-server-express doesnt provide such functions anymore?
推荐答案
Apollo Server 2.0 引入了许多旨在简化设置的重大更改.文档中有一个迁移指南,概述了这些变化.如果您只需要一个 GraphQL 服务器,那么入门可以像这样简单:
Apollo Server 2.0 introduced a number of breaking changes with the intention of simplifying setup. 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:
const { ApolloServer, gql } = require('apollo-server');
const server = new ApolloServer({ typeDefs, resolvers });
server.listen()
请注意,上面只是使用了 apollo-server
包.apollo-server-express
如果您想继续使用 Apollo 作为 express 中间件而不是将 Apollo 作为独立"服务器运行,那么它仍然存在.
Note that the above simply uses the apollo-server
package. apollo-server-express
still exists if you want to continue to utilize Apollo as express middleware instead of running Apollo as a "standalone" server.
const { ApolloServer, gql } = require('apollo-server-express');
const app = require('express')();
const server = new ApolloServer({ typeDefs, resolvers });
server.applyMiddleware({ app });
app.listen({ port: 3000 })
新的 API 无需单独导入和实现额外的中间件,如 body-parser
或 cors
.阅读文档,了解有关如何配置 Apollo 服务器的更多信息实例.
The new API eliminates the need to separately import and implement additional middleware like body-parser
or cors
. Read the docs for more information about how to configure your Apollo Server instance.
这篇关于无法从 apollo-server-express 获取函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!