Skip to content

Commit

Permalink
Add Redis and schema hot reload. (#40)
Browse files Browse the repository at this point in the history
* Add Redis and update schema option

* Fix redis

* Fix duplicates in the schema

* remove comments and rename things

* rename things

* add port

* Issue with update-schema

* Fix issue with the update of the schema

* Fix EOF and update locks
  • Loading branch information
MichelDiz authored Oct 12, 2023
1 parent 2611112 commit d871c2b
Show file tree
Hide file tree
Showing 13 changed files with 3,589 additions and 3,061 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"type": "node-terminal",
"name": "Debug via file (ts-node)",
"request": "launch",
"command": "ts-node -- ${workspaceFolder}/index.ts",
"command": "NODE_ENV=debug ts-node -- ${workspaceFolder}/index.ts",
"cwd": "${fileDirname}",
"internalConsoleOptions": "openOnSessionStart",
"skipFiles": [
Expand Down
6 changes: 6 additions & 0 deletions docker-compose.test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ services:
build: .
ports:
- "4001:4001"
- "4004:4004"
command: yarn startTestServer
environment:
- RUNNING_JEST=true
depends_on:
- dgraph-alpha
- redis
test-api:
build: .
command: /bin/sh -c "sleep 10 && yarn test"
Expand All @@ -30,3 +32,7 @@ services:
command: dgraph alpha --my dgraph-alpha:7080 --zero dgraph-zero:5080 --security "whitelist=0.0.0.0/0"
depends_on:
- dgraph-zero
redis:
image: redis:latest
ports:
- "6379:6379"
69 changes: 57 additions & 12 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,73 @@
import express from 'express';
import fs from 'fs';
import { buildSchema } from 'graphql';
import { ApolloServer, gql } from 'apollo-server-express';

import resolvers from './src/resolvers/index.ts';
import schemaDefinition from './src/schema/schemaAST.ts';
import schemaStore from './src/stores/schemaStore.ts';

import http from 'http';

let apolloServer: any;
let httpServer: any;

const defs = schemaDefinition();
const app = express();
app.use(express.json());

app.post('/update-schema', async (req, res) => {
let newSchemaString = req.body.schema;

if (!newSchemaString) {
return res.status(400).send('No Schema provided');
}
try {
buildSchema(newSchemaString);
} catch (error) {
console.error('Invalid schema:', error);
return res.status(400).send(`Invalid schema: ${error}`);
}
if (
process.env.RUNNING_JEST === 'true' ||
process.env.NODE_ENV === 'test' ||
process.env.NODE_ENV === 'debug'
) {
newSchemaString = fs.readFileSync(__dirname.concat('/src/schema/schema.graphql'), 'utf8');
}
httpServer?.close();
httpServer.removeAllListeners('request');

const typeDefs = gql`
${defs[0]}
`;
await schemaStore.setState({ state: newSchemaString });

const _resolvers = resolvers(defs[1])[0];
await startServer();

const server = new ApolloServer({ typeDefs, resolvers: _resolvers });
res.send({ status: 'success' });
});

async function startServer() {
await server.start();
server.applyMiddleware({ app, cors: false });
const port = 4001;
const app2 = express();

app.listen(port, () => {
console.log(`🚀 Server ready at http://localhost:${port}${server.graphqlPath}`),
console.log('Use postman to test the API');
const defs = await schemaDefinition();
const typeDefs = gql`
${defs[0]}
`;
const _resolvers = resolvers(defs[1])[0];

apolloServer = new ApolloServer({ typeDefs, resolvers: _resolvers });
await apolloServer.start();

apolloServer.applyMiddleware({ app: app2, cors: false, path: '/graphql' });

httpServer = http.createServer(app2);
httpServer.listen(4004, () => {
console.log(`🚀 Server ready at http://localhost:4004${apolloServer.graphqlPath}`);
});
}

const port = 4001;
app.listen(port, () => {
console.log(`🚀 Server ready at http://localhost:${port}`),
console.log('Use postman to test the API');
});

startServer().catch(err => console.error(err));
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module.exports = {
transform: {
'^.+\\.tsx?$': 'ts-jest',
},
// globalSetup: '<rootDir>/src/__tests__/setup.ts',
globalSetup: '<rootDir>/src/__tests__/setup.ts',
// globalTeardown: '<rootDir>/src/__tests__/teardown.ts',
testPathIgnorePatterns: ["<rootDir>/src/__tests__/setup.ts", "<rootDir>/src/__tests__/teardown.ts"],
};
Loading

0 comments on commit d871c2b

Please sign in to comment.