Skip to content

Instantly share code, notes, and snippets.

@kkemple
Created May 26, 2021 17:53
Show Gist options
  • Save kkemple/6492ef18c40a857ca205e94d6cf385d5 to your computer and use it in GitHub Desktop.
Save kkemple/6492ef18c40a857ca205e94d6cf385d5 to your computer and use it in GitHub Desktop.
Snippets for setting up federated GraphQL APIs
# This is a basic workflow to help you get started with Actions
name: Validate Schema
# Controls when the action will run. Triggers the workflow on push or pull request
# events but only for the noragrets branch
on:
pull_request:
types: [opened, reopened, synchronize]
paths:
- "service/*"
workflow_dispatch:
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build"
build:
# The type of runner that the job will run on
runs-on: ubuntu-latest
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2
# Runs a set of commands using the runners shell
- name: Check schema against registry
working-directory: ./website-service
env:
APOLLO_KEY: ${{ secrets.APOLLO_KEY }}
run: |
npx -p @apollo/rover rover subgraph check the-worst-dev-z1n1vn@current \
--name=service \
--schema=./schema.graphql
FROM node:14
# Create app directory
WORKDIR /usr/src/app
COPY package*.json ./
# Install app dependencies
RUN npm install
COPY . .
EXPOSE 4001
CMD [ "npm", "start" ]
# fly.toml file generated for twd-twitch-service on 2021-02-13T13:46:44-05:00
app = "app-gateway"
[build]
builtin = "node"
kill_signal = "SIGINT"
kill_timeout = 5
[[services]]
internal_port = 6969
protocol = "tcp"
[services.concurrency]
hard_limit = 25
soft_limit = 20
[[services.ports]]
handlers = ["http"]
port = "80"
[[services.ports]]
handlers = ["tls", "http"]
port = "443"
[[services.tcp_checks]]
grace_period = "1s"
interval = "10s"
port = "6969"
restart_limit = 5
timeout = "2s"
const { ApolloGateway } = require("@apollo/gateway");
const { ApolloServer } = require("apollo-server");
/* Apollo */
const gateway = new ApolloGateway();
const server = new ApolloServer({ gateway, subscriptions: false });
server.listen(6969).then(({ url }) => {
console.log(`🚀 Gateway API running at ${url}`);
});
# This is a basic workflow to help you get started with Actions
name: Deploy Service and Register Schema
# Controls when the action will run. Triggers the workflow on push or pull request
# events but only for the noragrets branch
on:
push:
branches: [noragrets]
paths:
- "service/*"
workflow_dispatch:
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build"
build:
# The type of runner that the job will run on
runs-on: ubuntu-latest
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2
- name: Deploy to fly
uses: superfly/flyctl-actions@master
env:
FLY_PROJECT_PATH: ./twitch-service
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}
with:
args: "deploy"
# Runs a set of commands using the runners shell
- name: Push schema to registry
working-directory: ./service
env:
APOLLO_KEY: ${{ secrets.APOLLO_KEY }}
run: |
npx -p @apollo/rover rover subgraph push the-worst-dev-z1n1vn@current \
--name=twitch \
--schema=./schema.graphql \
--routing-url=${{ secrets.SERVICE_URL }}
const { ApolloServer, SchemaDirectiveVisitor, gql } = require("apollo-server");
const { buildFederatedSchema } = require("@apollo/federation");
const fs = require("fs");
const typeDefs = fs.readFileSync("./schema.graphql", "utf8").toString();
const resolvers = {
Query: {},
};
// build the federated schema
const schema = buildFederatedSchema([{ typeDefs, resolvers }]);
// create the Apollo server and disable subscriptions
const server = new ApolloServer({
schema,
subscriptions: false,
});
// start up the server
server.listen(4001, async () => {
console.log(`Server is now running on http://localhost:4001`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment