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}`));
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 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()
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 })
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With