Closed AshotN closed 1 year ago
I'm not sure if this issue is actually solvable in any reasonable way. So my solution is to have a seperate queryResolver like so
export const companyFilterQueryResolver = resolve<Company, HookContext>({
name: async (value, obj, context) => {
if (context.params.user) {
return "Something"
}
return value
}
})
and just add it to my hooks under the regular queryResolver
schemaHooks.resolveQuery(companyQueryResolver),
schemaHooks.resolveQuery(companyFilterQueryResolver)
My use case for queryResolvers is to create filters for what the user is allowed to fetch
I have an ownership pattern something like this,
An invoice that is owned by a company A company that is controlled by a user
So if a user wants to access a invoice, the invoice query resolver gets the company ids like this
company_id: async (value, obj, context: HookContext<InvoiceService>) => {
const { user } = context.params
if (!user) return value
const companies: { id: string }[] = await app
.service('company')
.find({ user, paginate: false, query: { $select: ['id'] } })
const companyIds = companies.map((c) => c.id)
return { $in: companyIds }
}
The company query resolver does another check like this
controlling_user_id: async (value, obj, context) => {
if (context.params.user) {
return context.params.user.id
}
return value
}
This allows my invoice query to only return invoices that are owned by companies that are controlled by the requesting user
If this all seems correct @daffl I can close this issue as it was clearly user error
Steps to reproduce
Given a simple schema,
And we want only the
active
field to be publicly queryable.Expected behavior
No TS errors
Actual behavior
Tell us what happens instead
Typescript complains that name is not an available property
Likewise trying to access
name
as a query property in a hook will give a TS errorSystem configuration
Tell us about the applicable parts of your setup.