New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@wundergraph/composition

Package Overview
Dependencies
Maintainers
0
Versions
83
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@wundergraph/composition

  • 0.37.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
26K
decreased by-3.79%
Maintainers
0
Weekly downloads
 
Created
Source

WunderGraph Composition

npm version

The WunderGraph composition library facilitates the federation of multiple subgraph schemas into a single federated GraphQL schema.

Prerequisites

Federating subgraphs

The federateSubgraphs function is responsible for producing a valid federated graph. Each subgraph will be normalized and validated before federation. This normalization process does not affect the upstream schema. The final federated graph will also be validated. The function must be provided with an array of at least one Subgraph object. An example federation of two simple subgraphs:

import { federateSubgraphs, FederationResult, Subgraph } from '@wundergraph/composition';
import { parse } from 'graphql';

const result: FederationResult = federateSubgraphs([subgraphA, subgraphB]);

const subgraphA: Subgraph = {
  name: 'subgraph-a',
  url: 'http://localhost:4001',
  definitions: parse(`
    type User @key(fields: "id") {
      id: ID!
      name: String!
    }
  `),
};

const subgraphB: Subgraph = {
  name: 'subgraph-b',
  url: 'http://localhost:4002',
  definitions: parse(`
    type Query {
      users: [User!]!
    }
      
    type User @key(fields: "id") {
      id: ID!
      interests: [String!]!
    }
  `),
};

FederationResult

The federateSubgraphs function returns FederationResult, which is a union of FederationResultSuccess and FederationResultFailure. Both types in the union always define the following mutual properties:

propertyDescriptiontype
successassertion of composition successboolean
warningsarray of composition warnings (if any)Array
FederationResultSuccess

If federation was successful, the return type is FederationResultSuccess.

propertyDescriptiontype
federatedGraphASTan AST object representation of the federated graph sdlgraphql.DocumentNode
federatedGraphSchemaa schema object representation of the federated graph sdlgraphql.GraphQLSchema
successassertion that composition was successfultrue
warningsarray of composition warnings (if any)Array
FederationResultFailure

If federation was unsuccessful, the return type is FederationResultFailure.

propertyDescriptiontype
errorsarray of composition errorsArray
successassertion that composition was unsuccessfulfalse
warningsarray of composition warnings (if any)Array

Debugging

If normalization of any subgraph fails, or the federated graph itself is invalid, the AST and schema will not be produced (undefined properties). In these cases, the errors array will be defined and populated. An example of a simple debugging framework might be:

import { federateSubgraphs, FederationResult, Subgraph } from '@wundergraph.composition';
import { print, printSchema } from 'graphql';

const result: FederationResult = federateSubgraphs([subgraphA, subgraphB]);

if (result.success) {
  // Both options to print the federated graph as a string are included for documentational purposes only
  console.log(print(result.federatedGraphAST)); // log the federated graph AST as a string
  console.log(printSchema(result.federatedGraphSchema)); // log the federated graph schema as a string
} else {
  for (const err of result.errors) {
    console.log(err.message);
  }
}
for (const warning of result.warnings) {
  console.log(warning);
}

// subgraph definitions would be below [removed for brevity]

Errors

Errors can happen in three main stages:

  1. While validating the subgraph metadata, e.g., validating that each Subgraph object has a unique name.
  2. During the normalization process, which prepares the subgraph for federation. (if this stage fails, federation will not be attempted)
  3. During the federation process itself.

All errors will be appended to the FederationResultFailure.errors array.

Subgraph object

The Subgraph object is the core of the WunderGraph composition library. The definitions property must be provided as a graphQL.DocumentNode type. This is easily achieved by passing string representation of the subgraph SDL to the graphql.js parse function. An example is shown below:

import { Subgraph } from '@wundergraph/composition'
import { parse } from 'graphql';

const subgraphA: Subgraph = {
  name: 'subgraph-a',
  url: 'http://localhost:4001',
  definitions: parse(`
    type Query {
      user: User!
    }

    type User {
      name: String!
    }
  `),
};

Subgraph Properties

propertyDescriptiontype
nameunique name of the subgraphstring
urlunique endpoint for the subgraphstring
definitionsan AST representation of the subgraph SDLgraphql.DocumentNode

Contributing

When adding or changing error, please ensure GraphQL types begin with a capital letter for clarity:

  • Enum
  • Input Object
  • Interface
  • Object
  • Scalar
  • Union

FAQs

Package last updated on 27 Feb 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

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