Skip to content

Commit

Permalink
support for custom enum custom type (#1587)
Browse files Browse the repository at this point in the history
  • Loading branch information
lolopinto authored Aug 8, 2023
1 parent 93987f6 commit 706783e
Show file tree
Hide file tree
Showing 9 changed files with 255 additions and 39 deletions.
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.1.5",
"@snowtop/ent": "^0.1.8",
"@snowtop/ent-email": "^0.1.0-rc1",
"@snowtop/ent-passport": "^0.1.0-rc1",
"@snowtop/ent-password": "^0.1.0-rc1",
Expand Down

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/src/graphql/generated/schema.gql

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

30 changes: 30 additions & 0 deletions examples/simple/src/graphql/mutations/custom_enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { GraphQLEnumType } from "graphql";

export enum ContactLabel2 {
Work = "work",
Home = "home",
Default = "default",
Unknown = "unknown",
Self = "self",
}

export const GraphQLContactLabel2 = new GraphQLEnumType({
name: "ContactLabel2",
values: {
WORK: {
value: "work",
},
HOME: {
value: "home",
},
DEFAULT: {
value: "default",
},
UNKNOWN: {
value: "unknown",
},
SELF: {
value: "self",
},
},
});
11 changes: 11 additions & 0 deletions examples/simple/src/graphql/mutations/import_contact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
} from "../../ent/generated/user/actions/user_builder";
import { ExampleViewer } from "../../viewer/viewer";
import { ContactLabel } from "../../ent/generated/types";
import { ContactLabel2 } from "./custom_enum";

export class ImportContactResolver {
@gqlMutation({
Expand All @@ -36,6 +37,15 @@ export class ImportContactResolver {
type: "ContactLabel",
nullable: true,
},
{
name: "defaultLabel2",
type: {
type: "GraphQLContactLabel2",
importPath: "src/graphql/mutations/custom_enum",
tsType: "ContactLabel2",
tsImportPath: "src/graphql/mutations/custom_enum",
},
},
],
async: true,
})
Expand All @@ -44,6 +54,7 @@ export class ImportContactResolver {
userID: ID,
file: Promise<FileUpload>,
label?: ContactLabel,
_label2?: ContactLabel2,
) {
const file2 = await file;

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.1.6",
"version": "0.1.8",
"description": "snowtop ent framework",
"main": "index.js",
"types": "index.d.ts",
Expand Down
138 changes: 138 additions & 0 deletions ts/src/graphql/graphql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
GraphQLString,
GraphQLScalarType,
GraphQLError,
GraphQLEnumType,
GraphQLBoolean,
} from "graphql";
import { Kind, ValueNode } from "graphql/language";

Expand Down Expand Up @@ -523,6 +525,142 @@ describe("property", () => {
tsImportPath: "",
},
]);
GQLCapture.resolve([]);
});
});

describe("enabled. custom enum ", () => {
enum RequestStatus {
PENDING = "pending",
ACCEPTED = "accepted",
REJECTED = "rejected",
}

const GraphQLRequestStatus = new GraphQLEnumType({
name: "RequestStatus",
values: {
PENDING: {
value: "pending",
},
ACCEPTED: {
value: "accepted",
},
REJECTED: {
value: "rejected",
},
},
});

test("enabled. custom enum used incorrectly", () => {
try {
class Request {
@gqlField({
class: "User",
type: GraphQLRequestStatus,
})
status: RequestStatus;
}
throw new Error(`should have thrown`);
} catch (err) {
expect(err.message).toMatch(
/custom enum type RequestStatus is not supported this way. use CustomType syntax/,
);
}

validateNoCustom();
});

test("enabled. custom enum", () => {
class Request {
@gqlField({
class: "User",
type: {
type: "GraphQLRequestStatus",
importPath: "",
tsType: "RequestStatus",
tsImportPath: "",
},
})
status: RequestStatus;
}
validateOneCustomField({
nodeName: "User",
functionName: "status",
gqlName: "status",
fieldType: CustomFieldType.Field,
results: [
{
type: "GraphQLRequestStatus",
name: "",
tsType: "RequestStatus",
needsResolving: true,
},
],
args: [],
});

validateCustomTypes([
{
type: "GraphQLRequestStatus",
importPath: "",
tsType: "RequestStatus",
tsImportPath: "",
},
]);
GQLCapture.resolve([]);
});

test.only("enabled. custom enum as arg", () => {
class Request {
@gqlField({
class: "User",
type: GraphQLBoolean,
args: [
{
name: "status",
type: {
type: "GraphQLRequestStatus",
importPath: "",
tsType: "RequestStatus",
tsImportPath: "",
},
},
],
})
async doSomething(status: RequestStatus) {
return true;
}
}
validateOneCustomField({
nodeName: "User",
functionName: "doSomething",
gqlName: "doSomething",
fieldType: CustomFieldType.Function,
results: [
{
type: "Boolean",
name: "",
},
],
args: [
{
name: "status",
type: "GraphQLRequestStatus",
tsType: "RequestStatus",
needsResolving: true,
},
],
});

validateCustomTypes([
{
type: "GraphQLRequestStatus",
importPath: "",
tsType: "RequestStatus",
tsImportPath: "",
},
]);
GQLCapture.resolve([]);
});
});
});
Expand Down
Loading

0 comments on commit 706783e

Please sign in to comment.