Skip to content

Commit

Permalink
fix(utils): avoid optional properties on graphql generated file
Browse files Browse the repository at this point in the history
  • Loading branch information
carlos-r-l-rodrigues committed Dec 6, 2024
1 parent 7e04091 commit 4f79e31
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/green-pants-remain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@medusajs/utils": patch
---

fix: avoid optional fields on graphql generated types
25 changes: 14 additions & 11 deletions packages/core/utils/src/graphql/graphql-to-ts-types.ts
Original file line number Diff line number Diff line change
@@ -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({
Expand All @@ -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) => {
Expand All @@ -35,8 +35,8 @@ function buildEntryPointsTypeMap({
}
})
})
})
.filter(Boolean)
.filter(Boolean)
})
}

async function generateTypes({
Expand Down Expand Up @@ -111,6 +111,9 @@ export async function gqlSchemaToTypes({
output: "Record<string, unknown>",
},
},
avoidOptionals: {
field: true, // Avoid optional fields in types
},
},
filename: "",
schema: parse(printSchema(schema as any)),
Expand Down

0 comments on commit 4f79e31

Please sign in to comment.