-
Notifications
You must be signed in to change notification settings - Fork 272
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #295 from apollographql/igni/connectors_docker_exa…
…mple Connectors: docker example
- Loading branch information
Showing
18 changed files
with
2,926 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Connectors example | ||
|
||
## Prerequisites | ||
|
||
Docker | ||
Rust | ||
|
||
## Setup | ||
|
||
in this folder run: | ||
|
||
```bash | ||
$ docker-compose up | ||
``` | ||
|
||
In an other terminal, go to the root of this project (the router directory) and run: | ||
|
||
```bash | ||
$ cargo run -- --dev -s ./examples/connectors/supergraph-docker.graphql | ||
``` | ||
|
||
You can then open the sandbox explorer by visiting http://localhost:4000 in your browser! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
FROM node:18-bullseye-slim | ||
|
||
WORKDIR /usr/src/app | ||
|
||
COPY package.json . | ||
|
||
RUN npm install | ||
|
||
COPY index.js . | ||
|
||
CMD [ "npm", "run", "start" ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
const { ApolloServer } = require("@apollo/server"); | ||
const { expressMiddleware } = require("@apollo/server/express4"); | ||
const { buildSubgraphSchema } = require("@apollo/subgraph"); | ||
const { | ||
ApolloServerPluginDrainHttpServer, | ||
} = require("@apollo/server/plugin/drainHttpServer"); | ||
const rateLimit = require("express-rate-limit"); | ||
const express = require("express"); | ||
const http = require("http"); | ||
const { json } = require("body-parser"); | ||
const cors = require("cors"); | ||
const { parse } = require("graphql"); | ||
|
||
const rateLimitTreshold = process.env.LIMIT || 5000; | ||
|
||
const typeDefs = parse(`#graphql | ||
extend schema | ||
@link(url: "https://specs.apollo.dev/federation/v2.3" | ||
import: ["@key" "@shareable"]) | ||
type Query { | ||
me: User | ||
} | ||
type User @key(fields: "id") { | ||
id: ID! | ||
name: String | ||
username: String @shareable | ||
} | ||
`); | ||
|
||
const users = [ | ||
{ | ||
id: "1", | ||
name: "Ada Lovelace", | ||
birthDate: "1815-12-10", | ||
username: "@ada", | ||
}, | ||
{ | ||
id: "2", | ||
name: "Alan Turing", | ||
birthDate: "1912-06-23", | ||
username: "@complete", | ||
}, | ||
]; | ||
|
||
const resolvers = { | ||
Query: { | ||
me() { | ||
return users[0]; | ||
}, | ||
}, | ||
User: { | ||
__resolveReference(object) { | ||
return users.find((user) => user.id === object.id); | ||
}, | ||
}, | ||
}; | ||
|
||
async function startApolloServer(typeDefs, resolvers) { | ||
// Required logic for integrating with Express | ||
const app = express(); | ||
|
||
const limiter = rateLimit({ | ||
windowMs: 60 * 60 * 1000, // 1 hour | ||
max: rateLimitTreshold, | ||
}); | ||
|
||
const httpServer = http.createServer(app); | ||
|
||
const server = new ApolloServer({ | ||
schema: buildSubgraphSchema([ | ||
{ | ||
typeDefs, | ||
resolvers, | ||
}, | ||
]), | ||
plugins: [ApolloServerPluginDrainHttpServer({ httpServer })], | ||
}); | ||
|
||
await server.start(); | ||
app.use("/", cors(), json(), limiter, expressMiddleware(server)); | ||
|
||
// Modified server startup | ||
const port = process.env.PORT || 4001; | ||
|
||
await new Promise((resolve) => httpServer.listen({ port }, resolve)); | ||
console.log(`🚀 Accounts Server ready at http://localhost:${port}/`); | ||
} | ||
|
||
startApolloServer(typeDefs, resolvers); |
Oops, something went wrong.