Skip to content

Commit

Permalink
Access the relationship in a better way
Browse files Browse the repository at this point in the history
  • Loading branch information
brianjgeiger committed Aug 10, 2023
1 parent d4a10e1 commit 26d9fcb
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 17 deletions.
20 changes: 3 additions & 17 deletions app/serializers/osf-serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ import { AttributesObject } from 'jsonapi-typescript';
// This import will change with 4.12 of ember-data. It will instead be
// import { peekGraph } from '@ember-data/graph/-private';
import { peekGraph } from '@ember-data/record-data/-private';
// You should also be able to
// import { recordIdentifierFor } from '@ember-data/store';
// and use that to const relationship = peekGraph(store).get(recordIdentifierFor(record), key)
// instead of what I did in the hack below.
import { recordIdentifierFor } from '@ember-data/store';

import {
PaginatedMeta,
Expand Down Expand Up @@ -140,17 +137,6 @@ export default class OsfSerializer extends JSONAPISerializer {
return underscore(key);
}

getGraphRelationships(snapshot: DS.Snapshot) {
const graph = peekGraph(this.store);
const identifiers = graph.identifiers;
for (const [key, value] of identifiers) {
if(key.id === snapshot.id && key.type === snapshot.type.modelName) {
return value;
}
}
return undefined;
}

serialize(snapshot: DS.Snapshot, options: { includeId?: boolean, osf?: OsfSerializerOptions } = {}) {
const serialized = super.serialize(snapshot, options) as SingleResourceDocument;
serialized.data.type = underscore(serialized.data.type);
Expand All @@ -171,9 +157,9 @@ export default class OsfSerializer extends JSONAPISerializer {
// When https://github.com/emberjs/data/pull/8131 is merged, we should have public access
// to this data from a new import location.
if (serialized.data.relationships) {
const relationships = this.getGraphRelationships(snapshot);
const graph = peekGraph(this.store);
for (const key of Object.keys(serialized.data.relationships)) {
const rel = relationships[camelize(key)];
const rel = graph.get(recordIdentifierFor(snapshot.record), camelize(key));
let isClean = true;
if (rel.definition.kind === 'belongsTo') {
isClean = rel.localState === rel.remoteState;
Expand Down
3 changes: 3 additions & 0 deletions types/@ember-data/store.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare module '@ember-data/store' {
export function recordIdentifierFor(record: Record);
}

0 comments on commit 26d9fcb

Please sign in to comment.