0.24.0 • Published 7 days ago

@wundergraph/composition v0.24.0

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
7 days ago

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, Subgraph } from '@wundergraph/composition';
import { parse } from 'graphql';

const federationResult: 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!]!
    }
  `),
};

FederationResultContainer properties

The federateSubgraphs function returns a FederationResultContainer object. If federation was successful, the errors property will be undefined, and the federationResult object will be defined.

propertyDescriptiontype
errorsarray of composition errorsError[] | undefined
federationResultFederationResult object (see below)FederationResult | undefined

FederationResult properties

If federation was successful, FieldResultContainer will contain a defined federationResult property.

propertyDescriptiontype
argumentConfigurationsarray of router argument configurationsArgumentConfigurationData[]
federatedGraphASTan AST object representation of the federated graph sdlgraphql.DocumentNode
federatedGraphSchemaa schema object representation of the federated graph sdlgraphql.GraphQLSchema

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, Subgraph } from '@wundergraph.composition';
import { print, printSchema } from 'graphql';

const { errors, federationResult } = federateSubgraphs([subgraphA, subgraphB]);
if (errors) {
  for (const err of errors) {
    console.log(err.message);
  }
} else {
  // Both options to print the federated graph as a string are included for documentational purposes only
  console.log(print(federationResult!.federatedGraphAST)); // log the federated graph AST as a string
  console.log(printSchema(federationResult!.federatedGraphSchema)); // log the federated graph schema as a string
}

// 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 FederationResultContainer.errors array. Often, the error message will suggest potential fixes. For instance:

Error: The following root path is unresolvable:
    Query.user.name
    This is because:
        The root type field "Query.user" is defined in the following subgraphs: "subgraph-b".
    However, "User.name" is only defined in the following subgraphs: "subgraph-c".
    Consequently, "User.name" cannot be resolved through the root type field "Query.user".
    Potential solutions:
        Convert "User" into an entity using a "@key" directive.
        Add the shareable root type field "Query.user" to the following subgraphs: "subgraph-c".
            For example (note that V1 fields are shareable by default and do not require a directive):
                type Query {
                    ...
                    user: User @shareable
                }

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
0.24.0

7 days ago

0.23.2

11 days ago

0.23.1

16 days ago

0.23.0

18 days ago

0.22.1

28 days ago

0.22.0

1 month ago

0.21.0

1 month ago

0.20.3

2 months ago

0.20.2

2 months ago

0.20.1

2 months ago

0.20.0

2 months ago

0.19.0

2 months ago

0.18.5

3 months ago

0.18.4

3 months ago

0.18.3

3 months ago

0.18.1

3 months ago

0.18.2

3 months ago

0.18.0

4 months ago

0.17.1

4 months ago

0.17.0

4 months ago

0.16.0

4 months ago

0.15.0

4 months ago

0.14.0

5 months ago

0.13.0

5 months ago

0.12.2

5 months ago

0.12.1

5 months ago

0.12.0

5 months ago

0.11.3

6 months ago

0.11.2

6 months ago

0.11.1

6 months ago

0.11.0

6 months ago

0.10.1

7 months ago

0.10.0

8 months ago

0.9.0

8 months ago

0.8.1

8 months ago

0.8.0

8 months ago

0.7.1

8 months ago

0.7.0

8 months ago

0.6.2

8 months ago

0.6.1

9 months ago

0.6.0

9 months ago

0.5.3

9 months ago

0.5.2

9 months ago

0.5.1

9 months ago

0.5.0

9 months ago

0.4.1

9 months ago

0.4.0

9 months ago

0.3.0

9 months ago

0.2.0

9 months ago

0.0.1

10 months ago