Security News
Node.js EOL Versions CVE Dubbed the "Worst CVE of the Year" by Security Experts
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
apollo-server-express
Advanced tools
The apollo-server-express package is a library that allows you to integrate Apollo Server with an Express application. It provides tools to build a GraphQL server with Express, enabling you to define your GraphQL schema, resolvers, and context, and to handle HTTP requests and responses.
Setting up a basic Apollo Server with Express
This code sets up a basic Apollo Server with Express. It defines a simple GraphQL schema with a single query and a resolver for that query. The Apollo Server is then applied as middleware to the Express app, and the server is started on port 4000.
const express = require('express');
const { ApolloServer, gql } = 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.applyMiddleware({ app });
app.listen({ port: 4000 }, () =>
console.log(`Server ready at http://localhost:4000${server.graphqlPath}`)
);
Adding context to Apollo Server
This code demonstrates how to add context to the Apollo Server. The context function extracts the user from the request headers and makes it available to the resolvers. The resolver for the 'hello' query uses the context to personalize the response.
const express = require('express');
const { ApolloServer, gql } = require('apollo-server-express');
const typeDefs = gql`
type Query {
hello: String
}
`;
const resolvers = {
Query: {
hello: (parent, args, context) => `Hello ${context.user}!`,
},
};
const server = new ApolloServer({
typeDefs,
resolvers,
context: ({ req }) => ({ user: req.headers.user || 'world' })
});
const app = express();
server.applyMiddleware({ app });
app.listen({ port: 4000 }, () =>
console.log(`Server ready at http://localhost:4000${server.graphqlPath}`)
);
Using Apollo Server with Express middleware
This code shows how to use Apollo Server with other Express middleware. In this example, the body-parser middleware is used to parse JSON request bodies before the Apollo Server middleware is applied.
const express = require('express');
const { ApolloServer, gql } = require('apollo-server-express');
const bodyParser = require('body-parser');
const typeDefs = gql`
type Query {
hello: String
}
`;
const resolvers = {
Query: {
hello: () => 'Hello world!',
},
};
const server = new ApolloServer({ typeDefs, resolvers });
const app = express();
app.use(bodyParser.json());
server.applyMiddleware({ app });
app.listen({ port: 4000 }, () =>
console.log(`Server ready at http://localhost:4000${server.graphqlPath}`)
);
The express-graphql package is a middleware for Express that allows you to create a GraphQL HTTP server. It is simpler and more lightweight compared to apollo-server-express, but it lacks some of the advanced features and integrations provided by Apollo Server, such as schema stitching, Apollo Federation, and built-in support for subscriptions.
graphql-yoga is a fully-featured GraphQL server that works with any GraphQL schema. It is built on top of Express and apollo-server, and it provides a simple and flexible API for building GraphQL servers. Compared to apollo-server-express, graphql-yoga offers a more opinionated setup with built-in support for features like subscriptions, file uploads, and GraphQL Playground.
mercurius is a GraphQL adapter for Fastify, a fast and low-overhead web framework for Node.js. It provides a similar set of features to apollo-server-express, including schema stitching, subscriptions, and context management. However, it is designed to work specifically with Fastify, which offers better performance and scalability compared to Express.
This is the Express integration of Apollo Server. Apollo Server is a community-maintained open-source GraphQL server that works with many Node.js HTTP server frameworks. Read the docs. Read the CHANGELOG.
A full example of how to use apollo-server-express
can be found in the docs.
Before Apollo Server 3, we officially supported using this package with connect
as well. connect
is an older framework that express
evolved from. For now, we believe that this package is still compatible with connect
and we even run tests against connect
, but we may choose to break this compatibility at some point without a major version bump. If you rely on the ability to use Apollo Server with connect
, you may wish to make your own integration.
GraphQL Server is built with the following principles in mind:
Anyone is welcome to contribute to GraphQL Server, just read CONTRIBUTING.md, take a look at the roadmap and make your first PR!
FAQs
Production-ready Node.js GraphQL server for Express
The npm package apollo-server-express receives a total of 536,395 weekly downloads. As such, apollo-server-express popularity was classified as popular.
We found that apollo-server-express demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.