Socket
Socket
Sign inDemoInstall

@apollo/server

Package Overview
Dependencies
Maintainers
1
Versions
63
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@apollo/server

Core engine for Apollo GraphQL server


Version published
Weekly downloads
1M
increased by5.87%
Maintainers
1
Weekly downloads
 
Created

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

Keywords

FAQs

Package last updated on 10 Oct 2022

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