2.8.60 • Published 19 days ago

@pothos-examples/prisma-federation v2.8.60

Weekly downloads
-
License
-
Repository
-
Last release
19 days ago

Federation Plugin

A plugin for building subGraphs that are compatible with Apollo Federation 2.

Usage

This page will describe the basics of the Pothos API for federation, but will not cover detailed information on how federation works, or what all the terms on this page mean. For more general information on federation, see the official docs

Install

You will need to install the plugin, as well as the directives plugin (@pothos/plugin-directives) and @apollo/subgraph

yarn add @pothos/plugin-federation @pothos/plugin-directives @apollo/subgraph

You will likely want to install @apollo/server as well, but it is not required if you want to use a different server

yarn add @apollo/server

Setup

import DirectivePlugin from '@pothos/plugin-directives';
import FederationPlugin from '@pothos/plugin-federation';
const builder = new SchemaBuilder({
  // If you are using other plugins, the federation plugin should be listed after plugins like auth that wrap resolvers
  plugins: [DirectivePlugin, FederationPlugin],
});

Defining entities

Defining entities for you schema is a 2 step process. First you will need to define an object type as you would normally, then you can convert that object type to an entity by providing a key (or keys), and a method to load that entity.

const UserType = builder.objectRef<User>('User').implement({
  fields: (t) => ({
    id: t.exposeID('id'),
    name: t.exposeString('name'),
    username: t.exposeString('username'),
  }),
});

builder.asEntity(UserType, {
  key: builder.selection<{ id: string }>('id'),
  resolveReference: (user, users) => users.find(({ id }) => user.id === id),
});

keys are defined using builder.selection. This method MUST be called with a generic argument that defines the types for any fields that are part of the key. key may also be an array. resolveReference will be called with the type used by the key selection.

Entities are Object types that may be extended with or returned by fields in other services. builder.asEntity describes how the Entity will be loaded when used by another services. The key select (or selection) should use the types of scalars your server will produce for inputs. For example, Apollo server will convert all ID fields to strings, even if resolvers in other services returns IDs as numbers.

Extending external entities

External entities can be extended by calling builder.externalRef, and then calling implement on the returned ref.

builder.externalRef takes the name of the entity, a selection (using builder.selection, just like a key on an entity object), and a resolve method that loads an object given a key. The return type of the resolver is used as the backing type for the ref, and will be the type of the parent arg when defining fields for this type. The key also describes what fields will be selected from another service to use as the parent object in resolvers for fields added when implementing the externalRef.

const ProductRef = builder.externalRef(
  'Product',
  builder.selection<{ upc: string }>('upc'),
  (entity) => {
    const product = inventory.find(({ upc }) => upc === entity.upc);

    // extends the entity ({upc: string}) with other product details available in this service
    return product && { ...entity, ...product };
  },
);

ProductRef.implement({
  // Additional external fields can be defined here which can be used by `requires` or `provides` directives
  externalFields: (t) => ({
    price: t.float(),
    weight: t.float(),
  }),
  fields: (t) => ({
    // exposes properties added during loading of the entity above
    upc: t.exposeString('upc'),
    inStock: t.exposeBoolean('inStock'),
    shippingEstimate: t.float({
      // fields can add a `requires` directive for any of the externalFields defined above
      // which will be made available as part of the first arg in the resolver.
      requires: builder.selection<{ weight?: number; price?: number }>('price weight'),
      resolve: (data) => {
        // free for expensive items
        if ((data.price ?? 0) > 1000) {
          return 0;
        }
        // estimate is based on weight
        return (data.weight ?? 0) * 0.5;
      },
    }),
  }),
});

Adding a provides directive

To add a @provides directive, you will need to implement the Parent type of the field being provided as an external ref, and then use the .provides method of the returned ref when defining the field that will have the @provides directive. The provided field must be listed as an externalField in the external type.

const UserType = builder.externalRef('User', builder.selection<{ id: string }>('id')).implement({
  externalFields: (t) => ({
    // The field that will be provided
    username: t.string(),
  }),
  fields: (t) => ({
    id: t.exposeID('id'),
  }),
});

const ReviewType = builder.objectRef<Review>('Review');
ReviewType.implement({
  fields: (t) => ({
    id: t.exposeID('id'),
    body: t.exposeString('body'),
    author: t.field({
      // using UserType.provides<...>(...) instead of just UserType adds the provide annotations
      // and ensures the resolved value includes data for the provided field
      // The generic in Type.provides works the same as the `builder.selection` method.
      type: UserType.provides<{ username: string }>('username'),
      resolve: (review) => ({
        id: review.authorID,
        username: usernames.find((username) => username.id === review.authorID)!.username,
      }),
    }),
    product: t.field({
      type: Product,
      resolve: (review) => ({ upc: review.product.upc }),
    }),
  }),
});

