-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Admin Generator (Future): Fix generated code for optional number fiel…
…ds (#1759) Make type optional and check if value is defined before parsing into string or number.
- Loading branch information
Showing
6 changed files
with
55 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
packages/admin/cms-admin/src/generator/future/utils/isFieldOptional.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
}; |