graphql-compose / graphql-compose-mongoose

Mongoose model converter to GraphQL types with resolvers for graphql-compose https://github.com/nodkz/graphql-compose
MIT License
709 stars 94 forks source link

Model name with dot not working #396

Closed rombar86 closed 2 years ago

rombar86 commented 2 years ago

Hello,

When I create a Model with a name containing a dot, I get an Error. If I remove the dot everything is fine. Am I doing something wrong?

Schema: `import mongoose from "mongoose"; import { composeWithMongoose } from 'graphql-compose-mongoose' const Schema = mongoose.Schema;

const productSchema = new Schema({ name: { type: String, required: true, unique: true }, }, { collection: 'my.products' })

export const Product = mongoose.model("my.product", productSchema); export const ProductTC = composeWithMongoose(Product); `

Error: /home/user/workspace/myProject/node_modules/graphql/error/syntaxError.js:15 return new _GraphQLError.GraphQLError( ^ GraphQLError: Syntax Error: Unexpected Name "my". at syntaxError (/home/user/workspace/myProject/node_modules/graphql/error/syntaxError.js:15:10) at Parser.unexpected (/home/user/workspace/myProject/node_modules/graphql/language/parser.js:1476:41) at Parser.parseDefinition (/home/user/workspace/myProject/node_modules/graphql/language/parser.js:211:16) at Parser.many (/home/user/workspace/myProject/node_modules/graphql/language/parser.js:1529:26) at Parser.parseDocument (/home/user/workspace/myProject/node_modules/graphql/language/parser.js:121:25) at parse (/home/user/workspace/myProject/node_modules/graphql/language/parser.js:32:17) at TypeMapper.convertSDLTypeDefinition (/home/user/workspace/myProject/node_modules/graphql-compose/src/TypeMapper.ts:249:44) at Function.createTemp (/home/user/workspace/myProject/node_modules/graphql-compose/src/ObjectTypeComposer.ts:296:28) at Function.create (/home/user/workspace/myProject/node_modules/graphql-compose/src/ObjectTypeComposer.ts:266:21) at SchemaComposer.getOrCreateOTC (/home/user/workspace/myProject/node_modules/graphql-compose/src/SchemaComposer.ts:644:37) { path: undefined, locations: [ { line: 1, column: 1 } ], extensions: [Object: null prototype] {} } I use the latest version and Apollo 3.

nodkz commented 2 years ago

Dots are not allowed in GraphQL type names. So you should provide it explicitly:

export const Product = mongoose.model("my.product", productSchema);
export const ProductTC = composeWithMongoose(Product, { name: 'MyProduct' });

Moreover composeWithMongoose is the stale method. Please use composeMongoose instead. More information can be found here https://github.com/graphql-compose/graphql-compose-mongoose/blob/master/docs/releases/9.0.0.md

rombar86 commented 2 years ago

OK thank you!