graphql-compose-mongoose
graphql-compose-mongoose copied to clipboard
Schema must contain unique named types but contains multiple types named "MongoID"
I am facing a issue with stitching types .
const remoteQuery = TypeComposer.create(executableSchema._queryType);
const mutation = TypeComposer.create(executableSchema._mutationType);
const subscription = TypeComposer.create(executableSchema._subscriptionType);
GQC.rootQuery().addFields({
...remoteQuery.getFields()
});
GQC.rootMutation().addFields({
...mutation.getFields()
});
GQC.rootSubscription().addFields({
...subscription.getFields()
});
and then I am calling buildSchema();
Everything works fine, until I use a type that has the MongoID field. The issue is my executableSchema already contains definitions for MongoID that can't be taken out. Do you have a suggestion to handle this? It won't matter if it removes duplicates or if it uses the first implementation.
graphql-js
builds schema and it does not have built-in resolving of the duplicate type definition.
For now, as a workaround, you may import GraphQLMongoID
from this lib and use it in your other schemas.
import { GraphQLMongoID } from 'graphql-compose-mongose';
PS. Also will be helpful to know how you construct or use which tool/lib provides for you other MongoID type definition.
graphql-compose-mongoose
does not allow to override its MongoID type for now. But it will be improved in next version. Thanks for reporting!
From new 4.1.0 version you may override build-in MongoID
type by your implementation:
import { schemaComposer } from 'graphql-compose';
import { composeWithMongoose } from 'graphql-compose-mongoose';
schemaComposer.set('MongoID', YOUR_TYPE_IMPLEMENTATION);
const UserTC = composeWithMongoose(UserModel, { schemaComposer });
@nodkz I used a custom schema with the mongoID, I might be going around this in the wrong order, but I am using apollo, and I have schemas defined in apollo and graphql, I want to be able to use them together as if they were one (sharing types across), meanwhile, I worked around that by using GQC to parse the whole schema from the resolvers/typedefs variables, somehwhat close to what Apollo Server does.
I am having this problem also, but I’ve yet to define MongoID on any schema. Could it be the internals, particularly as it relates to generating shared schemas? In any case, I am assigning fields to some variation of "mongoose.Schema.ObjectId". Does that mean I'm using MongoID indirectly?
Here's an example,
venueId: { type: Schema.ObjectId, ref: 'Venue' },
are you saying I should use
venueId: String,
or
import { GraphQLMongoID } from 'graphql-compose-mongose';
....
venueId: { type: GraphQLMongoID, ref: 'Venue' },
The issue is I'm not storing plain keys and foreign-keys, I'm storying MongoID references.
I believe I’ve discovered at least the reproducible concern. I’m only experience this problem when following these directions
“Reusing the same mongoose Schema in embedded object fields”
With the latest upgrade, this leads to MongoID problems. I solved it by not registering shared schemas.
Sorry for late response.
@smooJitter you cannot use graphql types for defining mongoose schema. In mongoose schema, you may use only mongoose types. Graphql-compose-mongoose takes mongoose schema as a source and tries to generate types from it.
-
graphql-compose-mongoose
by default uses its built-inMongoID
GraphQL type for mongoose fields which defined like:
const ArticleSchema = new mongoose.Schema({
userId: {
type: mongoose.Schema.Types.ObjectId,
},
});
- In some cases when you stitch schemas can be a conflict for types which have the same name but have different instances. If you stitch schemas you should use one type instance, otherwise, it will be a conflict.
2.1) In old package version you may only import { GraphQLMongoID } from 'graphql-compose-mongose';
and use it in other schema definitions for avoiding conflicts.
2.2) But from version 4.1.0 you able to override internal built-in MongoID
type from another schema.
Just set from another schema MongoID type in graphql-compose
import { schemaComposer } from 'graphql-compose';
schemaComposer.set('MongoID', YOUR_TYPE_IMPLEMENTATION);
... and graphql-compose-mongoose
will use your type for MongoID
name (not built-in). But you need set type before calling composeWithMongoose
method.
@smooJitter if my answer does not help to find a solution. Please open PR with reproducible test-case, like it did here https://github.com/graphql-compose/graphql-compose-mongoose/blob/master/src/tests/github-issues-test.js or how it did in core package https://github.com/graphql-compose/graphql-compose/tree/master/src/tests/github_issues
I'm sorry for the late response. I actually thought I had this figure out. Actually, I took a different approach and now it's coming back to bit me.
After upgrading to 4.1.0 from a much earlier version, the instruction outline under "Reusing the same mongoose Schema in embedded object fields" of the README, no longer works. I simply stopped sharing Mongoose Schemas. After looking at the source code, it appears the issue seems to have something to do with convertFieldToGraphQL function of the "convertSchemaToGraphQL()" function. see #100.
Frankly, I don't understand how PR request work @nodkz. Nevertheless, if you confirm the instructions outlined under "Reusing the same mongoose Schema in embedded object fields" of the README still work, then I will give it another try with perhaps using different approach to how I call the convertSchemaToGraphQL.
Does this add clearity?
I cannot confirm that it works or not. Need to obtain reproducible example.
@smooJitter, please fork https://github.com/graphql-compose/graphql-compose-boilerplate and add there your basic use case with your error. I try to figure out the problem and provide a solution for you.
Thanks.
Ok