feathersjs / feathers

The API and real-time application framework
https://feathersjs.com
MIT License
15.02k stars 745 forks source link

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

Open HoekWax opened 1 year ago

HoekWax commented 1 year ago

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 commented 1 year ago

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

Hareis commented 1 year ago

maybe loss that image