-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Redis and schema hot reload. (#40)
* 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
Showing
13 changed files
with
3,589 additions
and
3,061 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
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)); |
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
Oops, something went wrong.