Socket
Socket
Sign inDemoInstall

@apollo/subgraph

Package Overview
Dependencies
Maintainers
0
Versions
141
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@apollo/subgraph

Apollo Subgraph Utilities


Version published
Weekly downloads
462K
decreased by-3.13%
Maintainers
0
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

FAQs

Package last updated on 27 Sep 2024

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