arackaf / mongo-graphql-starter

Creates a fully functioning, performant, extensible GraphQL endpoint from a Mongo DB. Supports middleware, Mongo 4 transactions.
MIT License
423 stars 29 forks source link

Adding readonly flag causes build to fail #42

Closed webdevisme closed 5 years ago

webdevisme commented 5 years ago

Geat tool, but I'm having real problems getting the readonly flag working. Inital schema:

export let Category = {
    table: 'categories',
    fields: {
        _id: MongoIdType,
        slug: StringType,
        name: StringType,
        excerpt: StringType,
        description: StringType,
        createdOn: DateType,
        modifiedOn: DateType
    }
}

And running:

import mgs from 'mongo-graphql-starter';
import * as adminSetup from './sp-api-setup';
import path from 'path';

// Create public (readonly) version
adminSetup.Category.readonly = true

mgs.createGraphqlSchema(adminSetup, path.resolve('src/public'))

Results in:

(node:8580) UnhandledPromiseRejectionWarning: SyntaxError: Syntax Error: Expected Name, found } (216:1)
  214 | type Mutation {
  215 | 
> 216 | }
      | ^
    at n (/home/user/project/node_modules/prettier/parser-graphql.js:1)...

My setup:

"mongo-graphql-starter": "^0.10.2"
"esm": "^3.2.25"
node: v10.16.3
npm: 6.9.0
Ubuntu 18.0.4.3 LTS

Any ideas?

arackaf commented 5 years ago

Oh wow. I think I see the problem. I’m assuming you have absolutely NO types in your scheme that are mutable, so my project is emitting an empty GraphQL mutation type (which apparently is invalid).

I’ll fix when able, but, I think a workaround for you would be to just add a junk, made up type to your project setup that’s NOT readonly. Just make up a name and give it a value for table. It’ll emit entries in the GraphQL endpoint that aren’t valid, but just don’t call them and you should be fine.

And like I said, I’ll look to fix this properly this week sometime.

webdevisme commented 5 years ago

Ahh, ok, that does explain it.

Thanks, I'll look forward to the update (as i'm not keen on having a writeable endpoint on the project I'm working on!)

I'll be digging into your code more as I go, only just scraping the surface at the moment!

arackaf commented 5 years ago

Yo should be fixed with 0.10.3 - give it a try and let me know

webdevisme commented 5 years ago

Hi, thanks for that. I've just had a chance to try that out and no joy unfortunately.

I'm still getting

(node:16603) UnhandledPromiseRejectionWarning: SyntaxError: Syntax Error: Expected Name, found } (216:1)
  214 | type Mutation {
  215 | 
> 216 | }
      | ^
    at n (/home/user/project/node_modules/prettier/parser-graphql.js:1)

error, unless I add a zombie writable type. :(

webdevisme commented 5 years ago

I've created a pull request #43 that might fix it :)

webdevisme commented 5 years ago

Hmm.. there's still a problem relating to the writeableNames array being populated (even with only readonly types) which is causing problems in the master schema and resolver.

For some reason, the readonly flag is not being passed through with each module.

arackaf commented 5 years ago

Oh ... try moving the readonly to right inside your projectSetup file, instead of adding it dynamically. See project 10 inside my test folder.

arackaf commented 5 years ago

Hm - I tried that, and everything still works, fine.

Honestly, I'd pull the repo, and see if you can get the tests to run. Make sure you have mongod running in the background somewhere. Really just see if you can get

npm run test10

to run.

If you can, see if you can tinker with the projectSetup to somehow, some way reproduce what you're seeing. If you can, just send me a PR with the updates and I'll absolutely have a look. But from what I'm seeing now, everything should work fine.

webdevisme commented 5 years ago

You're right. And apologies for the bad PM.

I deleted the node_modules and re installed, and everything's good now. (strange as I'd checked that it had updated, but was getting errors!?)

Great job, thanks :)

arackaf commented 5 years ago

Yeah! Glad to hear.