Skip to content

Commit 398e5a5

Browse files
authored
docs(apollo-subgraph): adding quickstart instructions for a subgraph (#134)
* docs(apollo-subgraph): adding quickstart instructions for using apollo subgraph * docs(apollo-subgraph): fixed typo fixes #132
1 parent 2e83ce7 commit 398e5a5

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,56 @@ await server.start()
162162

163163
server.applyMiddleware({ app })
164164
```
165+
#### Apollo subgraph server
166+
167+
There is a small change required to make the Apollo Server quickstart work when trying to build an [Apollo Subgraph Server](https://www.apollographql.com/docs/federation/building-supergraphs/subgraphs-apollo-server/).
168+
169+
Notably, we need to wrap our `typDefs` with the `gql` tag, from either the `graphql-tag` or the `apollo-server-core` packages. This converts the `typeDefs` to an `AST` or `DocumentNode` format and is required by `buildSubgraphSchema`, as mentioned in their [docs](https://www.apollographql.com/docs/federation/building-supergraphs/subgraphs-apollo-server/):
170+
>While Apollo Server can accept a string (or `DocumentNode`) for its `typeDefs`, the `buildSubgraphSchema` function below requires the schema we pass in to be a `DocumentNode`.
171+
172+
Then, we must use the `buildSubgraphSchema` function to build a schema that can be passed to an Apollo Gateway/supergraph, instead of `makeExecuteableSchema`. This uses `makeExecutableSchema` under the hood.
173+
174+
```ts
175+
import { ApolloServer } from '@apollo/server';
176+
import { startStandaloneServer } from '@apollo/server/standalone';
177+
import { gql } from 'graphql-tag'; // Or can be imported from 'apollo-server-core'
178+
import { buildSubgraphSchema } from '@apollo/subgraph';
179+
import { createApolloQueryValidationPlugin, constraintDirectiveTypeDefs } from 'graphql-constraint-directive';
180+
181+
const typeDefs = gql`
182+
extend schema @link(url: "https://specs.apollo.dev/federation/v2.0", import: ["@key", "@shareable"])
183+
184+
type Query {
185+
books: [Book]
186+
}
187+
type Book {
188+
title: String
189+
}
190+
type Mutation {
191+
createBook(input: BookInput): Book
192+
}
193+
input BookInput {
194+
title: String! @constraint(minLength: 5, format: "email")
195+
}
196+
`;
197+
198+
const schema = buildSubgraphSchema({
199+
typeDefs: [gql(constraintDirectiveTypeDefs), typeDefs]
200+
});
201+
202+
const plugins = [
203+
createApolloQueryValidationPlugin({
204+
schema
205+
})
206+
]
207+
208+
const server = new ApolloServer({
209+
schema,
210+
plugins
211+
});
212+
213+
await startStandaloneServer(server);
214+
```
165215

166216
#### Express
167217

0 commit comments

Comments
 (0)