Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

apollo-server-express

Package Overview
Dependencies
Maintainers
1
Versions
314
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

apollo-server-express

Production-ready Node.js GraphQL server for Express

  • 3.13.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
722K
increased by5.49%
Maintainers
1
Weekly downloads
 
Created

What is apollo-server-express?

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.

What are apollo-server-express's main functionalities?

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}`)
);

Other packages similar to apollo-server-express

Keywords

FAQs

Package last updated on 14 Nov 2023

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc