Skip to content

Commit

Permalink
Switch to swc build, fix eslint config, swiitch to using account-gql …
Browse files Browse the repository at this point in the history
…server
  • Loading branch information
Nexite committed Mar 30, 2023
1 parent a339c1b commit 832e217
Show file tree
Hide file tree
Showing 12 changed files with 760 additions and 2,676 deletions.
7 changes: 5 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
module.exports = {
extends: '@codeday',

parserOptions:
{
ecmaVersion: 2020,
},
rules: {
'import/no-missing-require': ['off'],
'import/no-unresolved': ['off'],
'require-jsdoc': ['off'],
'import/prefer-default-export': ['off'],
}
},
};
6 changes: 6 additions & 0 deletions .swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"$schema": "http://json.schemastore.org/swcrc",
"module": {
"type": "commonjs"
}
}
23 changes: 15 additions & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
FROM node:13-alpine
FROM node:16-alpine as builder

RUN mkdir /app
WORKDIR /app
RUN mkdir /build
WORKDIR /build

COPY package.json /app
COPY yarn.lock /app
COPY package.json /build
COPY yarn.lock /build
RUN yarn install --frozen-lockfile

COPY . /app
RUN rm /app/node_modules/@graphql-tools/delegate/index.cjs.js
COPY index.cjs.js /app/node_modules/@graphql-tools/delegate/index.cjs.js
# RUN rm /app/node_modules/@graphql-tools/delegate/index.cjs.js
# COPY index.cjs.js /app/node_modules/@graphql-tools/delegate/index.cjs.js

RUN yarn run build
COPY dist/ /build/dist

FROM node:16-alpine
RUN mkdir /app
WORKDIR /app

COPY --from=builder /build/ /app/

