Skip to content

Commit

Permalink
save polymorphic types in structs (#1852)
Browse files Browse the repository at this point in the history
  • Loading branch information
lolopinto authored Nov 14, 2024
1 parent b86e12f commit d0b098e
Show file tree
Hide file tree
Showing 15 changed files with 130 additions and 32 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions examples/simple/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/simple/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"tsconfig-paths": "^3.11.0"
},
"dependencies": {
"@snowtop/ent": "^0.2.0-alpha.7",
"@snowtop/ent": "^0.2.0-alpha.10",
"@snowtop/ent-email": "^0.1.0-rc1",
"@snowtop/ent-passport": "^0.1.0-rc1",
"@snowtop/ent-password": "^0.1.0-rc1",
Expand Down
6 changes: 5 additions & 1 deletion examples/simple/src/ent/tests/contact.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import EditContactAction from "../contact/actions/edit_contact_action";
import { LoggedOutExampleViewer, ExampleViewer } from "../../viewer/viewer";
import { query } from "@snowtop/ent";
import { v4 } from "uuid";
import { ContactLabel, ContactInfoSource } from "../generated/types";
import { ContactLabel, ContactInfoSource, NodeType } from "../generated/types";
import { Transaction } from "@snowtop/ent/action";
import CreateFileAction from "../file/actions/create_file_action";
import { advanceTo } from "jest-date-mock";
Expand Down Expand Up @@ -117,6 +117,8 @@ test("create contact with explicit attachments", async () => {
date: d,
note: "test",
dupeFileId: file.id,
creatorId: user.id,
creatorType: NodeType.User,
},
{
fileId: file2.id,
Expand All @@ -136,6 +138,8 @@ test("create contact with explicit attachments", async () => {
date: d.toISOString(),
note: "test",
dupeFileId: file.id,
creatorId: user.id,
creatorType: NodeType.User,
},
{
fileId: file2.id,
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/simple/src/graphql/generated/schema.gql

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 16 additions & 2 deletions examples/simple/src/graphql/tests/contact_type.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import {
queryRootConfig,
} from "@snowtop/ent-graphql-tests";
import { clearAuthHandlers } from "@snowtop/ent/auth";
import { encodeGQLID } from "@snowtop/ent/graphql";
import { encodeGQLID, mustDecodeIDFromGQLID } from "@snowtop/ent/graphql";
import schema from "../generated/schema";
import CreateUserAction from "../../ent/user/actions/create_user_action";
import { Contact, User } from "../../ent";
import { randomEmail, randomPhoneNumber } from "../../util/random";
import EditUserAction from "../../ent/user/actions/edit_user_action";
import CreateContactAction from "../../ent/contact/actions/create_contact_action";
import { LoggedOutExampleViewer, ExampleViewer } from "../../viewer/viewer";
import { ContactLabel } from "src/ent/generated/types";
import { ContactLabel, NodeType } from "src/ent/generated/types";
import EditContactAction from "src/ent/contact/actions/edit_contact_action";
import CreateContactEmailAction from "src/ent/contact_email/actions/create_contact_email_action";
import CreateContactPhoneNumberAction from "src/ent/contact_phone_number/actions/create_contact_phone_number_action";
Expand Down Expand Up @@ -368,6 +368,8 @@ test("create contact with attachments", async () => {
note: "note",
date: d.toISOString(),
dupeFileId: encodeGQLID(file),
creatorId: encodeGQLID(user),
creatorType: "User",
},
{
fileId: encodeGQLID(file2),
Expand All @@ -378,12 +380,24 @@ test("create contact with attachments", async () => {
],
},
},
[
"contact.id",
async function name(id: string) {
const entId = mustDecodeIDFromGQLID(id);
const contact = await Contact.loadX(user.viewer, entId);
expect(contact.attachments).toHaveLength(2);
expect(contact.attachments?.[0].creatorType).toBe(NodeType.User);
},
],
["contact.firstName", "Jon"],
["contact.lastName", "Snow"],
["contact.emails[0].emailAddress", email],
["contact.attachments[0].file.id", encodeGQLID(file)],
["contact.attachments[0].dupeFile.id", encodeGQLID(file)],
["contact.attachments[0].note", "note"],
["contact.attachments[0].creator.id", encodeGQLID(user)],
// TODO we should have a way to query nested graphql fragments...
// ["contact.attachments[0].creator....on User.type", "User"],
["contact.attachments[1].dupeFile.id", encodeGQLID(file2)],
["contact.attachments[1].file.id", encodeGQLID(file2)],
["contact.attachments[1].note", "note2"],
Expand Down
10 changes: 8 additions & 2 deletions internal/graphql/generate_ts_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -3362,8 +3362,14 @@ func buildCustomInterfaceNode(processor *codegen.Processor, ci *customtype.Custo
}

for _, f := range ci.Fields {
if !f.ExposeToGraphQL() {
continue
if ciInfo.input {
if !f.EditableGraphQLField() {
continue
}
} else {
if !f.ExposeToGraphQL() {
continue
}
}
ft := &fieldType{
Name: f.GetGraphQLName(),
Expand Down
2 changes: 1 addition & 1 deletion ts/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@snowtop/ent",
"version": "0.2.0-alpha.9",
"version": "0.2.0-alpha.10",
"description": "snowtop ent framework",
"main": "index.js",
"types": "index.d.ts",
Expand Down
3 changes: 0 additions & 3 deletions ts/src/schema/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -592,9 +592,6 @@ export interface FieldOptions {

fetchOnDemand?: boolean;

// on uuid types, provides the ability to disable base 64 encoding of the uuid if for example this is a raw uuid not associated with an ent/object in the db
disableBase64Encode?: boolean;

// if dbOnly, field isn't exposed in ent and graphql
// will still exit in the db and not be removed
// allows keeping the field in the db and avoid data loss if we still want the field for some reason
Expand Down
39 changes: 39 additions & 0 deletions ts/src/schema/struct_field.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -860,3 +860,42 @@ describe("struct as list global type", () => {
expect(f.format(val)).toBe(JSON.stringify(formatted));
});
});

test("struct with polymorphic field", async () => {
const f = structTypeF({
user_id: UUIDType({
polymorphic: {
types: ["User"],
},
}),
value: StringType(),
});

const val = {
user_id: v1(),
user_type: "user",
value: "string",
};

expect(await f.valid(val)).toBe(true);
expect(f.format(val)).toBe(JSON.stringify(val));
});

test("struct with invalid polymorphic field", async () => {
const f = structTypeF({
user_id: UUIDType({
polymorphic: {
types: ["User"],
},
}),
value: StringType(),
});

const val = {
user_id: v1(),
user_type: "hello",
value: "string",
};

expect(await f.valid(val)).toBe(false);
});
56 changes: 45 additions & 11 deletions ts/src/schema/struct_field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,8 @@ export class StructField extends BaseField implements Field {
throw new Error("valid was not called");
}
let ret: Object = {};
for (const k in this.options.fields) {
const field = this.options.fields[k];

const processField = (k: string, field: Field) => {
// check two values
// store in dbKey format

Expand All @@ -93,14 +92,27 @@ export class StructField extends BaseField implements Field {
}

if (val === undefined) {
continue;
return;
}
if (field.format) {
// indicate nested so this isn't JSON stringified
ret[dbKey] = field.format(val, true);
} else {
ret[dbKey] = val;
}
};

for (const k in this.options.fields) {
const field = this.options.fields[k];

processField(k, field);

if (field.getDerivedFields) {
const derivedFields = field.getDerivedFields(k);
for (const k in derivedFields) {
processField(k, derivedFields[k]);
}
}
}
// don't json.stringify if nested or list
if (nested) {
Expand Down Expand Up @@ -157,11 +169,12 @@ export class StructField extends BaseField implements Field {
}

let promises: (boolean | Promise<boolean>)[] = [];
// TODO probably need to support optional fields...
let valid = true;
for (const k in this.options.fields) {
const field = this.options.fields[k];
let dbKey = getStorageKey(field, k);

const processField = (
k: string,
dbKey: string,
field: Field,
): boolean | undefined => {
let fieldName = toFieldName(k);
let val = obj[fieldName];

Expand Down Expand Up @@ -193,15 +206,36 @@ export class StructField extends BaseField implements Field {
if (val === undefined || val === null) {
// nullable, nothing to do here
if (field.nullable) {
continue;
return;
}
valid = false;
break;
return false;
}
if (!field.valid) {
continue;
return;
}
promises.push(field.valid(val));
};
// TODO probably need to support optional fields...
let valid = true;
for (const k in this.options.fields) {
const field = this.options.fields[k];
let dbKey = getStorageKey(field, k);

if (processField(k, dbKey, field) === false) {
valid = false;
}

if (field.getDerivedFields) {
const derivedFields = field.getDerivedFields(k);
for (const k in derivedFields) {
const derivedField = derivedFields[k];
let dbKey = getStorageKey(derivedField, k);
if (processField(k, dbKey, derivedField) === false) {
valid = false;
}
}
}
}
if (!valid) {
return valid;
Expand Down

0 comments on commit d0b098e

Please sign in to comment.