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

Admin Generator (Future): Fix generated code for optional number fields #1759

Merged
merged 7 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
19 changes: 8 additions & 11 deletions packages/admin/cms-admin/src/generator/future/generateForm.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { IntrospectionQuery } from "graphql";

import { generateFormField } from "./generateFormField";
import { FormConfig, GeneratorReturn } from "./generator";
import { FormConfig, FormFieldConfig, GeneratorReturn } from "./generator";
import { camelCaseToHumanReadable } from "./utils/camelCaseToHumanReadable";
import { findRootBlocks } from "./utils/findRootBlocks";
import { generateImportsCode, Imports } from "./utils/generateImportsCode";
import { isFieldOptional } from "./utils/isFieldOptional";

export function generateForm(
{
Expand All @@ -28,13 +29,9 @@ export function generateForm(
const numberFields = config.fields.filter((field) => field.type == "number");
const booleanFields = config.fields.filter((field) => field.type == "boolean");

const isFieldOptional = (fieldName: string): boolean => {
const schemaEntity = gqlIntrospection.__schema.types.find((type) => type.kind === "OBJECT" && type.name === gqlType);
if (!schemaEntity) throw new Error(`didn't find entity ${gqlType} in schema types`);
if (schemaEntity.kind !== "OBJECT") throw new Error(`kind of ${gqlType} is not object, but should be.`); // this should not happen
const fieldDef = schemaEntity.fields.find((field) => field.name === fieldName);
if (!fieldDef) throw new Error(`didn't find field ${fieldName} of ${gqlType} in introspected gql-schema.`);
return fieldDef.type.kind !== "NON_NULL";
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const isFieldOptionalWrapper = (fieldConfig: FormFieldConfig<any>) => {
return isFieldOptional({ config: fieldConfig, gqlIntrospection: gqlIntrospection, gqlType: gqlType });
};

const fragmentName = config.fragmentName ?? `${gqlType}Form`;
Expand Down Expand Up @@ -156,7 +153,7 @@ export function generateForm(
} ${
numberFields.length > 0 || Object.keys(rootBlocks).length > 0
? `& {
${numberFields.map((field) => `${String(field.name)}${isFieldOptional(String(field.name)) ? `?` : ``}: string;`).join("\n")}
${numberFields.map((field) => `${String(field.name)}${isFieldOptionalWrapper(field) ? `?` : ``}: string;`).join("\n")}
${Object.keys(rootBlocks)
.map((rootBlockKey) => `${rootBlockKey}: BlockState<typeof rootBlocks.${rootBlockKey}>;`)
.join("\n")}
Expand Down Expand Up @@ -186,7 +183,7 @@ export function generateForm(
${numberFields
.map((field) => {
let assignment = `String(data.${instanceGqlType}.${String(field.name)})`;
if (isFieldOptional(String(field.name))) {
if (isFieldOptionalWrapper(field)) {
assignment = `data.${instanceGqlType}.${String(field.name)} ? ${assignment} : undefined`;
}
return `${String(field.name)}: ${assignment},`;
Expand Down Expand Up @@ -222,7 +219,7 @@ export function generateForm(
${numberFields
.map((field) => {
let assignment = `parseFloat(formValues.${String(field.name)})`;
if (isFieldOptional(String(field.name))) {
if (isFieldOptionalWrapper(field)) {
assignment = `formValues.${String(field.name)} ? ${assignment} : null`;
}
return `${String(field.name)}: ${assignment},`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { IntrospectionEnumType, IntrospectionNamedTypeRef, IntrospectionObjectTy
import { FormConfig, FormFieldConfig, GeneratorReturn } from "./generator";
import { camelCaseToHumanReadable } from "./utils/camelCaseToHumanReadable";
import { Imports } from "./utils/generateImportsCode";
import { isFieldOptional } from "./utils/isFieldOptional";

export function generateFormField(
{ gqlIntrospection }: { gqlIntrospection: IntrospectionQuery },
Expand All @@ -26,9 +27,7 @@ export function generateFormField(
if (!introspectionField) throw new Error(`didn't find field ${name} in gql introspection type ${gqlType}`);
const introspectionFieldType = introspectionField.type.kind === "NON_NULL" ? introspectionField.type.ofType : introspectionField.type;

const requiredByIntrospection = introspectionField.type.kind == "NON_NULL";

const required = config.required ?? requiredByIntrospection; //if undefined default to requiredByIntrospection
const required = isFieldOptional({ config, gqlIntrospection, gqlType });

//TODO verify introspectionField.type is compatbile with config.type

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { IntrospectionQuery } from "graphql";

import { FormFieldConfig } from "../generator";

export const isFieldOptional = ({
config,
gqlIntrospection,
gqlType,
}: {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
config: FormFieldConfig<any>;
gqlIntrospection: IntrospectionQuery;
gqlType: string;
}): boolean => {
if (config.required) return false;
const schemaEntity = gqlIntrospection.__schema.types.find((type) => type.kind === "OBJECT" && type.name === gqlType);
if (!schemaEntity) throw new Error(`didn't find entity ${gqlType} in schema types`);
if (schemaEntity.kind !== "OBJECT") throw new Error(`kind of ${gqlType} is not object, but should be.`); // this should not happen
const fieldDef = schemaEntity.fields.find((field) => field.name === String(config.name));
if (!fieldDef) throw new Error(`didn't find field ${String(config.name)} of ${gqlType} in introspected gql-schema.`);
return fieldDef.type.kind !== "NON_NULL";
};
Loading