Breeze / breeze-client

Breeze for JavaScript clients
MIT License
38 stars 16 forks source link

executeQueryLocally does not return Entity when EntityQuery has select parameter #66

Closed mahish closed 2 years ago

mahish commented 2 years ago

Im using https://github.com/tschettler/breeze-odata4 adapter, however this should not be affected in any way as far as I understand.

Looks like when using executeQueryLocally with select parameter, I get an array of simple objects: without entityType or entityAspect.

const entityQuery = new EntityQuery({
  from: 'entityTypeName',
  select: ['propertyName'],
});

// returns Entity[]
const { results } = await manager.executeQuery(entityQuery);

// but

// returns Record<string, any>[]
const results = manager.executeQueryLocally(entityQuery);

https://github.com/Breeze/breeze-client/blob/125f4671cb563ed77c86bb60c4bf7bff75af8396/src/entity-manager.ts#L2510

mahish commented 2 years ago

This workaround works but I find it… as a workaround. I would expect to get Entity[] using both strategies for fetching data.

const entityQuery = new EntityQuery({
  from: 'entityTypeName',
  select: ['propertyName', 'entityType'],
});

// returns `{ propertyName: any, entityType: EntityType }`
const results = manager.executeQueryLocally(entityQuery);
marcelgood commented 2 years ago

That is the expected behavior. Select does a projection into a simple object using the specified properties. If you run a select against the server it's the same thing. You get simple JS objects. The objects are no longer entities at that point as they no longer match the shape and don't have an id. Even if you select the id, if they came back as entities they would get merged into the cache potentially over the full entities and you would lose data, especially if you save them back to the DB.

mahish commented 2 years ago

@marcelgood Thank you for clarification. Make sense and as I can see it is also clearly noted in documentation http://breeze.github.io/doc-js/api-docs/classes/entityquery.html#select . However it is not the case for me. The behaviour is inconsistent. See the screenshot:

image

So, it seems the behaviour of executeQuery (not executeQueryLocally) is not the consistent in my case. And since executeQuery is implemented by breeze-odata4 (link), I should ask there.

marcelgood commented 2 years ago

Yes, that would be an issue with the breeze-odata4 adapter. We didn't write this adapter, nor do we support it. Running a projection query against the server (executeQuery) is supposed to return plain objects. As the documentation states, there can be entities nested, such as when you select a navigation property, but the root object is a plain object. This is how it works with the adapters that are supplied with Breeze out of the box.