0

Consider I have a simple graph representing many to many relation with relation table.

Person[id,name] <-- Permission[id,person_id,team_id,permission_string] --> Team[id,name]

So basically any person can have multiple permissions regarding any team.

Relation is bidirectional and can be queried from both sides:

query People {
    people {
        id
        name
        permissions {
            id
            permission // string
            team {
                id
            }
        }
    }
}

query Teams {
    teams {
        id
        name
        permissions {
            id
            permission // string
            person {
                id
            }
        }
    }
}

Now if You think about it - there is nothing stopping me from doing:

query Teams {
    teams {
        id
        name
        permissions {
            id
        }
    }
}

And getting permissions by id.

I could even have 3 separate queries for people, teams and permission entities, query once for each and then move around graph by ids:

query People {
    people {
        id
        name
        permissions {
            id
        }
    }
}

query Teams {
    teams {
        id
        name
        permissions {
            id
        }
    }
}

query Permissions {
    permissions {
        id
        permission
        person {
            id
        }
        team {
            id
        }
    }
}

I guess the question is - how does data normalization in Apollo Angular cache play role here?

I know that we can point query fields to cache using read and toReference, but that is for known query params, for example read field from cache based on given query variable.

Would that mean I need to set up all possible queries:

people
person(id)
teams
team(id)
permissions
permission(id)

and then create typePolicies for person, team and permission to point at cache, like below:

Query: {
    fields: {
        person: {
            read(_, { args, toReference }) {
                return toReference({
                    __typename: 'Person',
                    id: args['id'],
                });
            },
        },

So now I would have to query first for all people, teams and permissions, and then iterate over results and query for each separately hoping cache will kick in and return without fetching server?

So for example:

person(10) would return permissions: [5,7,8] and I would query permission(5), permission(7), permission(8) and it would return data from cache because of typePolicies pointing there?

Is this the intended approach? Won't it be slow for larger data? How else to tackle this?

Ernio
  • 948
  • 10
  • 25

0 Answers0