Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc7078a1

Browse files
captbaritonefacebook-github-bot
authored andcommitted
Iteration on new quick start guide based on feedback (#4966)
Summary:Based on feedback from cpojer (some of which echoed other feedback I've received), this updates the new quickstart guide to clarify some details:1. Configures babel plugin via the Vite React plugin rather than needing to use the vite Relay plugin2. Clarifies what `relay.config.json` is for3. Clarifies that the `<RelayEnvironmentPrivider>` must nest around all other components.4. Adds defining a fragment component to the guide (echoing feedback evanyeung gave earlier).5. Clarify what running the compiler doesPull Requestresolved:#4966Reviewed By: evanyeungDifferential Revision: D73519975Pulled By: captbaritonefbshipit-source-id: eb45049c5ac89bc133840a6ecbd3ce3726968bc9
1 parentbd321a2 commitc7078a1

File tree

1 file changed

+50
-17
lines changed

1 file changed

+50
-17
lines changed

‎website/docs/getting-started/quick-start.md

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,32 +29,30 @@ You’ll be prompted for a project name. Type: `relay-example`
2929
```bash
3030
cd relay-example
3131

32-
# Note: Version 18^ of Relay is missing React 19 as a peer dependency. This will be fixed in the next release. Until then we must add `--force` to the following commands.
32+
# Note: Version 18^ of Relay is missing React 19 as a peer dependency. To avoid
33+
# this, we are using the `@main` versions of the Relay packages until the next Relay release
3334

3435
# Runtime dependencies
35-
npm install relay-runtime react-relay --force
36+
npm install relay-runtime@main react-relay@main
3637
# Dev dependencies
37-
npm install --devvite-plugin-relay relay-compiler --force
38+
npm install --devbabel-plugin-relaygraphqlrelay-compiler@main
3839
# Types
39-
npm install --dev @types/relay-runtime @types/react-relay --force
40+
npm install --dev @types/relay-runtime @types/react-relay
4041
```
4142

4243
##Configure Vite to use Relay
4344

44-
Relay uses a[Babel plugin](./babel-plugin.md) to insert code generated by the Relay compiler into your bundle. We can enablethat transform in Vite withthe`vite-plugin-relay`plugin:
45+
Relay uses a[Babel plugin](./babel-plugin.md) to insert code generated by the Relay compiler into your bundle. We can enablethe Relay Babel plugin we installed earliery by configuringtheReact Viteplugin.
4546

4647
```tsx title="vite.config.ts"
4748
import {defineConfig }from'vite'
4849
importreactfrom'@vitejs/plugin-react'
49-
// change-line
50-
importrelayfrom'vite-plugin-relay';
5150

5251
// https://vite.dev/config/
5352
exportdefaultdefineConfig({
5453
plugins: [
55-
react(),
5654
// change-line
57-
relay
55+
react({ babel: { plugins: ["relay"] } })
5856
],
5957
})
6058
```
@@ -63,12 +61,14 @@ See [Babel Plugin](./babel-plugin.md) for information about how to configure the
6361

6462
##Configure the Relay Compiler
6563

66-
Next we will download the schema for the Star Wars GraphQL endpoint and configure the Relay compiler to use that schema:
64+
Next we will download theGraphQLschema for the Star Wars GraphQL endpoint.
6765

6866
```bash
6967
curl -O https://raw.githubusercontent.com/graphql/swapi-graphql/refs/heads/master/schema.graphql
7068
```
7169

70+
And define our`relay.config.json` config file which tells the[Relay Compiler](./compiler.md) which schema file we want it to use and other details about our project.
71+
7272
```json title="relay.config.json"
7373
{
7474
"src":"./src",
@@ -130,13 +130,18 @@ createRoot(document.getElementById("root")!).render(
130130

131131
See[Relay Environment](../api-reference/relay-runtime/relay-environment.md) for an overview of the Relay Environment and how to configure it.
132132

133+
:::tip
134+
`<RelayEnvironmentProvider>` exposes your Environment via[React context](https://react.dev/learn/passing-data-deeply-with-context), so it must wrap your entire application.
135+
:::
136+
133137
##Define your first Relay component
134138

135-
Finally we can start defining the data we want to fetch and build our UI:
139+
Finally we can start defining the data we want to fetch and build our UI. Our app will fetch a list of films and render each one using a`<Film>` component.
136140

137141
```tsx title="src/App.tsx"
138142
import {AppQuery }from"./__generated__/AppQuery.graphql";
139143
import {graphql,useLazyLoadQuery }from"react-relay";
144+
importFilmfrom"./Film";
140145

141146
exportdefaultfunction App() {
142147
const data=useLazyLoadQuery<AppQuery>(
@@ -145,8 +150,7 @@ export default function App() {
145150
allFilms {
146151
films {
147152
id
148-
title
149-
director
153+
...Film_item
150154
}
151155
}
152156
}
@@ -160,21 +164,48 @@ export default function App() {
160164
<div>
161165
<h1>Star Wars Films</h1>
162166
{films?.map((film)=> (
163-
<likey={film.id}>
164-
<b>{film.title}</b>: directed by <i>{film.director}</i>
165-
</li>
167+
<Filmkey={film.id}film={film} />
166168
))}
167169
</div>
168170
);
169171
}
170172
```
171173

174+
##Define your first fragment
175+
176+
One of Relay's core principles is that each component should define its own data dependencies. So, we define our`<Film>` component using a[GraphQL Fragment](https://graphql.org/learn/queries/#fragments).
177+
178+
```typescript title="src/Film.tsx"
179+
import {graphql,useFragment }from"react-relay";
180+
importtype {Film_item$key }from"./__generated__/Film_item.graphql";
181+
182+
exportdefaultfunction FilmListItem(props: { film:Film_item$key; }) {
183+
const film=useFragment<Film_item$key>(
184+
graphql`
185+
fragment Film_item on Film {
186+
title
187+
director
188+
}
189+
`,
190+
props.film
191+
);
192+
193+
return (
194+
<li>
195+
<b>{film.title}</b>: directed by <i>{film.director}</i>
196+
</li>
197+
);
198+
}
199+
```
200+
172201
##Compile and run your app
173202

174203
All that’s left is to run the Relay compiler and start your app!
175204

205+
The Relay compiler generates TypeScript types and combines your queries and fragments into optimized representations. You have to run the Relay compiler each time you modify your GraphQL queries or fragments.
206+
176207
:::tip
177-
If you have[Watchman](https://facebook.github.io/watchman/) installed, run`echo "{}" > .watchmanconfig` to create a Watchman root.
208+
If you have[Watchman](https://facebook.github.io/watchman/) installed you can run`npx relay-compiler --watch` to have the compiler run in watch mode, but you'll need to run`echo "{}" > .watchmanconfig` to create a Watchman root.
178209
:::
179210

180211
```bash
@@ -183,3 +214,5 @@ npm run dev
183214
```
184215

185216
You should now be able to open your app in a browser:[http://localhost:5173/](http://localhost:5173/)
217+
218+
May the force be with you!

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp