🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

@apollo/subgraph

Package Overview
Dependencies
Maintainers
1
Versions
153
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@apollo/subgraph

Apollo Subgraph Utilities

2.11.1
latest
Source
npm
Version published
Weekly downloads
494K
2.78%
Maintainers
1
Weekly downloads
 
Created

What is @apollo/subgraph?

@apollo/subgraph is a package that allows you to create a subgraph for Apollo Federation. It enables you to build a federated GraphQL architecture by defining and managing subgraphs that can be composed into a single federated schema.

What are @apollo/subgraph's main functionalities?

Define a Subgraph Schema

This feature allows you to define a subgraph schema using GraphQL type definitions and resolvers. The `buildSubgraphSchema` function is used to create the schema for the subgraph.

const { buildSubgraphSchema } = require('@apollo/subgraph');
const { gql } = require('apollo-server');

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

const resolvers = {
  Query: {
    hello: () => 'Hello world!',
  },
};

const schema = buildSubgraphSchema({ typeDefs, resolvers });

Extend Types from Other Subgraphs

This feature allows you to extend types from other subgraphs using the `@key` directive. This is useful for creating relationships between entities managed by different subgraphs.

const { buildSubgraphSchema } = require('@apollo/subgraph');
const { gql } = require('apollo-server');

const typeDefs = gql`
  extend type User @key(fields: "id") {
    id: ID! @external
    posts: [Post]
  }

  type Post @key(fields: "id") {
    id: ID!
    title: String
    content: String
  }
`;

const resolvers = {
  User: {
    posts(user) {
      return getPostsByUserId(user.id);
    },
  },
};

const schema = buildSubgraphSchema({ typeDefs, resolvers });

Federation Directives

This feature allows you to use federation directives like `@key` and `@external` to define how types are shared and resolved across subgraphs. The `__resolveReference` resolver is used to fetch the complete object when only a reference is provided.

const { buildSubgraphSchema } = require('@apollo/subgraph');
const { gql } = require('apollo-server');

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

const resolvers = {
  Product: {
    __resolveReference(product) {
      return getProductById(product.id);
    },
  },
};

const schema = buildSubgraphSchema({ typeDefs, resolvers });

Other packages similar to @apollo/subgraph

Keywords

graphql

FAQs

Package last updated on 24 Jun 2025

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