graphql-compose / graphql-compose/graphql-compose-mongoose

findByIds resolver issue with multiple Schemas using useDb()

Open
#47 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
706
Forks
98
PR merge metrics
No merged PRs in 30d

Description

Previously when there was only one schema this worked, where source.attributes is an array of MongoIDs.
```
AttributeGroupTC.addRelation(
'attributesData', {
resolver: () => AttributeTC.get('$findByIds'),
prepareArgs: {
_ids: (source) => source.attributes,
},
projection: { attributes: true },
},
);
```
After multiple schemas, findByIds resolves to null, only findMany works like below:

```
schema.js
const getSchema = (db: any) => {
const GQC = new ComposeStorage();

const ViewerTC = GQC.get('Viewer');
GQC.rootQuery().addFields({
viewer: {
type: ViewerTC.getType(),
description: 'Data under client context',
resolve: () => ({}),
},
});
const fields = {
attributeGroup: AttributeGroup.getTC(db).get('$findOne'),
attributeGroupList: AttributeGroup.getTC(db).get('$findMany'),

attribute: Attribute.getTC(db).get('$findOne'),
attributeList: Attribute.getTC(db).get('$findMany'),
};

ViewerTC.addFields(fields);

return GQC.buildSchema();
};

export default { getSchema };
```
```
attribute.js
const AttributeSchema = () => new Schema({
_id: Schema.Types.ObjectId,
name: String,
...
},
{
collection: 'attributes',
});
const cache = {};

const getTC = (db: any) => {
if (cache[db.name] && cache[db.name].TC) {
return cache[db.name].TC;
}
const dbModel = db.model('attribute', AttributeSchema());
mongooseTypeStorage.clear();
const AttributeTC = composeWithDataLoader(composeWithMongoose(dbModel), { cacheExpiration: 10000 });
cache[db.name] = {
...cache[db.name],
TC: AttributeTC,
};
return AttributeTC;
};

export default { getTC };
```
```
attributeGroup.js
import Attribute from './attribute';

const AttributeGroupSchema = () => new Schema({
_id: Schema.Types.ObjectId,
name: String,
attributes: [Schema.Types.ObjectId],
},
{
collection: 'attribute_groups',
});
const cache = {};

const getTC = (db: any) => {
if (cache[db.name] && cache[db.name].TC) {
return cache[db.name].TC;
}

const dbModel = db.model('AttributeGroup', AttributeGroupSchema());
mongooseTypeStorage.clear();
const AttributeGroupTC = composeWithDataLoader(composeWithMongoose(dbModel), { cacheExpiration: 10000 });
AttributeGroupTC.addRelation(
'attributesData', {
resolver: () => Attribute.getTC(db).get('$findMany'),
prepareArgs: {
filter: source => ({
_operators: {
_id: { in: source.attributes },
},
}),
},
projection: { attributes: true },
},
);
cache[db.name] = {
...cache[db.name],
TC: AttributeGroupTC,
};
return AttributeGroupTC;
};

export default { getTC };
```

Here is my package.json
```
"graphql-compose": "^2.2.0",
"graphql-compose-connection": "^2.2.2",
"graphql-compose-dataloader": "^1.1.2",
"graphql-compose-mongoose": "^1.6.0",
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.