Building your schema and starting a server

// Use new `toSubGraphSchema` method to add subGraph specific types and queries to the schema
const schema = builder.toSubGraphSchema({});

const server = new ApolloServer({
  schema,
});

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

For a functional example that combines multiple graphs built with Pothos into a single schema see https://github.com/hayes/pothos/tree/main/packages/plugin-federation/tests/example

2.8.60

19 days ago

2.8.59

2 months ago

2.8.56

2 months ago

2.8.58

2 months ago

2.8.57

2 months ago

2.8.55

3 months ago

2.8.54

3 months ago

2.8.52

3 months ago

2.8.53

3 months ago

2.8.51

4 months ago

2.8.50

4 months ago

2.8.49

5 months ago

2.8.48

5 months ago

2.8.47

5 months ago

2.8.30

9 months ago

2.8.39

8 months ago

2.8.34

8 months ago

2.8.33

8 months ago

2.8.32

9 months ago

2.8.31

9 months ago

2.8.38

8 months ago

2.8.37

8 months ago

2.8.36

8 months ago

2.8.35

8 months ago

2.8.41

8 months ago

2.8.40

8 months ago

2.8.45

5 months ago

2.8.44

7 months ago

2.8.43

7 months ago

2.8.42

8 months ago

2.8.46

5 months ago

2.8.29

9 months ago

2.8.28

9 months ago

2.8.23

10 months ago

2.8.22

10 months ago

2.8.27

9 months ago

2.8.26

10 months ago

2.8.25

10 months ago

2.8.24

10 months ago

2.8.19

12 months ago

2.8.18

1 year ago

2.8.21

11 months ago

2.8.20

12 months ago

2.8.17

1 year ago

2.8.16

1 year ago

2.8.15

1 year ago

2.8.14

1 year ago

2.8.13

1 year ago

2.8.12

1 year ago

2.8.11

1 year ago

2.8.10

1 year ago

2.8.9

1 year ago

2.8.8

1 year ago

2.8.7

1 year ago

2.8.6

1 year ago

2.8.5

1 year ago

2.8.4

1 year ago

2.8.1

1 year ago

2.8.3

1 year ago

2.8.2

1 year ago

2.8.0

1 year ago

2.7.4

1 year ago

2.7.3

1 year ago

2.7.5

1 year ago

2.6.22

1 year ago

2.6.23

1 year ago

2.6.24

1 year ago

2.7.0

1 year ago

2.7.2

1 year ago

2.7.1

1 year ago

2.6.20

1 year ago

2.6.21

1 year ago

2.6.19

1 year ago

2.6.18

1 year ago

2.6.17

1 year ago

2.6.16

1 year ago

2.6.15

1 year ago

2.6.14

1 year ago

2.6.13

1 year ago

2.6.12

1 year ago

2.6.1

2 years ago

2.6.0

2 years ago

2.6.3

2 years ago

2.6.2

2 years ago

2.6.11

2 years ago

2.6.10

2 years ago

2.6.5

2 years ago

2.6.4

2 years ago

2.6.7

2 years ago

2.6.6

2 years ago

2.6.9

2 years ago

2.6.8

2 years ago

2.5.0

2 years ago

2.5.2

2 years ago

2.5.1

2 years ago

2.5.4

2 years ago

2.5.3

2 years ago

2.5.5

2 years ago

2.4.21

2 years ago

2.4.22

2 years ago

2.4.20

2 years ago

2.4.19

2 years ago

2.4.18

2 years ago

2.4.17

2 years ago

2.4.16

2 years ago

2.4.15

2 years ago

2.4.1

2 years ago

2.4.0

2 years ago

2.4.14

2 years ago

2.4.3

2 years ago

2.4.13

2 years ago

2.4.2

2 years ago

2.4.5

2 years ago

2.3.6

2 years ago

2.4.4

2 years ago

2.4.10

2 years ago

2.4.12

2 years ago

2.4.11

2 years ago

2.4.7

2 years ago

2.3.8

2 years ago

2.4.6

2 years ago

2.3.7

2 years ago

2.4.9

2 years ago

2.4.8

2 years ago

2.3.9

2 years ago

2.3.12

2 years ago

2.3.11

2 years ago

2.3.10

2 years ago

2.3.5

2 years ago

2.3.4

2 years ago

2.3.3

2 years ago

2.3.2

2 years ago