feathers icon indicating copy to clipboard operation
feathers copied to clipboard

Typebox, being able to query a reference. Include the schema

Open HoekWax opened this issue 1 year ago • 2 comments

Hello, I have a table Portal that has multiple PortalUser (basically Ticket and Ticket-user)

in my portalSchema I have an array of PortalUser

portalUsers: Type.Array(Type.Ref(portalUserSchema)),

I can resolve them like so

export const portalResolver = resolve<Portal, HookContext>({
    portalUsers: virtual(async (portal, context) => {
        return context.app.service('portal-user').find({ query: { portalId: portal.id }, paginate: false })
    })
})

but if I try to add "portalUsers" to the queryProperties like so

export const portalQueryProperties = Type.Pick(portalSchema, [
    'id',
    'status',
    'deletedAt',
    'teamId',
    'portalUsers'
])

I get this error

can't resolve reference PortalUser from id #
Error: can't resolve reference PortalUser from id #
    at Object.code (/Users/geraudm/Documents/Work/fixly/fixly-server/node_modules/ajv/lib/vocabularies/core/ref.ts:19:39)
    at keywordCode (/Users/geraudm/Documents/Work/fixly/fixly-server/node_modules/ajv/lib/compile/validate/index.ts:532:9)
    at /Users/geraudm/Documents/Work/fixly/fixly-server/node_modules/ajv/lib/compile/validate/index.ts:228:21
    at CodeGen.code (/Users/geraudm/Documents/Work/fixly/fixly-server/node_modules/ajv/lib/compile/codegen/index.ts:525:33)
    at CodeGen.block (/Users/geraudm/Documents/Work/fixly/fixly-server/node_modules/ajv/lib/compile/codegen/index.ts:680:20)

event if the id is already set by feathers.

HoekWax avatar Apr 21 '23 15:04 HoekWax

Something that can work for you if you have this issue

Convert something like this

export const portalQueryProperties = Type.Pick(portalSchema, [
    'id',
    'status',
    'deletedAt',
    'teamId',
    'portalUsers'
])
export const portalQuerySchema = Type.Intersect(
    [
        querySyntax(portalQueryProperties),
        // Add additional query properties here
        Type.Object({}, { additionalProperties: false })
    ],
    { additionalProperties: false }
)
export type PortalQuery = Static<typeof portalQuerySchema>
export const portalQueryValidator = getValidator(portalQuerySchema, queryValidator)
export const portalQueryResolver = resolve<PortalQuery, HookContext>({})

to this

export const portalQueryProperties = Type.Pick(portalSchema, [
    'id',
    'status',
    'deletedAt',
    'teamId',
    'portalUsers'
])
export const portalQuerySchema = Type.Intersect(
    [
        querySyntax(portalQueryProperties),
        // Add additional query properties here
        Type.Object({}, { additionalProperties: false })
    ],
    { additionalProperties: false }
)
export type PortalQuery = Static<typeof portalQuerySchema>

queryValidator,addSchema(portalUserSchema) // 👈

export const portalQueryValidator = getValidator(portalQuerySchema, queryValidator)
export const portalQueryResolver = resolve<PortalQuery, HookContext>({})

You need to user addSchema on the queryValidator

HoekWax avatar Apr 21 '23 16:04 HoekWax

maybe loss that image

Hareis avatar Apr 22 '23 00:04 Hareis