Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
    MG.
    @satanTime
    Hi there, feel free to ask any questions about the lib.
    Walid Elalaouy
    @WalidEll
    Hello, any hint on how to get entities in relation to the root entity if it doesn't exist in the store
    MG.
    @satanTime
    Hi. Do you mean to get staff without company?
    Walid Elalaouy
    @WalidEll
    Imagine we fetched the user and the company from the Remote API, the company has an address with the id 1 but it doesn't exist in the store, the actual behavior will link the user and the company because they are in the store but the address will remain null, how can I detect and trigger the service to fetch the address from remote storage
    MG.
    @satanTime
    Ah, I see. A good question. Currently I use it for GraphQL, or I use meta to detect required relationships, something like company/ID?relations=user,user.manager,user.manager.tasks.
    I would recommend to fetch all the data when the component has been loaded, otherwise there may be data collisions, when the store has company, but it has been removed on the backend with its address.
    MG.
    @satanTime
    I usually do like that - when a component / route has been initialized - there is are quest to backend to which is based on related selector (to know relationships), once backend has the response, it uses fromFlat / fromGraph to put data into the store. So a load of a component / route always refreshes the whole shape of related entities, not only missed ones.
    Walid Elalaouy
    @WalidEll
    I see, thank you :D
    MG.
    @satanTime

    No problem.

    There is an example how I collect all relationships to send a request to load them.
    I think, you could adapt it for your needs to verify which data is missed in a selected entity and to send an additional request in order to load it.

    export const selectorToRelations = (
        selector: {relationships: Array<HANDLER_RELATED_ENTITY<unknown, unknown>>},
        prefix: string = '',
    ): Array<string> => {
        const relationships: Array<string> = [];
        for (const relationship of selector.relationships) {
            if (typeof relationship.keyValue === 'symbol') {
                continue;
            }
            const key = `${prefix ? `${prefix}.` : ''}${relationship.keyValue}`;
            if (!relationships.includes(key)) {
                relationships.push(key);
            }
            relationships.push(...selectorToRelations(relationship, key));
        }
    
        return relationships;
    };