Skip to content

Commit

Permalink
Merge pull request #321 from the-hideout/graphql-yoga
Browse files Browse the repository at this point in the history
Graphql yoga
  • Loading branch information
Razzmatazzz authored Aug 16, 2024
2 parents b6e54dc + e1fb537 commit 745ffb4
Show file tree
Hide file tree
Showing 44 changed files with 3,368 additions and 2,881 deletions.
2 changes: 1 addition & 1 deletion .node-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
18.16.0
20.11.0
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,4 @@ There's also an http webserver in the /http folder. It can be run with `npm run
- CACHE_BASIC_AUTH (used for caching)
- ENVIRONMENT (either `production` or `dev`; determines which KVs are read)
- PORT (defaults to 8088)
- WORKERS (defaults to # of cpus - 1; determines how many worker threads are created to respond to requests)
109 changes: 0 additions & 109 deletions custom-endpoints/nightbot.mjs

This file was deleted.

27 changes: 0 additions & 27 deletions custom-endpoints/twitch.mjs

This file was deleted.

1 change: 1 addition & 0 deletions datasources/crafts.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import WorkerKV from '../utils/worker-kv.mjs';
class CraftsAPI extends WorkerKV {
constructor(dataSource) {
super('craft_data', dataSource);
this.gameModes.push('pve');
}

async getList(context, info) {
Expand Down
1 change: 1 addition & 0 deletions datasources/hideout.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import WorkerKV from '../utils/worker-kv.mjs';
class HideoutAPI extends WorkerKV {
constructor(dataSource) {
super('hideout_data', dataSource);
this.gameModes.push('pve');
}

async getList(context, info) {
Expand Down
48 changes: 22 additions & 26 deletions datasources/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,25 @@ import TradersAPI from './traders.mjs';
class DataSource {
constructor(env) {
this.env = env;
this.barter = new BartersAPI(this);
this.craft = new CraftsAPI(this);
this.hideout = new HideoutAPI(this);
this.historicalPrice = new HistoricalPricesAPI(this);
this.archivedPrice = new ArchivedPricesAPI(this);
this.item = new ItemsAPI(this);
this.map = new MapAPI(this);
this.schema = new SchemaAPI(this);
this.status = new StatusAPI(this);
this.traderInventory = new TraderInventoryAPI(this);
this.trader = new TradersAPI(this);
this.task = new TasksAPI(this);

this.initialized = false;
this.loading = false;
this.requests = {};
this.kvLoaded = [];

this.kvWorkers = {
barter: this.barter,
craft: this.craft,
hideout: this.hideout,
historicalPrice: this.historicalPrice,
archivedPrice: this.archivedPrice,
item: this.item,
map: this.map,
schema: this.schema,
task: this.task,
trader: this.trader,
traderInventory: this.traderInventory,
this.worker = {
barter: new BartersAPI(this),
craft: new CraftsAPI(this),
hideout: new HideoutAPI(this),
historicalPrice: new HistoricalPricesAPI(this),
archivedPrice: new ArchivedPricesAPI(this),
item: new ItemsAPI(this),
map: new MapAPI(this),
schema: new SchemaAPI(this),
status: new StatusAPI(this),
task: new TasksAPI(this),
trader: new TradersAPI(this),
traderInventory: new TraderInventoryAPI(this),
};
}

Expand Down Expand Up @@ -94,7 +83,7 @@ class DataSource {
}
let lowestExpire = Number.MAX_SAFE_INTEGER;
let schemaExpire = Number.MAX_SAFE_INTEGER;
for (const worker of Object.values(this.kvWorkers)) {
for (const worker of Object.values(this.worker)) {
if (!this.requests[requestId].kvUsed.includes(worker.kvName)) {
continue;
}
Expand All @@ -116,9 +105,16 @@ class DataSource {
if (ttl <= 0) {
ttl = 0;
}
ttl = Math.max(ttl, 60);
ttl = Math.max(ttl, 60 * 5);
return ttl;
}

clearRequestData(requestId) {
delete this.requests[requestId];
for (const worker of Object.values(this.worker)) {
worker.clearRequestCache(requestId);
}
}
}

export default DataSource;
60 changes: 60 additions & 0 deletions graphql-yoga.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { createYoga, useExecutionCancellation } from 'graphql-yoga'
import { v4 as uuidv4 } from 'uuid';

import DataSource from './datasources/index.mjs';
import schema from './schema.mjs';
import graphqlUtil from './utils/graphql-util.mjs';
import graphQLOptions from './utils/graphql-options.mjs';

import useRequestTimer from './plugins/plugin-request-timer.mjs';
import useHttpServer from './plugins/plugin-http-server.mjs';
import useCacheMachine from './plugins/plugin-use-cache-machine.mjs';
import useTwitch from './plugins/plugin-twitch.mjs';
import useNightbot from './plugins/plugin-nightbot.mjs';
import usePlayground from './plugins/plugin-playground.mjs';
import useOptionMethod from './plugins/plugin-option-method.mjs';

let dataAPI, yoga;

export default async function getYoga(env) {
if (!dataAPI) {
dataAPI = new DataSource(env);
}
if (yoga) {
dataAPI.env = env;
return yoga;
}
yoga = createYoga({
schema: (context) => {
// this context only has the env vars present on creation
context.request.requestId = uuidv4();
if (env.ctx) {
context.request.ctx = env.ctx;
}
if (context.ctx) {
context.request.ctx = context.ctx;
}
return schema(dataAPI, graphqlUtil.getDefaultContext(dataAPI, context.request.requestId));
},
context: async ({request, params}) => {
return graphqlUtil.getDefaultContext(dataAPI, request.requestId);
},
plugins: [
//useExecutionCancellation(),
useRequestTimer(),
useOptionMethod(),
useTwitch(env),
usePlayground(),
useNightbot(env),
useHttpServer(env),
useCacheMachine(env),
],
cors: {
origin: graphQLOptions.cors.allowOrigin,
credentials: true,
allowedHeaders: ['Content-Type'],
methods: graphQLOptions.cors.allowMethods.split(', '),
},
});
return yoga;
}
4 changes: 3 additions & 1 deletion handlers/graphiql.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@


const defaultQuery = `# Welcome to the Tarkov.dev API Playground
#
# Type queries into this side of the screen, click the Execute query
Expand Down Expand Up @@ -114,6 +116,6 @@ const html = baseEndpoint => `
`

const headers = { 'Content-Type': 'text/html' }
const handler = (request, { baseEndpoint }) => new Response(html(baseEndpoint), { headers });
const handler = ({ baseEndpoint }) => new Response(html(baseEndpoint), { headers });

export default handler;
Loading

0 comments on commit 745ffb4

Please sign in to comment.