Relay Modern node does not contain fragment properties

I have the following setup in my React project:

export default class OverviewScreen extends React.Component<any, any> {

public render() {

    return (
        <QueryRenderer
            environment={environment}
            query={OverviewScreenQuery}
            render={this.queryRender}/>
    );
}

protected queryRender({error, props}): JSX.Element {

    if (error) {
        return <div>{error.message}</div>;
    } else if (props) {
        return (
            <div>
                <div>
                    <ActivityOfferList viewer={props.viewer} title="Titel"/>
                    <ActivityTypeListsFragment viewer={props.viewer}/>
                </div>
            </div>
        );
    }
    return <div>Loading...</div>;
}
}

const OverviewScreenQuery = graphql`
query OverviewScreenQuery {
    viewer {
        ...HorizontalOfferList_viewer
        ...ActivityTypeLists_viewer
    }
}`;

      


class ActivityTypeLists extends React.Component<IHorizontalOfferListProps, any> {

public render() {

    return (
        <div>
        {this.props.viewer.allActivityTypes.edges.map((typeEdge) => {
            let typeNode = typeEdge.node;
            return this.getCardListForActivityType(typeNode);
        })}
        </div>
    );
}

private getCardListForActivityType(typeNode: any) {
    console.log(typeNode);

    return (
        <CardList key={typeNode.__id} title={typeNode.title}>
            {typeNode.activities.edges.map(({node}) => {
                return (
                    <RelayPicturedTypeActivityCard key={node.__id} offer={node} activityType={typeNode}/>
                );
            })}
        </CardList>
    );
}
}

export const ActivityTypeListsFragment = createFragmentContainer(ActivityTypeLists, graphql`
fragment ActivityTypeLists_viewer on Viewer {
    allActivityTypes(first: 5) {
        edges {
            node {
                ...PicturedTypeActivityCard_offer
            }
        }
    }
}
`);

      


export class PicturedTypeActivityCard extends React.Component<any, any> {

    public render() {
        return (
            <PicturedCard title={this.props.offer.title} subtitle={this.props.activityType.title} width={3}/>
        );
    }
}

export const RelayPicturedTypeActivityCard = createFragmentContainer(PicturedTypeActivityCard, graphql`
    fragment PicturedTypeActivityCard_offer on ActivityType {
        title
        activities(first: 4) {
            edges {
            node {
                id
                title
            }
        }
    }
    }
`);

      

Which should work and give me the correct output from the graphcool relay endpoint.

The network call to the relay endpoint is indeed correct and I am getting all ActivityTypes and their activities and headers from my endpoint.

But somehow, in the getCardListForActivityType () function, the Node type contains only the __id node as data and has no name at all:

enter image description here

If I insert the title and actions directly instead of using

...PicturedTypeActivityCard_offer

      

then the data will also be transferred correctly. So something with Fragment must be turned off.


Why is the network call complete and using the fragment correctly to retrieve data, but the node object never gets the retrieved data?

+3


source to share


1 answer


This is indeed the correct behavior.

Your components have to individually specify all their own data dependencies, Relay only passes in the component it requested. Since your component is not asking for any data, it receives an empty object.



This __id

one you see is used internally by Relay and you shouldn't rely on it (which is why it is prefixed __

).

Basically, the prop viewer

on component ActivityTypeLists

will have exactly the same format as the request requested by the fragment ActivityTypeLists_viewer

, without any other fragments of other components that you reference there.

0


source







All Articles