File size: 4,847 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
[Table of contents](../../readme.md) / [API](../intro.md) / [GraphQL](./intro.md)
# Fragments
Fragments help us to always know exactly what kind of data will be returned for a given query and ensure that we are consistent in what data should be returned to any given component.
Imagine we wrote two queries like this:
```
# query 1
user {
uid
displayName
}
# query 2
user {
uid
username
}
```
If these queries get implemented in different places, for different components, we're going to end up with a confusing mess where we never really know what information we have access to when writing our react components. We might accidentally write `{ user.username }` expecting that data to exist, but alas we wrote the featuring using query 1. Oops.
Fragments are an important part of how we query our GraphQL API from the frontend and you'll be using them constantly.
## Example
Here's a real-life example of how you would use fragments to query our API:
```js
import { gql } from 'react-apollo';
export const userInfoFragment = gql`
fragment userInfo on User {
uid
photoURL
displayName
username
}
`;
```
And here's how that would look in a query file:
```js
const getUser = gql`
query getUser($username: String, $after: String) {
user(username: $username) {
...userInfo
}
}
${userInfoFragment}
`;
```
By using this fragment, we can always ensure we're getting back the data we need and start to build up an internal mental model of what data we have access to regardless of where we're working in the app.
This is especially useful for when we want to handle some pagination logic. We can just use a fragment to both a) save ourselves the hassle of writing a big query and 2) ensure we haven't forgotten a key field (like `cursor`) during implementation. Like this:
```js
export const userCommunitiesFragment = gql`
fragment userCommunities on User {
communityConnection {
pageInfo {
hasNextPage
hasPreviousPage
}
edges {
node {
...communityInfo
}
}
}
}
${communityInfoFragment}
`;
```
Notice there that we're using a sub-fragment to get the `communityInfo`, too!
That looks like:
```js
export const communityInfoFragment = gql`
fragment communityInfo on Community {
id
name
slug
}
`;
```
And now we've saved ourselves time, and ensured that we will always have access to critical data about the communities, even if we are writing a query against the user.
## Structure

The reason these fragments are so obnoxiously granular is because there are times when you might have circular fragment requirements, which don't play well with webpack at the moment. For example, a user might require a story, which might require a user. So the files are importing each other and it breaks.
To solve this I've made super granular fragments in individual files. I've found it's somewhat useful from a naming convention point of view, and makes it super clear when importing files and actually using the fragments in a query.
## Performance notes
Fragments make it tempting to create really deep queries, but that's a bad idea for performance reasons. Therefore, fragments themselves should be as shallow as possible to combat this, ideally never going more than one level of resources deep. (`story.id` and `story.content.title` are great, `story.community.channels.stories` not so much)
When writing the fragments + queries, I tried to think to myself:
***If I were using this fragment, what data would I absolutely expect to be returned?***
For example, the `storyInfo` fragment looks like:
```js
import { gql } from 'react-apollo';
import { userInfoFragment } from '../user/userInfo';
export const storyInfoFragment = gql`
fragment storyInfo on Story {
id
messageCount
createdAt
modifiedAt
published
deleted
locked
content {
title
description
}
author {
...userInfo
}
}
${userInfoFragment}
`;
```
I included the `author` and `...userInfo` fragment on `storyInfo` because I can't think of a single use case we have where we'd want to show a story *without* the author info. However, note that I didn't include any information about the channel this story was posted under. That's because if I'm *viewing that channel* it would be a massively underperforming query to also include channel data with *each story*.
So instead, if I *do* need the channel data for a story, I add that at the query layer:
```js
const getStory = gql`
query getStory($id: String) {
...storyInfo
channel {
...frequencyInfo
}
}
${storyInfoFragment}
${frequencyInfoFragment}
`;
```
|