Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support of non standard id field name #398

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/MSTGQLObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ export function MSTGQLRef<T extends IAnyModelType>(
return node
},
set(value: any) {
return [label, MSTGQL_ID_DELIM, value.id].join("")
const typeDef = value.store.getTypeDef(value.__typename)
const id = value[typeDef.identifierAttribute ?? 'id']
return [label, MSTGQL_ID_DELIM, id].join("")
}
})
}
Expand Down
9 changes: 6 additions & 3 deletions src/deflateHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ export function deflateHelper(store: any, data: any) {
if (!data || typeof data !== "object") return data
if (Array.isArray(data)) return data.map(deflate)

const { __typename, id } = data
const { __typename } = data


if (__typename && store.isRootType(__typename)) {
// GQL object with root type, keep only __typename & id
return { __typename, id }
// GQL object with root type, keep only __typename & identifier
const typeDef = store.getTypeDef(__typename)
const idName = typeDef.identifierAttribute ?? 'id'
return { __typename, [idName]: data[idName] }
} else {
// GQL object with non-root type, return object with all props deflated
const snapshot: any = {}
Expand Down
5 changes: 4 additions & 1 deletion src/mergeHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export function mergeHelper(store: any, data: any) {
if (!data || typeof data !== "object") return data
if (Array.isArray(data)) return data.map(merge)

const { __typename, id } = data
const { __typename } = data

// convert values deeply first to MST objects as much as possible
const snapshot: any = {}
Expand All @@ -18,6 +18,9 @@ export function mergeHelper(store: any, data: any) {
if (__typename && store.isKnownType(__typename)) {
// GQL object with known type, instantiate or recycle MST object
const typeDef = store.getTypeDef(__typename)

const id = data[typeDef.identifierAttribute ?? 'id']

// Try to reuse instance, even if it is not a root type
let instance = id !== undefined && resolveIdentifier(typeDef, store, id)
if (instance) {
Expand Down