schema: generate runtime constants for enum values (#961)

Summary:
We have a `const Reactions` convenience enum in `github/graphql.js`.
That value is useful, but that module is slated to die. This commit
extends our Flow type generation script to include these values.

Test Plan:
Existing unit tests suffice.

wchargin-branch: schema-generate-enums
This commit is contained in:
William Chargin 2018-10-31 12:22:58 -07:00 committed by GitHub
parent 9bccb7661d
commit 2377f1980f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 66 additions and 13 deletions

View File

@ -9,12 +9,20 @@ export type Actor = Human | TalentedChimpanzee;
export type Color = \\"BLUE\\" | \\"GREEN\\" | \\"RED\\";
export const Color$Values: {|
+BLUE: \\"BLUE\\",
+GREEN: \\"GREEN\\",
+RED: \\"RED\\",
|} = Object.freeze({ BLUE: \\"BLUE\\", GREEN: \\"GREEN\\", RED: \\"RED\\" });
export type DateTime = string;
export type Dollars = number;
export type EmptyEnum = empty;
export const EmptyEnum$Values: {||} = Object.freeze({});
export type EmptyUnion = empty;
export type Human = {|

View File

@ -16,7 +16,7 @@ export default function generateFlowTypes(
schema: Schema,
prettierOptions: PrettierOptions
): string {
const typedefs = [];
const definitions = [];
function formatField(field: FieldType): string {
switch (field.type) {
@ -66,17 +66,32 @@ export default function generateFlowTypes(
const type = schema[typename];
switch (type.type) {
case "SCALAR":
typedefs.push(`export type ${typename} = ${type.representation};`);
definitions.push(`export type ${typename} = ${type.representation};`);
break;
case "ENUM": {
const rhs =
Object.keys(type.values).length === 0
? "empty"
: Object.keys(type.values)
const values = Object.keys(type.values)
.sort()
.map((x) => JSON.stringify(x))
.join(" | ");
typedefs.push(`export type ${typename} = ${rhs};`);
.map((x) => JSON.stringify(x));
// export type E = "A" | "B";
const typeRhs = values.length === 0 ? "empty" : values.join(" | ");
definitions.push(`export type ${typename} = ${typeRhs};`);
// export const E$Values: {|+A: "A", +B: "B"|} = Object.freeze(...);
const objectName = `${typename}$Values`;
const objectType = [
"{|",
values.map((x) => `+${x}: ${x}`).join(", "),
"|}",
].join("");
const objectValue = [
"Object.freeze({",
values.map((x) => `${x}: ${x}`).join(", "),
"})",
].join("");
definitions.push(
`export const ${objectName}: ${objectType} = ${objectValue};`
);
break;
}
case "OBJECT": {
@ -90,7 +105,7 @@ export default function generateFlowTypes(
.map((x) => `+${x.fieldname}: ${x.rhs}`)
.join(", ");
const rhs = `{|\n${fieldContents}\n|}`;
typedefs.push(`export type ${typename} = ${rhs};`);
definitions.push(`export type ${typename} = ${rhs};`);
break;
}
case "UNION": {
@ -100,7 +115,7 @@ export default function generateFlowTypes(
: Object.keys(type.clauses)
.sort()
.join(" | ");
typedefs.push(`export type ${typename} = ${rhs};`);
definitions.push(`export type ${typename} = ${rhs};`);
break;
}
// istanbul ignore next: unreachable per Flow
@ -112,7 +127,7 @@ export default function generateFlowTypes(
const rawSource = [
"// @flow",
"// Autogenerated file. Do not edit.",
...typedefs,
...definitions,
].join("\n\n");
return prettier.format(rawSource, prettierOptions);
}

View File

@ -110,6 +110,20 @@ export type PullRequestReviewState =
| "DISMISSED"
| "PENDING";
export const PullRequestReviewState$Values: {|
+APPROVED: "APPROVED",
+CHANGES_REQUESTED: "CHANGES_REQUESTED",
+COMMENTED: "COMMENTED",
+DISMISSED: "DISMISSED",
+PENDING: "PENDING",
|} = Object.freeze({
APPROVED: "APPROVED",
CHANGES_REQUESTED: "CHANGES_REQUESTED",
COMMENTED: "COMMENTED",
DISMISSED: "DISMISSED",
PENDING: "PENDING",
});
export type Reaction = {|
+__typename: "Reaction",
+content: ReactionContent,
@ -125,6 +139,22 @@ export type ReactionContent =
| "THUMBS_DOWN"
| "THUMBS_UP";
export const ReactionContent$Values: {|
+CONFUSED: "CONFUSED",
+HEART: "HEART",
+HOORAY: "HOORAY",
+LAUGH: "LAUGH",
+THUMBS_DOWN: "THUMBS_DOWN",
+THUMBS_UP: "THUMBS_UP",
|} = Object.freeze({
CONFUSED: "CONFUSED",
HEART: "HEART",
HOORAY: "HOORAY",
LAUGH: "LAUGH",
THUMBS_DOWN: "THUMBS_DOWN",
THUMBS_UP: "THUMBS_UP",
});
export type Ref = {|
+__typename: "Ref",
+id: string,