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

Exclude the __typename field from GadgetRecord.toJSON() #176

Merged
merged 1 commit into from
Jun 30, 2023
Merged
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
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