Skip to content

Commit

Permalink
fix: type merge
Browse files Browse the repository at this point in the history
  • Loading branch information
Akryum committed Mar 14, 2024
1 parent 659e6d6 commit e37d5b4
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
8 changes: 4 additions & 4 deletions packages/core/src/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import type { ResolvedGraphQLSchema } from './graphql/schema.js'
import type { MoquerieInstance } from './instance.js'
import type { SchemaTransformStore } from './resource/schemaTransformStore.js'
import { hooks } from './hooks.js'
import { getTypesFromFile } from './resource/fromTypes.js'
import { type PartialResourceSchemaType, getTypesFromFile } from './resource/fromTypes.js'

export async function getResourceSchema(mq: MoquerieInstance, schemaTransformStore: SchemaTransformStore) {
const ctx = await mq.getContext()

const types: ResourceSchemaType[] = []
const types: Array<ResourceSchemaType | PartialResourceSchemaType> = []

// REST

Expand Down Expand Up @@ -97,8 +97,8 @@ export async function getResourceSchema(mq: MoquerieInstance, schemaTransformSto
})
}
else {
mergeMap.set(type.name, type)
mergedTypes.push(type)
mergeMap.set(type.name, type as ResourceSchemaType)
mergedTypes.push(type as ResourceSchemaType)
}
}

Expand Down
17 changes: 5 additions & 12 deletions packages/core/src/resource/fromTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import ts from 'typescript'
import type { MoquerieInstance } from '../instance.js'
import type { ResourceSchemaField, ResourceSchemaType } from '../types/resource.js'

export type PartialResourceSchemaType = Omit<ResourceSchemaType, 'nonNull' | 'array' | 'inline'>

export async function getTypesFromFile(mq: MoquerieInstance, files: string[], baseTags: string[] = []) {
const types: ResourceSchemaType[] = []
const types: PartialResourceSchemaType[] = []

const program = ts.createProgram(files.map(f => path.resolve(mq.data.cwd, f)), {})
const checker = program.getTypeChecker()
Expand All @@ -29,8 +31,6 @@ export async function getTypesFromFile(mq: MoquerieInstance, files: string[], ba
for (const node of typeNodes) {
const fields: Record<string, ResourceSchemaField> = {}

let inline = true

node.members.forEach((member) => {
const fieldName = member.name?.getText()
if (!fieldName) {
Expand Down Expand Up @@ -93,10 +93,6 @@ export async function getTypesFromFile(mq: MoquerieInstance, files: string[], ba

const tags = ['field', ...baseTags]

if (['id', '_id'].includes(fieldName)) {
inline = false
}

const jsDoc = nameSymbol?.getJsDocTags()
let isDeprecated = false
let deprecationReason: string | undefined
Expand Down Expand Up @@ -129,7 +125,7 @@ export async function getTypesFromFile(mq: MoquerieInstance, files: string[], ba
const meta: Record<string, any> = {}

const jsDocTags = typeSymbol?.getJsDocTags()
let isDeprecated = false
let isDeprecated: boolean | undefined
let deprecationReason: string | undefined
if (jsDocTags) {
for (const tag of jsDocTags) {
Expand All @@ -146,14 +142,11 @@ export async function getTypesFromFile(mq: MoquerieInstance, files: string[], ba
name: node.name.text,
tags,
description: typeSymbol?.getDocumentationComment(checker)?.find(c => c.kind === 'text')?.text ?? undefined,
array: true,
fields,
nonNull: false,
isDeprecated,
deprecationReason,
inline,
meta,
} satisfies ResourceSchemaType
} satisfies PartialResourceSchemaType

types.push(resType)
}
Expand Down
21 changes: 21 additions & 0 deletions packages/core/src/rest/schema.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,33 @@
import type { MoquerieInstance } from '../instance.js'
import { getContext } from '../context.js'
import { getTypesFromFile } from '../resource/fromTypes.js'
import type { ResourceSchemaType } from '../types/resource.js'

export async function getRestResourceSchema(mq: MoquerieInstance) {
const ctx = await getContext(mq)

if (ctx.config.rest?.typeFiles) {
const { types } = await getTypesFromFile(mq, ctx.config.rest.typeFiles, ['rest'])

const finalTypes: Array<ResourceSchemaType> = []

for (const key in types) {
const type = types[key]

// Inline
let inline = true
if (Object.keys(type.fields).some(field => ['id', '_id'].includes(field))) {
inline = false
}

finalTypes.push({
...type,
inline,
nonNull: false,
array: true,
})
}

return {
types,
}
Expand Down

0 comments on commit e37d5b4

Please sign in to comment.