From 4f79e3184da9bdad2d582c9374a706413b0f0f17 Mon Sep 17 00:00:00 2001 From: "Carlos R. L. Rodrigues" Date: Fri, 6 Dec 2024 08:55:55 -0300 Subject: [PATCH] fix(utils): avoid optional properties on graphql generated file --- .changeset/green-pants-remain.md | 5 ++++ .../utils/src/graphql/graphql-to-ts-types.ts | 25 +++++++++++-------- 2 files changed, 19 insertions(+), 11 deletions(-) create mode 100644 .changeset/green-pants-remain.md diff --git a/.changeset/green-pants-remain.md b/.changeset/green-pants-remain.md new file mode 100644 index 0000000000000..6875fb1cc0eab --- /dev/null +++ b/.changeset/green-pants-remain.md @@ -0,0 +1,5 @@ +--- +"@medusajs/utils": patch +--- + +fix: avoid optional fields on graphql generated types diff --git a/packages/core/utils/src/graphql/graphql-to-ts-types.ts b/packages/core/utils/src/graphql/graphql-to-ts-types.ts index b449f6f252f80..7082ae0277e3e 100644 --- a/packages/core/utils/src/graphql/graphql-to-ts-types.ts +++ b/packages/core/utils/src/graphql/graphql-to-ts-types.ts @@ -1,7 +1,7 @@ import { codegen } from "@graphql-codegen/core" -import { type GraphQLSchema, parse, printSchema } from "graphql" import * as typescriptPlugin from "@graphql-codegen/typescript" import { ModuleJoinerConfig } from "@medusajs/types" +import { type GraphQLSchema, parse, printSchema } from "graphql" import { FileSystem } from "../common" function buildEntryPointsTypeMap({ @@ -13,15 +13,15 @@ function buildEntryPointsTypeMap({ }): { entryPoint: string; entityType: any }[] { // build map entry point to there type to be merged and used by the remote query - return joinerConfigs - .flatMap((config) => { - const aliases = Array.isArray(config.alias) - ? config.alias - : config.alias - ? [config.alias] - : [] + return joinerConfigs.flatMap((config) => { + const aliases = Array.isArray(config.alias) + ? config.alias + : config.alias + ? [config.alias] + : [] - return aliases.flatMap((alias) => { + return aliases + .flatMap((alias) => { const names = Array.isArray(alias.name) ? alias.name : [alias.name] const entity = alias?.["entity"] return names.map((aliasItem) => { @@ -35,8 +35,8 @@ function buildEntryPointsTypeMap({ } }) }) - }) - .filter(Boolean) + .filter(Boolean) + }) } async function generateTypes({ @@ -111,6 +111,9 @@ export async function gqlSchemaToTypes({ output: "Record", }, }, + avoidOptionals: { + field: true, // Avoid optional fields in types + }, }, filename: "", schema: parse(printSchema(schema as any)),