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

Use `setRecordIdFn` to override default `_id` generation behavior?

Open
#364 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

Hi, thank you for building such a great framework!

I need to be able to generate mutations that will automatically create _id. This functionality already exists but it builds a Mongo ObjectId. I need the ID to be a string.

The behavior I want to achieve:
1. client calls the createOne mutation without providing an ID
2. server creates a new mongo document with a string ID `ex: _id: 'some-UUID-234'`

I think I can modify the default behavior by using `setRecordIdFn` on my object type composer but I can't get it to work.

Shouldn't this work considering this excerpt from [`payloadRecordId.ts`](https://github.com/graphql-compose/graphql-compose-mongoose/blob/master/src/resolvers/helpers/payloadRecordId.ts) in this repo:
```js
export type PayloadRecordIdHelperOpts = {
/** Custom function for id generation. By default: `doc._id`. */
fn?: (doc: any, context: any) => any;
/** Custom output type for returned recordId */
type?: string | ComposeOutputTypeDefinition;
};

export function payloadRecordId(
tc: ObjectTypeComposer | InterfaceTypeComposer,
opts?: PayloadRecordIdHelperOpts | false
): ObjectTypeComposerFieldConfigMapDefinition | null {
if (opts === false) return null;

return {
recordId: {
description: 'Document ID',
type: opts?.type ? opts.type : tc.hasField('_id') ? tc.getFieldTC('_id') : 'MongoID',
resolve: (source, _, context) => {
const doc = (source as any)?.record;
if (!doc) return;
return opts?.fn ? opts.fn(doc, context) : doc?._id;
},
},
};
}
```

Thank you for your time!

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.