As explained here, an Apollo Server is an open-source, spec-compliant GraphQL server that's compatible with any GraphQL client, including Apollo Client. It's the best way to build a production-ready, self-documenting GraphQL API that can use data from any source.
Initialize a node js project using npm init --yes && npm pkg set type="module"
Applications that run Apollo Server require two top-level dependencies:
- graphql
- @apollo/server
npm i graphql @apollo/server
- Create a file named index.js in the root of your project folder
- Paste the following code:
import { ApolloServer } from "@apollo/server";
import { startStandaloneServer } from '@apollo/server/standalone'
const typeDefs = `#graphql
type Book {
title: String
author: String
}
type Query {
books: [Book]
}
`
const books = [
{
title: "The Awekaning",
author: "Kate Chopin"
},
{
title: "City of Glass",
author: "Paul Auster"
}
]
const resolvers = {
Query: {
books: () => books
}
}
const server = new ApolloServer({
typeDefs,
resolvers
})
const { url } = await startStandaloneServer(server, {
listen : { port: 4000 },
});
console.log(`🚀 Server ready at: ${url}`);
Note Add this to the Scripts of your package.json file:
"start": "node index.js"
Then run your server with:
npm start
Execute your query by visiting the url