Tallyb / loopback-graphql

Adding graphql to loopback, based on Apollo Server
MIT License
201 stars 18 forks source link

UpperFirst causing problem for lower case model names #12

Closed JonnyBGod closed 7 years ago

JonnyBGod commented 7 years ago

Is there any reason to force using Model names starting with upper case letter?

This is causing problems when using models with lower case names.

utils.js#L50

Error Log:

.../node_modules/loopback-graphql/lib/ast.js:47
    types[modelName].fields[propertyName] = {
                    ^

TypeError: Cannot read property 'fields' of undefined
    at mapProperty (.../node_modules/loopback-graphql/lib/ast.js:47:21)
    at _.forEach (.../api/node_modules/loopback-graphql/lib/ast.js:223:13)
    at .../node_modules/lodash/lodash.js:4970:15
    at baseForOwn (.../node_modules/lodash/lodash.js:3020:24)
    at .../node_modules/lodash/lodash.js:4939:18
    at Function.forEach (./node_modules/lodash/lodash.js:9344:14)
 ..   at _.forEach.model (.../node_modules/loopback-graphql/lib/ast.js:222:11)
    at arrayEach (.../node_modules/lodash/lodash.js:537:11)
    at Function.forEach (.../node_modules/lodash/lodash.js:9344:14)
    at abstractTypes (.../node_modules/loopback-graphql/lib/ast.js:213:7)
    at module.exports (.../node_modules/loopback-graphql/lib/boot.js:14:17)
    at .../node_modules/loopback-boot/lib/executor.js:437:5
    at Array.forEach (native)
    at setupComponents (.../node_modules/loopback-boot/lib/executor.js:430:27)
    at execute (.../node_modules/loopback-boot/lib/executor.js:41:3)
    at bootLoopBackApp (.../node_modules/loopback-boot/index.js:154:3)
    at Object.<anonymous> (.../server/server.js:25:1)
    at Module._compile (module.js:573:32)
    at Object.Module._extensions..js (module.js:582:10)
    at Module.load (module.js:490:32)
    at tryModuleLoad (module.js:449:12)
    at Function.Module._load (module.js:441:3)
Tallyb commented 7 years ago

I am not sure this is because of the upper. Can you try and isolate the problematic model and paste it here, so I can reproduce?

JonnyBGod commented 7 years ago

I had this problem with multiple models where I was using lower case names. After changing the models to uppercase it worked for all of them.

The problem is when extending loopback built in models like User I need to create a new model called user to keep an understandable name.

JonnyBGod commented 7 years ago

Also worked fine with lower case model names if I remove all _.upperFirst().

Tallyb commented 7 years ago

The right fix is to have on line 47 types[utils.singularModelName(model)].fields