CMD yarn run start
16 changes: 7 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
"name": "@srnd/gql-server",
"version": "0.1.0",
"description": "GraphQL interface to SRND services.",
"main": "dist/index.js",
"devDependencies": {
"@babel/core": "^7.3.3",
"@babel/polyfill": "^7.4.3",
"@codeday/eslint-config": "^1.3.0",
"eslint": "^7.3.1",
"nodemon": "^2.0.4"
"nodemon": "^2.0.4",
"prettier": "^2.8.7"
},
"scripts": {
"build": "babel src -d dist --copy-files",
"build": "swc src -d dist --copy-files",
"start": "node dist/index.js",
"dev-once": "yarn run build && node dist/index.js",
"dev": "nodemon --watch src --exec yarn run dev-once"
Expand All @@ -29,10 +31,6 @@
"homepage": "https://github.com/SRND/Topo#readme",
"dependencies": {
"@apollo/client": "^3.2.9",
"@babel/cli": "^7.2.3",
"@babel/core": "^7.3.3",
"@babel/polyfill": "^7.4.3",
"@babel/preset-env": "^7.3.1",
"@codeday/uploader-node": "^1.0.1",
"@graphql-tools/delegate": "7.0.7",
"@graphql-tools/links": "^7.0.3",
Expand All @@ -59,7 +57,7 @@
"dotenv": "^8.2.0",
"emoji-strip": "^1.0.1",
"express": "^4.16.4",
"extract-files": "^9.0.0",
"extract-files": "^10.0.0",
"form-data": "^3.0.0",
"graphql": "15.3.0",
"graphql-upload": "^11.0.0",
Expand All @@ -70,9 +68,9 @@
"map-obj": "^4.1.0",
"node-fetch": "^2.6.0",
"phone": "^2.4.19",
"prettier": "^1.16.4",
"pretty-quick": "^1.10.0",
"subscriptions-transport-ws": "^0.9.18",
"swc": "^1.0.11",
"syslog-client": "^1.1.1",
"ws": "^7.4.6"
}
Expand Down
9 changes: 5 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable global-require */
require('@babel/polyfill');
require('dotenv').config();
require('./server.js').default();
import { config } from "dotenv";
config();

import server from "./server.js";
server();
48 changes: 28 additions & 20 deletions src/remoteTransport.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { print } from 'graphql';
import FormData from 'form-data';
import { observableToAsyncIterable } from '@graphql-tools/utils';
import { createClient } from 'graphql-ws';
import extractFiles from 'extract-files/public/extractFiles';
import { extractFiles } from 'extract-files';

//
// HTTP requests
Expand All @@ -12,9 +12,13 @@ import extractFiles from 'extract-files/public/extractFiles';
function makeExecutor(httpEndpoint, options) {
return async function executor({ document, variables, context }) {
const allowedHeaders = Object.keys(context?.headers || {})
.filter((name) => name.toLowerCase().substr(0, 2) === 'x-')
.filter(
(name) => name.toLowerCase().substr(0, 2) === 'x-'
// check if the header is allowed to be forwarded
|| options?.forwardHeaders?.map((header) => header.toLowerCase()).includes(name)
)
.reduce((accum, name) => ({ ...accum, [name]: context?.headers[name] }), {});
// .filter((name) => name.toLowerCase().startsWith(prefix))
// .filter((name) => name.toLowerCase().startsWith(prefix))

const query = print(document);
let clone = variables;
Expand All @@ -27,12 +31,15 @@ function makeExecutor(httpEndpoint, options) {

// Handles detecting if file uploads are part of the request, and extracting them
if (variables) {
const awaitedVariables = (await Promise.all(
Object.keys(variables)
.map(async (key) => [key, await variables[key]])
)).reduce((accum, [k, v]) => ({ ...accum, [k]: v }), {});

({ clone, files } = extractFiles(awaitedVariables, 'variables', (v) => !!v?.createReadStream));
const awaitedVariables = (
await Promise.all(Object.keys(variables).map(async (key) => [key, await variables[key]]))
).reduce((accum, [k, v]) => ({ ...accum, [k]: v }), {});

({ clone, files } = extractFiles(
awaitedVariables,
'variables',
(v) => !!v?.createReadStream
));
}

// Handles generating a multi-part request
Expand All @@ -47,24 +54,26 @@ function makeExecutor(httpEndpoint, options) {
// TODO(@tylermenezes): In theory, form-data and fetch support appending f.createReadStream() directly, but
// in practice it doesn't seem to work. Processing uploads this way will work, but takes up a lot more memory
// than necessary
await Promise.all(filesArr.map(async (f, i) => {
const chunks = []
for await (let chunk of f.createReadStream()) {
chunks.push(chunk);
}
form.append(i, Buffer.concat(chunks), f.filename);
}));
await Promise.all(
filesArr.map(async (f, i) => {
const chunks = [];
for await (const chunk of f.createReadStream()) {
chunks.push(chunk);
}
form.append(i, Buffer.concat(chunks), f.filename);
})
);

headers = {
...form.getHeaders(),
...headers,
};
body = form;

// Handles simple requests
// Handles simple requests
} else {
headers['Content-Type'] = 'application/json';
body = JSON.stringify({ query, variables: clone })
body = JSON.stringify({ query, variables: clone });
}

// Do the thing!
Expand All @@ -76,7 +85,7 @@ function makeExecutor(httpEndpoint, options) {
const result = fetchResult.json();
if (options?.debug) console.log(JSON.stringify({ body, result }));
return result;
}
};
}

//
Expand Down Expand Up @@ -123,7 +132,6 @@ function makeSubscriber(wsEndpoint, options) {
}

export default function makeRemoteTransport(httpEndpoint, wsEndpoint, options) {

return {
executor: makeExecutor(httpEndpoint, options?.executor),
subscriber: wsEndpoint ? makeSubscriber(wsEndpoint, options?.subscriber) : undefined,
Expand Down
4 changes: 3 additions & 1 deletion src/remotes/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ function getConnectionResolvers(prefix, schemas) {

export default async function createAccountSchema(uri, wsUri) {
console.log(` * account(${uri})`);
const { executor, subscriber } = makeRemoteTransport(uri, wsUri, "");
const { executor, subscriber } = makeRemoteTransport(uri, wsUri, {
executor: { forwardHeaders: ["Authorization", "Account-Authorization"] },
});
const schema = wrapSchema({
schema: await introspectSchema(executor),
executor,
Expand Down
Loading

0 comments on commit 832e217

Please sign in to comment.