-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add wundergraph to compose graphql APIs (#64)
Co-authored-by: valia fetisov <[email protected]>
- Loading branch information
1 parent
d06fdb0
commit 5eb82a6
Showing
22 changed files
with
25,166 additions
and
12 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
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
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
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
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
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
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
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 |
---|---|---|
@@ -1,16 +1,29 @@ | ||
events {} | ||
|
||
http { | ||
log_format proxy_log '[$time_local] Proxy: "$proxy_host" "$upstream_addr" ' | ||
'$remote_addr - $remote_user "$host$request_uri" ' | ||
'$status $body_bytes_sent "$http_referer" ' | ||
'"$http_user_agent" "$http_x_forwarded_for"'; | ||
|
||
server { | ||
# frontend | ||
location / { | ||
proxy_pass http://frontend:3000; | ||
} | ||
|
||
# api | ||
location /backend/ { | ||
rewrite /backend/(.*) /$1 break; | ||
proxy_pass http://api:3000; | ||
proxy_set_header Host $host; | ||
} | ||
|
||
# graphql API composition | ||
location /wundergraph/graphql { | ||
rewrite /wundergraph/graphql /graphql break; | ||
proxy_pass http://wundergraph:3002; | ||
proxy_set_header Host $host; | ||
} | ||
} | ||
} |
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,2 @@ | ||
node_modules | ||
.wundergraph/generated |
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 @@ | ||
!/.wundergraph |
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,28 @@ | ||
{ | ||
// From: https://github.com/airbnb/javascript/issues/451 - we want airbnb, but not it's react rules (because we don't use react) | ||
"extends": [ | ||
"airbnb-base", | ||
"airbnb-typescript/base" | ||
], | ||
"parser": "@typescript-eslint/parser", | ||
"parserOptions": { | ||
"project": "./tsconfig.json" | ||
}, | ||
"ignorePatterns": ["**/generated/*"], | ||
"plugins": [ | ||
"@typescript-eslint" | ||
], | ||
"root": true, | ||
"rules": { | ||
"no-return-await": [ | ||
"error" | ||
], | ||
"import/prefer-default-export": "off", | ||
"no-console": [ | ||
"error" | ||
], | ||
"max-len": ["error", { "code": 120 }], | ||
"quotes": "off" | ||
} | ||
} | ||
|
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,2 @@ | ||
# wundergraph | ||
.wundergraph/generated |
Empty file.
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,57 @@ | ||
import { | ||
configureWunderGraphApplication, cors, introspect, EnvironmentVariable, LoggerLevel, | ||
} from '@wundergraph/sdk'; | ||
import dotenv from 'dotenv'; | ||
import server from './wundergraph.server'; | ||
import operations from './wundergraph.operations'; | ||
|
||
dotenv.config(); | ||
const ecosystemGqlEndpoint = process.env.ECOSYSTEM_GQL_ENDPOINT; | ||
if (!ecosystemGqlEndpoint) { | ||
throw new Error('ECOSYSTEM_GQL_ENDPOINT environment variable is not set'); | ||
} | ||
const switchboardGqlEndpoint = process.env.SWITCHBOARD_GQL_ENDPOINT || 'http://localhost:3001/graphql'; | ||
const allowedOrigins = (process.env.ALLOWED_ORIGINS || 'http://localhost:3001,http://localhost:3000').split(','); | ||
|
||
const ecosystem = introspect.graphql({ | ||
apiNamespace: 'ecosystem', | ||
url: ecosystemGqlEndpoint, | ||
introspection: { | ||
disableCache: true, | ||
}, | ||
}); | ||
|
||
const switchboard = introspect.graphql({ | ||
apiNamespace: '', | ||
url: switchboardGqlEndpoint, | ||
headers: (builder) => builder.addClientRequestHeader('Authorization', 'Authorization'), | ||
introspection: { | ||
disableCache: true, | ||
}, | ||
}); | ||
|
||
// configureWunderGraph emits the configuration | ||
configureWunderGraphApplication({ | ||
options: { | ||
listen: { | ||
host: new EnvironmentVariable('NODE_HOST', '0.0.0.0'), | ||
port: new EnvironmentVariable('NODE_PORT', '3002'), | ||
}, | ||
logger: { | ||
level: new EnvironmentVariable<LoggerLevel>('NODE_LOG_LEVEL', 'debug'), | ||
}, | ||
}, | ||
apis: [switchboard, ecosystem], | ||
server, | ||
operations, | ||
generate: { | ||
codeGenerators: [], | ||
}, | ||
cors: { | ||
...cors.allowAll, | ||
allowedOrigins, | ||
}, | ||
security: { | ||
enableGraphQLEndpoint: true, | ||
}, | ||
}); |
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,26 @@ | ||
import { configureWunderGraphOperations } from '@wundergraph/sdk'; | ||
import type { OperationsConfiguration } from './generated/wundergraph.operations'; | ||
|
||
export default configureWunderGraphOperations<OperationsConfiguration>({ | ||
operations: { | ||
defaultConfig: { | ||
authentication: { | ||
required: false, | ||
}, | ||
}, | ||
queries: (config) => ({ | ||
...config, | ||
liveQuery: { | ||
enable: true, | ||
pollingIntervalSeconds: 1, | ||
}, | ||
}), | ||
mutations: (config) => ({ | ||
...config, | ||
}), | ||
subscriptions: (config) => ({ | ||
...config, | ||
}), | ||
custom: {}, | ||
}, | ||
}); |
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,17 @@ | ||
import { configureWunderGraphServer, EnvironmentVariable, LoggerLevel } from '@wundergraph/sdk/server'; | ||
|
||
export default configureWunderGraphServer(() => ({ | ||
options: { | ||
listen: { | ||
host: new EnvironmentVariable('SERVER_HOST', '0.0.0.0'), | ||
port: new EnvironmentVariable('SERVER_PORT', '3003'), | ||
}, | ||
logger: { | ||
level: new EnvironmentVariable<LoggerLevel>('SERVER_LOG_LEVEL', 'debug'), | ||
}, | ||
}, | ||
hooks: { | ||
queries: {}, | ||
mutations: {}, | ||
}, | ||
})); |
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,23 @@ | ||
# see https://docs.docker.com/engine/reference/builder/#understand-how-arg-and-from-interact | ||
ARG NODE_VERSION=node:16.14.2 | ||
|
||
FROM $NODE_VERSION AS dependency-base | ||
|
||
# create destination directory | ||
RUN mkdir -p /app | ||
WORKDIR /app | ||
|
||
# copy the app, note .dockerignore | ||
COPY package.json . | ||
COPY package-lock.json . | ||
RUN npm ci | ||
|
||
FROM dependency-base AS production | ||
|
||
COPY . . | ||
|
||
# Run in production mode | ||
ENV NODE_ENV=production | ||
|
||
# initialize db and start the app | ||
CMD npm run build && 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,21 @@ | ||
# Wundergraph service | ||
|
||
The service to compose multiple graphql endpoints together. Documentation is of the underlying package is available under https://docs.wundergraph.com/. This service is intended to be run only in production to merge graphql endpoint produced by the `../api` service with [ecosystem-api](https://github.com/makerdao-ses/ecosystem-api) | ||
|
||
## Quick start | ||
|
||
1. Create env file with correct env variables (start with `cp example.env .env`) | ||
2. Make sure both services that should be composed are running, e.g.: | ||
- Run `npm run dev` in `../api` directory of this repo | ||
- Set correct `ECOSYSTEM_GQL_ENDPOINT` env variable in the file created above | ||
3. Run `npm run dev` inside `./wundergraph` directory | ||
4. Interact with graphql endpoint of wundergraph running at `http://localhost:3002/graphql` | ||
- E.g.: run `npm run dev` in `../frontend` directory of the project to be able to interact with newly created endpoint (you will need to set playground to connect to `http://localhost:3002/graphql`) | ||
|
||
## Environment variables | ||
|
||
- `ECOSYSTEM_GQL_ENDPOINT` (required): URL of the graphql endpoint that needs to be migrated (in our case that should be `https://ecosystem-dashboard.herokuapp.com/graphql`, but for testing we can use any other graphql endpoint such as `https://countries.trevorblades.com/graphql`). Pre-requirements for the endpoint are: | ||
- Enabled graphql introspection | ||
- Appropriate CORS settings | ||
- `SWITCHBOARD_GQL_ENDPOINT` (optional, default `http://localhost:3001/graphql`): URL of the switchboard graphql endpoint that is proxied transparently | ||
- `ALLOWED_ORIGINS` (optional, default `http://localhost:3001,http://localhost:3000`) |
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,3 @@ | ||
SWITCHBOARD_GQL_ENDPOINT="http://localhost:3001/" | ||
ECOSYSTEM_GQL_ENDPOINT="https://countries.trevorblades.com/graphql" | ||
ALLOWED_ORIGINS="http://localhost:3001,http://localhost:3000" |
Oops, something went wrong.