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

apollo-server

Package Overview
Dependencies
Maintainers
1
Versions
320
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

apollo-server

Production ready GraphQL Server

  • 3.13.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
184K
increased by0.86%
Maintainers
1
Weekly downloads
 
Created

What is apollo-server?

Apollo Server is a community-driven, open-source GraphQL server that works with any GraphQL schema. It is designed to be a self-contained GraphQL server that can be used with any Node.js HTTP server framework, such as Express, Koa, or Hapi.

What are apollo-server's main functionalities?

Creating a Basic GraphQL Server

This code sets up a basic Apollo Server with a single query type 'hello' that returns a string 'Hello world!'.

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

Integrating with Express

This code demonstrates how to integrate Apollo Server with an Express application.

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 Middleware for Authentication

This code adds middleware to the Apollo Server for authentication by checking the authorization header and adding the user to the context.

const { ApolloServer, gql } = require('apollo-server');

const typeDefs = gql`
  type Query {
    hello: String
  }
`;

const resolvers = {
  Query: {
    hello: (parent, args, context) => {
      if (!context.user) throw new Error('Not authenticated');
      return 'Hello world!';
    },
  },
};

const getUser = (token) => {
  // Mock function to get user from token
  return token ? { id: 1, name: 'John Doe' } : null;
};

const server = new ApolloServer({
  typeDefs,
  resolvers,
  context: ({ req }) => {
    const token = req.headers.authorization || '';
    const user = getUser(token);
    return { user };
  },
});

server.listen().then(({ url }) => {
  console.log(`🚀  Server ready at ${url}`);
});

Other packages similar to apollo-server

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