What is @apollo/server?
@apollo/server is a powerful and flexible GraphQL server library that allows you to build and run a GraphQL API in Node.js. It provides tools for schema definition, query execution, and integration with various data sources and middleware.
What are @apollo/server's main functionalities?
Schema Definition
Defines a simple GraphQL schema with a single query and sets up an Apollo Server instance to serve it.
const { ApolloServer, gql } = require('@apollo/server');
const typeDefs = gql`
type Query {
hello: String
}
`;
const resolvers = {
Query: {
hello: () => 'Hello world!',
},
};
const server = new ApolloServer({ typeDefs, resolvers });
server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});
Middleware Integration
Integrates Apollo Server with an Express.js application, allowing you to use GraphQL as middleware.
const { ApolloServer } = require('@apollo/server');
const express = require('express');
const { expressMiddleware } = require('@apollo/server/express');
const typeDefs = gql`
type Query {
hello: String
}
`;
const resolvers = {
Query: {
hello: () => 'Hello world!',
},
};
const server = new ApolloServer({ typeDefs, resolvers });
const app = express();
server.start().then(() => {
app.use('/graphql', expressMiddleware(server));
app.listen({ port: 4000 }, () => {
console.log('Server is running on http://localhost:4000/graphql');
});
});
Data Source Integration
Shows how to integrate a REST API as a data source in Apollo Server, allowing you to fetch data from external APIs.
const { ApolloServer, gql } = require('@apollo/server');
const { RESTDataSource } = require('apollo-datasource-rest');
class MyAPI extends RESTDataSource {
constructor() {
super();
this.baseURL = 'https://api.example.com/';
}
async getExampleData() {
return this.get('example-endpoint');
}
}
const typeDefs = gql`
type Query {
exampleData: String
}
`;
const resolvers = {
Query: {
exampleData: async (_source, _args, { dataSources }) => {
return dataSources.myAPI.getExampleData();
},
},
};
const server = new ApolloServer({
typeDefs,
resolvers,
dataSources: () => ({
myAPI: new MyAPI(),
}),
});
server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});
Other packages similar to @apollo/server
express-graphql
express-graphql is a GraphQL HTTP server middleware for Express. It is simpler and more lightweight compared to @apollo/server, making it a good choice for smaller applications or those that do not require the full feature set of Apollo Server.
graphql-yoga
graphql-yoga is a fully-featured GraphQL server that is easy to set up and use. It is built on top of Express and Apollo Server, providing a more opinionated and streamlined experience. It is suitable for developers who want a quick and easy way to get started with GraphQL.
mercurius
mercurius is a GraphQL adapter for Fastify, a fast and low-overhead web framework for Node.js. It offers high performance and integrates seamlessly with Fastify, making it a great choice for applications that prioritize speed and efficiency.
apollo-server-core
This package implements the core logic of Apollo Server. It exports a base version of ApolloServer
. Typically you do not use this class directly but instead use an ApolloServer
imported from the batteries-included apollo-server
package or one of the integration packages like apollo-server-express
.
It also exports a set of plugins such as ApolloServerPluginUsageReporting
which you can provide to the plugins
option to the ApolloServer
constructor.
Read the docs.
Read the CHANGELOG.