lukemorales / query-key-factory

A library for creating typesafe standardized query keys, useful for cache management in @tanstack/query
https://www.npmjs.com/package/@lukemorales/query-key-factory
MIT License
1.16k stars 32 forks source link

why does `enabled` not work within the query-key-factory? #61

Closed ethanneff closed 1 year ago

ethanneff commented 1 year ago

Why does this fail?

const query = useQuery(queries.shipmentFees.available(params, enabled));

export const queries = createQueryKeyStore({
  shipmentFees: {
    available: (params: ShipmentFeesEndpointFetchAvailableRequest, enabled: boolean) => ({
      enabled,
      queryKey: [apiEndpointHandler, params],
      queryFn: () => ShipmentFeesEndpoint(apiEndpointHandler).fetchAvailable(params, { splitArrayParams: true }),
    }),
  },
});

And this work?

const query = useQuery({ ...queries.shipmentFees.available(params), enabled });

export const queries = createQueryKeyStore({
  shipmentFees: {
    available: (params: ShipmentFeesEndpointFetchAvailableRequest) => ({
      queryKey: [apiEndpointHandler, params],
      queryFn: () => ShipmentFeesEndpoint(apiEndpointHandler).fetchAvailable(params, { splitArrayParams: true }),
    }),
  },
});
lukemorales commented 1 year ago

The simple answer is: it doesn't work because it was never supposed to work (sorry if this sounds rude, but that's just it). The documentation and the type system is pretty clear about what properties the objects accept.