Skip to content

Commit f6cd668

Browse files
Merge pull request #70 from SGO34/upgrade-gsg-Steve
Edited README with new Apollo server instructions and link to documentation
2 parents c919224 + d10a2b3 commit f6cd668

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+1463
-1382
lines changed

.github/workflows/nodejs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jobs:
88

99
strategy:
1010
matrix:
11-
node-version: [12.18.1]
11+
node-version: [16.14.0]
1212

1313
steps:
1414
- uses: actions/checkout@v1

.nvmrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
10.15.0
1+
16.14.0

CHANGELOG.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,25 @@
22

33
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
44

5-
## [7.5.0](https://github.com/teamstarter/graphql-sequelize-generator/compare/v7.4.1...v7.5.0) (2021-10-19)
6-
5+
### [7.5.1](https://github.com/teamstarter/graphql-sequelize-generator/compare/v7.5.0...v7.5.1) (2022-01-20)
76

87
### Features
98

10-
* **count:** Allow the count to have extra args. Use the ones from list if not specified. ([5ad2d81](https://github.com/teamstarter/graphql-sequelize-generator/commit/5ad2d81))
11-
* **count-extra-arguments:** added the abbility to add an extra argum ([f128137](https://github.com/teamstarter/graphql-sequelize-generator/commit/f128137))
12-
* **count-extra-arguments:** added the abbility to add an extra argument to the count query ([5c24eba](https://github.com/teamstarter/graphql-sequelize-generator/commit/5c24eba))
9+
- **webhooks:** Make it easier to use graphql-web-hooks with GSG.
10+
11+
## [7.5.0](https://github.com/teamstarter/graphql-sequelize-generator/compare/v7.4.1...v7.5.0) (2021-10-19)
1312

13+
### Features
1414

15+
- **count:** Allow the count to have extra args. Use the ones from list if not specified. ([5ad2d81](https://github.com/teamstarter/graphql-sequelize-generator/commit/5ad2d81))
16+
- **count-extra-arguments:** added the abbility to add an extra argum ([f128137](https://github.com/teamstarter/graphql-sequelize-generator/commit/f128137))
17+
- **count-extra-arguments:** added the abbility to add an extra argument to the count query ([5c24eba](https://github.com/teamstarter/graphql-sequelize-generator/commit/5c24eba))
1518

1619
### [7.4.1](https://github.com/teamstarter/graphql-sequelize-generator/compare/v7.4.0...v7.4.1) (2021-09-21)
1720

18-
1921
### Bug Fixes
2022

21-
* **parent-obejct-attributes:** Fix an issue with HasOne relations that failed to be fetched. ([ce28b57](https://github.com/teamstarter/graphql-sequelize-generator/commit/ce28b57))
22-
23-
23+
- **parent-obejct-attributes:** Fix an issue with HasOne relations that failed to be fetched. ([ce28b57](https://github.com/teamstarter/graphql-sequelize-generator/commit/ce28b57))
2424

2525
## [7.4.0](https://github.com/teamstarter/graphql-sequelize-generator/compare/v7.3.5...v7.4.0) (2021-08-10)
2626

README.md

Lines changed: 81 additions & 31 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,44 +24,92 @@ 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.applyMiddleware({
61-
app,
62-
path: '/graphql'
63-
})
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();
64113
```
65114

66115
## Getting started with boilerplates
@@ -69,3 +118,4 @@ You can easily start a project with graphql-sequelize-generator using these boil
69118

70119
- In JavaScript : [GSG Boilerplate](https://github.com/teamstarter/gsg-boilerplate)
71120
- In TypeScript : [GSG Typescript Boilerplate](https://github.com/teamstarter/gsg-boilerplate-typescript)
121+

lib/associations/field.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ function generateAssociationField(relation, types, graphqlSchemaDeclaration, mod
77
var newBaseType = graphqlSchemaDeclaration &&
88
// @ts-ignore
99
!types[relation.target.name].associationsInjected
10-
? __1.injectAssociations(types[relation.target.name], graphqlSchemaDeclaration, types, models, globalPreCallback)
10+
? (0, __1.injectAssociations)(types[relation.target.name], graphqlSchemaDeclaration, types, models, globalPreCallback)
1111
: types[relation.target.name];
1212
var type = relation.associationType === 'BelongsToMany' ||
1313
relation.associationType === 'HasMany'
@@ -23,7 +23,7 @@ function generateAssociationField(relation, types, graphqlSchemaDeclaration, mod
2323
// An arg with the key order will automatically be converted to a order on the target
2424
name: 'order',
2525
type: graphql_1.GraphQLString
26-
}
26+
},
2727
]
2828
};
2929
if (relation.associationType === 'HasMany') {

lib/associations/fields.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var field_1 = __importDefault(require("./field"));
1616
function generateAssociationsFields(associations, types) {
1717
var fields = {};
1818
for (var associationName in associations) {
19-
fields[associationName] = field_1["default"](associations[associationName], types);
19+
fields[associationName] = (0, field_1["default"])(associations[associationName], types);
2020
}
2121
return fields;
2222
}

lib/associations/inject.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ var debug_1 = __importDefault(require("debug"));
1818
var graphql_sequelize_1 = require("graphql-sequelize");
1919
var createResolver_1 = __importDefault(require("../createResolver"));
2020
var field_1 = __importDefault(require("./field"));
21-
var debug = debug_1["default"]('gsg');
21+
var debug = (0, debug_1["default"])('gsg');
2222
function injectAssociations(modelGraphQLType, graphqlSchemaDeclaration, outputTypes, models, globalPreCallback, proxyModelName) {
2323
if (proxyModelName === void 0) { proxyModelName = null; }
2424
var modelName = proxyModelName || modelGraphQLType.name;
@@ -34,18 +34,18 @@ function injectAssociations(modelGraphQLType, graphqlSchemaDeclaration, outputTy
3434
var associationsFields = {};
3535
for (var associationName in associations) {
3636
if (!graphqlSchemaDeclaration[associations[associationName].target.name]) {
37-
debug("Cannot generate the association for model [" + associations[associationName].target.name + "] as it wasn't declared in the schema declaration. Skipping it.");
37+
debug("Cannot generate the association for model [".concat(associations[associationName].target.name, "] as it wasn't declared in the schema declaration. Skipping it."));
3838
continue;
3939
}
40-
associationsFields[associationName] = field_1["default"](associations[associationName], outputTypes, graphqlSchemaDeclaration, models, globalPreCallback, createResolver_1["default"](graphqlSchemaDeclaration[associations[associationName].target.name], models, globalPreCallback, associations[associationName]));
40+
associationsFields[associationName] = (0, field_1["default"])(associations[associationName], outputTypes, graphqlSchemaDeclaration, models, globalPreCallback, (0, createResolver_1["default"])(graphqlSchemaDeclaration[associations[associationName].target.name], models, globalPreCallback, associations[associationName]));
4141
}
4242
// We have to mutate the original field, as type names must be unique
4343
// We cannot return a new type as the type may have already been used
4444
// In previous models.
4545
var baseFields = {};
4646
var modelDefinition = graphqlSchemaDeclaration[modelName];
4747
if (typeof modelDefinition !== 'undefined') {
48-
baseFields = graphql_sequelize_1.attributeFields(modelDefinition.model, {
48+
baseFields = (0, graphql_sequelize_1.attributeFields)(modelDefinition.model, {
4949
allowNull: false,
5050
exclude: modelDefinition.excludeFields,
5151
commentToDescription: true

lib/createResolver.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
2525
function verb(n) { return function (v) { return step([n, v]); }; }
2626
function step(op) {
2727
if (f) throw new TypeError("Generator is already executing.");
28-
while (_) try {
28+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
2929
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
3030
if (y = 0, t) op = [op[0] & 2, t.value];
3131
switch (op[0]) {
@@ -90,7 +90,7 @@ function allowOrderOnAssociations(findOptions, model) {
9090
if (parts.length === 2) {
9191
var associationName = parts[0];
9292
if (typeof model.associations[associationName] === 'undefined') {
93-
throw new Error("Association " + associationName + " unknown on model " + model.name + " order");
93+
throw new Error("Association ".concat(associationName, " unknown on model ").concat(model.name, " order"));
9494
}
9595
if (typeof findOptions.include === 'undefined') {
9696
findOptions.include = [];
@@ -191,7 +191,7 @@ function createResolver(graphqlTypeDeclaration, models, globalPreCallback, relat
191191
var listAfter = graphqlTypeDeclaration.list && graphqlTypeDeclaration.list.after
192192
? graphqlTypeDeclaration.list.after
193193
: undefined;
194-
return graphql_sequelize_1.resolver(relation || graphqlTypeDeclaration.model, {
194+
return (0, graphql_sequelize_1.resolver)(relation || graphqlTypeDeclaration.model, {
195195
contextToOptions: graphqlTypeDeclaration.list
196196
? graphqlTypeDeclaration.list.contextToOptions
197197
: undefined,
@@ -263,11 +263,11 @@ function createResolver(graphqlTypeDeclaration, models, globalPreCallback, relat
263263
return [2 /*return*/, graphqlTypeDeclaration.list &&
264264
graphqlTypeDeclaration.list.removeUnusedAttributes === false
265265
? result
266-
: removeUnusedAttributes_1["default"](result, info, graphqlTypeDeclaration.model, models)];
266+
: (0, removeUnusedAttributes_1["default"])(result, info, graphqlTypeDeclaration.model, models)];
267267
case 10: return [2 /*return*/, graphqlTypeDeclaration.list &&
268268
graphqlTypeDeclaration.list.removeUnusedAttributes === false
269269
? processedFindOptions
270-
: removeUnusedAttributes_1["default"](processedFindOptions, info, graphqlTypeDeclaration.model, models)];
270+
: (0, removeUnusedAttributes_1["default"])(processedFindOptions, info, graphqlTypeDeclaration.model, models)];
271271
}
272272
});
273273
}); },

lib/generateApolloServer.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
1515
};
1616
exports.__esModule = true;
1717
var graphql_1 = require("graphql");
18-
var apollo_server_express_1 = require("apollo-server-express");
18+
var server_1 = require("@apollo/server");
19+
var disabled_1 = require("@apollo/server/plugin/disabled");
20+
var ws_1 = require("graphql-ws/lib/use/ws");
1921
var schema_1 = __importDefault(require("./schema"));
2022
function generateApolloServer(_a) {
21-
var graphqlSchemaDeclaration = _a.graphqlSchemaDeclaration, customMutations = _a.customMutations, types = _a.types, models = _a.models, _b = _a.apolloServerOptions, apolloServerOptions = _b === void 0 ? {} : _b, _c = _a.pubSubInstance, pubSubInstance = _c === void 0 ? null : _c, _d = _a.callWebhook, callWebhook = _d === void 0 ? function () { return null; } : _d, _e = _a.globalPreCallback, globalPreCallback = _e === void 0 ? function () { return null; } : _e;
22-
var graphqlSchema = new graphql_1.GraphQLSchema(schema_1["default"]({
23+
var graphqlSchemaDeclaration = _a.graphqlSchemaDeclaration, customMutations = _a.customMutations, types = _a.types, models = _a.models, _b = _a.apolloServerOptions, apolloServerOptions = _b === void 0 ? {} : _b, _c = _a.pubSubInstance, pubSubInstance = _c === void 0 ? null : _c, _d = _a.callWebhook, callWebhook = _d === void 0 ? function () { return null; } : _d, _e = _a.wsServer, wsServer = _e === void 0 ? null : _e, _f = _a.globalPreCallback, globalPreCallback = _f === void 0 ? function () { return null; } : _f;
24+
var graphqlSchema = new graphql_1.GraphQLSchema((0, schema_1["default"])({
2325
graphqlSchemaDeclaration: graphqlSchemaDeclaration,
2426
customMutations: customMutations,
2527
types: types,
@@ -28,6 +30,11 @@ function generateApolloServer(_a) {
2830
pubSubInstance: pubSubInstance,
2931
callWebhook: callWebhook
3032
}));
31-
return new apollo_server_express_1.ApolloServer(__assign({ schema: graphqlSchema, cacheControl: false }, apolloServerOptions));
33+
// Hand in the schema we just created and have the
34+
// WebSocketServer start listening.
35+
if (wsServer) {
36+
(0, ws_1.useServer)({ schema: graphqlSchema }, wsServer);
37+
}
38+
return new server_1.ApolloServer(__assign({ schema: graphqlSchema, plugins: [(0, disabled_1.ApolloServerPluginCacheControlDisabled)()] }, apolloServerOptions));
3239
}
3340
exports["default"] = generateApolloServer;

lib/generateTypes/graphQLType.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ function generateGraphQLType(model, types, isInput) {
2929
if (isInput === void 0) { isInput = false; }
3030
var GraphQLClass = isInput ? GraphQLInputObjectType : GraphQLObjectType;
3131
var type = new GraphQLClass({
32-
name: isInput ? model.name + "Input" : model.name,
32+
name: isInput ? "".concat(model.name, "Input") : model.name,
3333
fields: function () { return (__assign(__assign({}, attributeFields(model, {
3434
allowNull: !!isInput,
3535
commentToDescription: true
36-
})), (isInput ? fields_1["default"](model.associations, types) : {}))); }
36+
})), (isInput ? (0, fields_1["default"])(model.associations, types) : {}))); }
3737
});
3838
return type;
3939
}

0 commit comments

Comments
 (0)