Skip to content

Commit e9fea8c

Browse files
feat(associations): Allow to skip associations.
1 parent 9b887c5 commit e9fea8c

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717
"node": ">=9.11.2"
1818
},
1919
"devDependencies": {
20+
"dataloader-sequelize": "^1.7.6",
2021
"eslint": "^4.16.0",
2122
"eslint-config-standard": "^11.0.0-beta.0",
2223
"eslint-plugin-import": "^2.8.0",
2324
"eslint-plugin-node": "^5.2.1",
2425
"eslint-plugin-promise": "^3.6.0",
2526
"eslint-plugin-standard": "^3.0.1",
26-
"dataloader-sequelize": "^1.7.6",
2727
"express": "^4.16.4",
2828
"graphql": "^14.0.2",
2929
"graphql-relay": "^0.5.5",

src/generate/associationsFields.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const { GraphQLList, GraphQLString, GraphQLInt } = require('graphql')
22
const { attributeFields } = require('graphql-sequelize')
33
const createResolver = require('../createResolver')
4+
const debug = require('debug')('gsg')
45

56
const generateAssociationField = (relation, types, resolver = null) => {
67
const type =
@@ -67,18 +68,24 @@ const injectAssociations = (
6768
proxyModelName = null
6869
) => {
6970
const modelName = proxyModelName || modelType.name
71+
if (Object.keys(modelName).length === 0) {
72+
throw new Error(
73+
'Associations cannot be injected if no models were provided.'
74+
)
75+
}
7076
const associations = models[modelName].associations
7177
if (Object.keys(associations).length === 0) {
7278
return modelType
7379
}
7480
const associationsFields = {}
7581
for (let associationName in associations) {
7682
if (!graphqlSchemaDeclaration[associations[associationName].target.name]) {
77-
throw new Error(
83+
debug(
7884
`Cannot generate the association for model [${
7985
associations[associationName].target.name
80-
}] as it wasn't declared in the schema declaration.`
86+
}] as it wasn't declared in the schema declaration. Skipping it.`
8187
)
88+
continue
8289
}
8390
associationsFields[associationName] = generateAssociationField(
8491
associations[associationName],

0 commit comments

Comments
 (0)