Includes

From SocialStack
Revision as of 14:38, 2 December 2021 by Luke (talk | contribs) (Created page with "Includes are a mechanism in SocialStack which allow you to request additional content when obtaining an entity (or a list of entity) from the API. For example, you have a loop...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Includes are a mechanism in SocialStack which allow you to request additional content when obtaining an entity (or a list of entity) from the API. For example, you have a loop of things and would like to also display the name of the user who created each one. They are not case sensitive.

Frontend Usage

Typically includes are used from either Loop or Content:

    import Loop from 'UI/Loop';
    
    ...
    
    <Loop over='page/list' includes={['creatorUser']}>
        {page => {
            return <div>
                The page "{page.title}" was created by {page.creatorUser.username}
            </div>;
        }}
    </Loop>
    import Content from 'UI/Content';
    
    ...
    
    <Content type='page' id={1} includes={['creatorUser']}>
        {page => {
            if(!page){
                // Just an example. The page might also not exist. See <Content> usage for more info.
                return 'Loading..';
            }
            
            return <div>
                The page "{page.title}" was created by {page.creatorUser.username}
            </div>;
        }}
    </Content>

If you aren't in the React render scope, you can specify includes via a webRequest as well:


    webRequest('page/1', null, {includes: ['creatorUser']});

API Usage

The frontend is actually just adding any includes on as a GET parameter, like so:

https://www.example.com/v1/page/1?includes=creatorUser

Nested Includes

You will often have situations where you want to include things on the included objects as well. This is done with a dot:

    ['creatorUser', 'creatorUser.tags']


Show me everything

You can wildcard include to show all available current level includes, and this is available nested as well:

    ['*', 'creatorUser.*']

Note that the wildcard is only supported as the last field (*.creatorUser is not available for example, as not everything necessarily supports the named field you want).