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

@apollo/federation

Package Overview
Dependencies
Maintainers
1
Versions
144
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@apollo/federation

Apollo Federation Utilities

  • 0.3.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
383K
increased by0.82%
Maintainers
1
Weekly downloads
 
Created

What is @apollo/federation?

@apollo/federation is a library that enables the composition of multiple GraphQL services into a single unified graph. It allows you to build a federated data graph, which can be composed of multiple subgraphs, each managed by different teams or services.

What are @apollo/federation's main functionalities?

Defining a Federated Schema

This code demonstrates how to define a federated schema using @apollo/federation. It includes type definitions and resolvers for a Product type, and sets up an Apollo Server with the federated schema.

const { buildFederatedSchema } = require('@apollo/federation');
const { ApolloServer, gql } = require('apollo-server');

const typeDefs = gql`
  type Product @key(fields: "id") {
    id: ID!
    name: String
  }

  extend type Query {
    product(id: ID!): Product
  }
`;

const resolvers = {
  Product: {
    __resolveReference(product) {
      return { id: product.id, name: "Sample Product" };
    }
  },
  Query: {
    product(_, { id }) {
      return { id, name: "Sample Product" };
    }
  }
};

const server = new ApolloServer({
  schema: buildFederatedSchema([{ typeDefs, resolvers }])
});

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

Extending Types Across Services

This code demonstrates how to extend types across different services using @apollo/federation. It shows how to add a reviews field to the Product type, which is defined in another service.

const { buildFederatedSchema } = require('@apollo/federation');
const { ApolloServer, gql } = require('apollo-server');

const typeDefs = gql`
  extend type Product @key(fields: "id") {
    id: ID! @external
    reviews: [Review]
  }

  type Review {
    id: ID!
    content: String
  }

  extend type Query {
    reviews(productId: ID!): [Review]
  }
`;

const resolvers = {
  Product: {
    reviews(product) {
      return [{ id: "1", content: "Great product!" }];
    }
  },
  Query: {
    reviews(_, { productId }) {
      return [{ id: "1", content: "Great product!" }];
    }
  }
};

const server = new ApolloServer({
  schema: buildFederatedSchema([{ typeDefs, resolvers }])
});

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

Gateway Setup

This code demonstrates how to set up an Apollo Gateway to compose multiple federated services into a single graph. It includes a service list with URLs for the products and reviews services.

const { ApolloServer } = require('apollo-server');
const { ApolloGateway } = require('@apollo/gateway');

const gateway = new ApolloGateway({
  serviceList: [
    { name: 'products', url: 'http://localhost:4000' },
    { name: 'reviews', url: 'http://localhost:4001' }
  ]
});

const server = new ApolloServer({
  gateway,
  subscriptions: false
});

server.listen({ port: 4002 }).then(({ url }) => {
  console.log(`🚀 Gateway ready at ${url}`);
});

Other packages similar to @apollo/federation

FAQs

Package last updated on 24 May 2019

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