Skip to content

Commit

Permalink
add linkUrl option to buildSubGraphSchema
Browse files Browse the repository at this point in the history
  • Loading branch information
hayes committed Feb 7, 2023
1 parent 4fa71f7 commit bce9778
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/afraid-tomatoes-jam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@pothos/plugin-federation': minor
---

Add `linkUrl` option to buildSubGraphSchema
6 changes: 3 additions & 3 deletions packages/deno/packages/core/config-store.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion packages/deno/packages/core/types/builder-options.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions packages/deno/packages/migration/mod.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions packages/deno/packages/plugin-add-graphql/mod.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions packages/deno/packages/plugin-directives/index.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/deno/packages/plugin-errors/index.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 16 additions & 4 deletions packages/plugin-federation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ information on federation, see the

### Install

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

```bash
yarn add @pothos/plugin-federation @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
Expand All @@ -28,10 +29,11 @@ yarn add @apollo/server
### Setup

```typescript
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: [FederationPlugin],
plugins: [DirectivePlugin, FederationPlugin],
});
```

Expand Down Expand Up @@ -162,7 +164,10 @@ ReviewType.implement({

```typescript
// Use new `toSubGraphSchema` method to add subGraph specific types and queries to the schema
const schema = builder.toSubGraphSchema({});
const schema = builder.toSubGraphSchema({
// defaults to v2.3
linkUrl: 'https://specs.apollo.dev/federation/v2.3',
});

const server = new ApolloServer({
schema,
Expand All @@ -179,3 +184,10 @@ startStandaloneServer(server, { listen: { port: 4000 } })

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](https://github.com/hayes/pothos/tree/main/packages/plugin-federation/tests/example)

### Printing the schema

If you are printing the schema as a string for any reason, and then using the printed schema for
Apollo Federation(submitting if using Managed Federation, or composing manually with `rover`), you
must use `printSubgraphSchema`(from `@apollo/subgraph`) or another compatible way of printing the
schema(that includes directives) in order for it to work.
6 changes: 5 additions & 1 deletion packages/plugin-federation/src/global-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,11 @@ declare global {

selection: <Shape extends object>(selection: SelectionFromShape<Shape>) => Selection<Shape>;

toSubGraphSchema: (options: BuildSchemaOptions<Types>) => GraphQLSchema;
toSubGraphSchema: (
options: BuildSchemaOptions<Types> & {
linkUrl?: string;
},
) => GraphQLSchema;

asEntity: <Param extends ObjectRef<unknown>, KeySelection extends Selection<object>>(
param: Param,
Expand Down
5 changes: 4 additions & 1 deletion packages/plugin-federation/src/schema-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ schemaBuilderProto.externalRef = function externalRef<
return new ExternalEntityRef<SchemaTypes, Shape, KeySelection>(this, name, key, resolveReference);
};

schemaBuilderProto.toSubGraphSchema = function toSubGraphSchema(options) {
schemaBuilderProto.toSubGraphSchema = function toSubGraphSchema({
linkUrl = 'https://specs.apollo.dev/federation/v2.3',
...options
}) {
const schema = this.toSchema({
...options,
extensions: {
Expand Down
5 changes: 4 additions & 1 deletion website/pages/docs/plugins/federation.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,10 @@ ReviewType.implement({

```typescript
// Use new `toSubGraphSchema` method to add subGraph specific types and queries to the schema
const schema = builder.toSubGraphSchema({});
const schema = builder.toSubGraphSchema({
// defaults to v2.3
linkUrl: 'https://specs.apollo.dev/federation/v2.3',
});

const server = new ApolloServer({
schema,
Expand Down

0 comments on commit bce9778

Please sign in to comment.