Skip to content

Commit d10a2b3

Browse files
committed
updated readme with link to documentation
1 parent 4a6c096 commit d10a2b3

File tree

1 file changed

+80
-35
lines changed

1 file changed

+80
-35
lines changed

README.md

Lines changed: 80 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ easily generate a GraphQL API from your sequelize models.
55

66
It's a very good fit for POCs and MVPs, while also scaling pretty well thanks to [dataloader-sequelize](https://github.com/mickhansen/dataloader-sequelize).
77

8-
## Manual
8+
---
99

10-
Get started with **[the online documentation](https://teamstarter.github.io/gsg-documentation/)**
10+
## Documentation
11+
The complete documentation car be found [here](https://teamstarter.github.io/gsg-documentation/)
1112

1213
## What can I do with GSG?
1314

@@ -23,54 +24,98 @@ The tools provided by this library will allow you to:
2324
- Add custom fields/resolvers on auto-generated types.
2425
- Easy integration with [dataloader-sequelize](https://github.com/mickhansen/dataloader-sequelize)
2526

27+
---
28+
2629
## Getting started
2730

28-
Add the lib and the peer dependencies:
31+
---
32+
33+
### Setting up the dependencies and the library
34+
35+
Add the lib and the peer dependencies of GraphQL-Sequelize-Generator:
2936

3037
```
31-
$ yarn add graphql-sequelize-generator graphql sequelize graphql-sequelize
38+
yarn add graphql-sequelize-generator graphql sequelize graphql-sequelize @apollo/server dataloader-sequelize graphql-relay ws
3239
```
3340

34-
⚠️ Caution: GSG requires at least Node v9.11.2 or greater as it is using async/await.
41+
⚠️ Caution: GSG requires Node v9.11.2 or greater as it is using async/await.
42+
43+
---
44+
45+
### Initializing the project with Sequelize-CLI and adding data to the database
3546

36-
Then you will be ready to add a GraphQL API to your express server with only a few lines of code:
47+
If you need to initialize the project, please follow this Sequelize documentation page : [Sequelize-Cli and Migrations](https://sequelize.org/docs/v6/other-topics/migrations/)
48+
49+
---
50+
51+
# Setting up your server
52+
53+
Create a file where you will set up your server and paste the following code. We used index.js (at the root of our example project):
3754

3855
```javascript
39-
import express from 'express'
40-
const {
41-
generateModelTypes,
42-
generateGraphqlExpressMiddleware,
43-
} = require('graphql-sequelize-generator')
44-
import models from './models'
45-
46-
const types = generateModelTypes(models)
47-
48-
graphqlSchemaDeclaration.user = {
49-
model: models.user,
50-
actions: ['list', 'create'],
51-
}
52-
53-
const server = generateApolloServer({
54-
graphqlSchemaDeclaration,
55-
types,
56-
models,
57-
})
58-
59-
const app = express()
60-
server.start()
61-
server.applyMiddleware({
62-
app,
63-
path: '/graphql',
64-
})
56+
// index.js
57+
const { expressMiddleware } = require("@apollo/server/express4");
58+
const express = require("express");
59+
const http = require("http");
60+
const cors = require("cors");
61+
const json = require("body-parser");
62+
const { createContext, EXPECTED_OPTIONS_KEY } = require("dataloader-sequelize");
63+
const setupServer = require("./schema");
64+
const models = require("./models"); //Assuming "models" is your import of the Sequelize models folder, initialized by Sequelize-Cli
65+
66+
const createServer = async (options = {}, globalPreCallback = () => null) => {
67+
const app = express();
68+
options = {
69+
spdy: { plain: true },
70+
...options,
71+
};
72+
const httpServer = http.createServer(options, app);
73+
const { server } = setupServer(globalPreCallback, httpServer);
74+
await server.start();
75+
//server.applyMiddleware({ app, path: '/graphql' })
76+
app.use(
77+
"/graphql",
78+
cors(),
79+
json(),
80+
expressMiddleware(server, {
81+
context: async ({ req, connection }) => {
82+
const contextDataloader = createContext(models.sequelize);
83+
84+
// Connection is provided when a webSocket is connected.
85+
if (connection) {
86+
// check connection for metadata
87+
return {
88+
...connection.context,
89+
[EXPECTED_OPTIONS_KEY]: contextDataloader,
90+
};
91+
}
92+
},
93+
})
94+
);
95+
96+
await new Promise((resolve) => {
97+
httpServer.listen(process.env.PORT || 8080, () => {
98+
resolve();
99+
});
100+
101+
console.log(
102+
`🚀 Server ready at http://localhost:${process.env.PORT || 8080}/graphql`
103+
);
104+
});
105+
return httpServer;
106+
};
107+
108+
const closeServer = async (server) => {
109+
await Promise.all([new Promise((resolve) => server.close(() => resolve()))]);
110+
};
111+
112+
createServer();
65113
```
66114

67-
<!-- A priori OK mais pas sûr -->
68-
69115
## Getting started with boilerplates
70116

71117
You can easily start a project with graphql-sequelize-generator using these boilerplates:
72118

73119
- In JavaScript : [GSG Boilerplate](https://github.com/teamstarter/gsg-boilerplate)
74120
- In TypeScript : [GSG Typescript Boilerplate](https://github.com/teamstarter/gsg-boilerplate-typescript)
75121

76-
<!-- OK -->

0 commit comments

Comments
 (0)