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

Clarification Regarding Subdocuments and InputTypes

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

Description

Is it possible to provide more documentation around how to leverage graphql-compose-mongoose generated sub-document types. Specifically, how do I best leverage the generated input types to create mutation / query resolvers on the sub-documents? This will help me address the issue raised here ( [https://github.com/nodkz/graphql-compose-mongoose/issues/25] ).

My Schema looks something like this.
```
export const CanAcessSetting = new mongoose.Schema({
online: Boolean,
location: Boolean,
profile: Boolean,
});

export const LanguagSetting = new mongoose.Schema({
label: String,
locale: String,
priority: Number,
});

export const UserSettings = new mongoose.Schema({
language: LanguagSetting,
languages: [LanguagSetting],
privacy: CanAcessSetting,
region: {
regionHome: String,
regionsAccessible: [String],
},
});

export const UserAccountSchema = new mongoose.Schema(
email: String,
username: String,
settings: UserSettings
})
```

Graphql-compose-mongoose generates the following mutation input types:

- UserAccountSettingsInput
- UserAccountSettingsUserAccountSettingsPrivacyInput
- UserAccountSettingsUserAccountSettingsLanguageInput
- UserAccountSettingsUserAccountSettingsRegionInput

I would like write specific mutations for each field in the UserSettings subdocument. When I run the following, the keys under settings are removed (languages, privacy, and region).

```
mutation {
userUpdateById(record: {
_id: "5a0d614bfdb9ea74a3c8ca13",
settings: {
language: { label: "English", locale: "en" }
}
}) {
recordId
record {
settings {
language
}
}
}
}
```
I can write a custom resolver on the settings field such that it updates (using dot notation) only what I need it to update e.g.,

```
async function updateUserLanguageSettings(userId, record: {language, languages}) {
const filter = { _id: userId };
const update = {
$set: {
'settings.language': language,
'settings.languages': languages,
},
};
const options = {
returnNewDocument: true,
};
const userLanguageSettings = await UserAccount.findOneAndUpdate(
filter,
update,
options,
).select('settings').lean();

// return an object corresponding to the "UserAccountSettingsInput"
const { _id, settings } =
return { ... userLanaguageSettings }
}
```
With that said, will something like this work?

```
UserAccountTC.addResolver({
name: 'updateUserLanguageSettings',
kind: 'mutation',
type: [UserAccountSettingsUserAccountSettingsLanguageInput], // does the input type go here?
args: {
_id: 'MongoID!',
language: 'Object',
languages: 'Object'
},
resolve: async ({ source, args }) => {
const userSettings = await updateUserLanguageSettings(
sources._id,
args.language,
args.languages
);
return { ...userSettings }
},
});
```
Since input types exist for all of the subdocuments, it seems like graphql-compose-mongoose should also generate "update[SubdocumentInput]ByParentId" resolvers on each subdocument input type.

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.