Fragments
Before diving into the data connect, let’s see how to define fragments in Relate. Instead of setting up GraphQL fragments as you do in Relay, in Relate fragments are defined as a simple, plain JavaScript object. You do this by attributing the properties you want with a truthy value, can be 1
or true
. For example:
1 | { |
You can also define nested fragments like so:
1 | { |
Relate also ships with a function to merge fragments with which you can grab in fragments that come from children components and merge them together.
Let’s see an example. Suppose we want to create a component that displays an user profile picture and his name and email on the side. Let’s not worry with data connect and passing props for now, just fragments build:
1 | import React, {Component} from 'react'; |
Pretty clean, you’re merging the fragments from the components the User
component is rendering. Let’s suppose that UserPicture
and UserInfo
have the following fragments:
1 | import React, {Component} from 'react'; |
1 | import React, {Component} from 'react'; |
Simple example just to demonstrate two things, first the component will receive through props the data they asked for, secondly the mergeFragments
will merge the two fragments into:
1 | { |
Static fragments should be placed in presentational components which require data from the server. Your container components will be responsible for handling variables and is where the data connect comes to action as we’ll demonstrate in the next page.