Skip to content

Commit

Permalink
Merge pull request #295 from apollographql/igni/connectors_docker_exa…
Browse files Browse the repository at this point in the history
…mple

Connectors: docker example
  • Loading branch information
o0Ignition0o authored Dec 19, 2023
2 parents 316ab5b + e0e4172 commit 7414c5d
Show file tree
Hide file tree
Showing 18 changed files with 2,926 additions and 0 deletions.
22 changes: 22 additions & 0 deletions examples/connectors/README.md
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!
1 change: 1 addition & 0 deletions examples/connectors/accounts/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
11 changes: 11 additions & 0 deletions examples/connectors/accounts/Dockerfile
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" ]
91 changes: 91 additions & 0 deletions examples/connectors/accounts/index.js
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);
Loading

0 comments on commit 7414c5d

Please sign in to comment.