Skip to content

Commit

Permalink
Merge pull request #176 from gadget-inc/harry/ggt-3893-gadgetrecordto…
Browse files Browse the repository at this point in the history
…json-should-exclude-the

Exclude the `__typename` field from GadgetRecord.toJSON()
  • Loading branch information
airhorns committed Jun 30, 2023
2 parents d59d082 + 6f4f422 commit cf3358c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
22 changes: 21 additions & 1 deletion packages/api-client-core/spec/GadgetRecord.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { omit } from "lodash";
import { ChangeTracking, GadgetRecord } from "../src/GadgetRecord";
interface SampleBaseRecord {
id?: string;
Expand Down Expand Up @@ -42,6 +43,7 @@ describe("GadgetRecord", () => {
let productBaseRecord: SampleBaseRecord;
beforeAll(() => {
productBaseRecord = {
__typename: "Product",
id: "123",
name: "A cool product",
body: "A description of why it's cool",
Expand All @@ -51,7 +53,7 @@ describe("GadgetRecord", () => {
it("should respond toJSON, which returns the inner __gadget.fields properties", () => {
const product = new GadgetRecord<SampleBaseRecord>(productBaseRecord);
expect(product.toJSON()).toEqual({
...productBaseRecord,
...omit(productBaseRecord, "__typename"),
});
});

Expand Down Expand Up @@ -403,4 +405,22 @@ describe("GadgetRecord", () => {
product.setField("changed", true);
expect(product.getField("changed")).toEqual(true);
});

test("the typename should be accessible on the object if passed when constructing", () => {
const product = new GadgetRecord<SampleBaseRecord>(productBaseRecord);
expect(product.typename).toEqual("Product");
});

test("the typename should be undefined on the object if not passed when constructing", () => {
const product = new GadgetRecord<SampleBaseRecord>({});
expect(product.typename).toBeUndefined();
});

test("the typename should not be included in the toJSON output", () => {
let instance = new GadgetRecord<{ __typename: "foo" }>({ __typename: "foo" });
expect(instance.toJSON()).toEqual({});

instance = new GadgetRecord<{ __typename: "foo"; bar: number }>({ __typename: "foo", bar: 1 });
expect(instance.toJSON()).toEqual({ bar: 1 });
});
});
19 changes: 14 additions & 5 deletions packages/api-client-core/src/GadgetRecord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,16 @@ export class GadgetRecordImplementation<Shape extends RecordShape> {
};

private empty = false;

constructor(data: Shape) {
this.__gadget.instantiatedFields = cloneDeep(data);
this.__gadget.persistedFields = cloneDeep(data);
Object.assign(this.__gadget.fields, data);
__typename?: string;

constructor(data?: Shape | null) {
if (data) {
const { __typename, ...fields } = data;
this.__typename = __typename;
this.__gadget.instantiatedFields = cloneDeep(fields);
this.__gadget.persistedFields = cloneDeep(fields);
Object.assign(this.__gadget.fields, fields);
}

if (!data || Object.keys(data).length === 0) {
this.empty = true;
Expand Down Expand Up @@ -77,6 +82,10 @@ export class GadgetRecordImplementation<Shape extends RecordShape> {
return this.empty;
}

get typename(): string | undefined {
return this.__typename;
}

/** Returns the value of the field for the given `apiIdentifier`. These properties may also be accessed on this record directly. This method can be used if your model field `apiIdentifier` conflicts with the `GadgetRecord` helper functions. */
getField(apiIdentifier: string) {
return this.__gadget.fields[apiIdentifier];
Expand Down

0 comments on commit cf3358c

Please sign in to comment.