Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add blog post highlighting changes in Relay 15 #4258

Closed
wants to merge 6 commits into from
Closed
153 changes: 153 additions & 0 deletions website/blog/2023-03-30-relay-15.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
---
title: Relay v15.0 and VSCode Extension v2.0
author: The Relay Team
hide_table_of_contents: false
---

The Relay team is happy to announce the release of Relay v15 as well as v2 of the Relay VSCode extension. These releases are a long time coming and include lots of new feature, bug fixes and quality of life improvements. While this release is a major version bump and includes a couple breaking changes, few users should be affected and upgrading should be seamless for most use cases. You can find the full list of changes in the [v15 Release Notes](https://github.com/facebook/relay/releases/tag/v15.0.0).
ernieturner marked this conversation as resolved.
Show resolved Hide resolved

## What's new in Relay 15?

### Relay Resolver improvements

A significant portion of our development effort since our last release has gone into improving [Relay Resolvers](https://relay.dev/docs/guides/relay-resolvers) (a mechanism for exposing derived data in the graph). It is worth noting that Relay Resolvers are still experimental and API changes might occur in the future.

#### Terser docblock tags

We've also created a simplified format of the docblock tags necessary to create a Relay Resolver. In many scenarios you can now use a simplified `ParentType.field_name: ReturnType` syntax to define what new field your Relay Resolver exposes.

Before:

```js
/**
* @RelayResolver
* @onType User
* @fieldName favorite_page
* @rootFragment myRootFragment
*/
```

After:

```js
/**
* @RelayResolver User.favorite_page: Page
* @rootFragment myRootFragment
*/
```

#### Define multiple resolvers per file

Prior to this release we only allowed a single Relay Resolver per file and required the Relay Resolver function to be the default export. In Relay 15 you're now able to define multiple Relay Resolvers per file and use named exports. In order to use this feature you'll need to enable the `use_named_imports_for_relay_resolvers` feature flag in your Relay config.

```js
/**
* @RelayResolver User.favorite_page: Page
* @rootFragment favoritePageFragment
*/
function usersFavoritePage(){
...
}

/**
* @RelayResolver User.best_friend: Friend
* @rootFragment bestFriendFragment
*/
function usersBestFriend(){
...
}

module.exports = {
usersFavoritePage,
usersBestFriend
}
```

### Support `@refetchable` on interfaces
ernieturner marked this conversation as resolved.
Show resolved Hide resolved

Previously it wasn't possible to add the `@refetchable` directive to fragments on server interface types. That support has now been added and will no longer error in the Relay compiler.
ernieturner marked this conversation as resolved.
Show resolved Hide resolved

```
// schema.graphql

interface RefetchableInterfaceFoo @fetchable(field_name: "id") {
id: ID!
}

extend type Query {
fetch__RefetchableInterfaceFoo(id: ID!): RefetchableInterfaceFoo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would we expect people to write this in their schemas? @rbalicki2

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this works in OSS, then yeah, I'd expect people to either write it or (more likely) auto-generate it in their schemas? But I haven't checked that it "just works"

This makes me think we should probably include another parameter on the @fetchable directive that specifies what field... /shrug

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mostly just copied this schema from the unit tests in the commit that added this. We don't use this at all so to be honest I was mostly just making this section up a bit

}

// fragment

fragment RefetchableFragmentFoo on RefetchableInterfaceFoo
@refetchable(queryName: "RefetchableFragmentFooQuery") {
id
}
```

### Persisted query improvements

For users who are using URL-based persisted queries, you can now specify custom headers to send with the request to persist your queries. For example, this can be used to send auth headers to your query persistence URL endpoint.
ernieturner marked this conversation as resolved.
Show resolved Hide resolved

```js
persistConfig: {
url: 'example.com/persist',
headers: {
Authorization: 'bearer TOKEN'
}
}
```

And for users who are using file-based persisted queries a new feature flag, `compact_query_text` was added to remove all whitespace from persisted query output text. This can cause a substantial reduction in your persisted query text file (we've seen an example of reducing a single query text from 84kb to 31kb). This new feature flag can be enabled within your Relay config file.
ernieturner marked this conversation as resolved.
Show resolved Hide resolved

```js
persistConfig: {
file: 'path/to/file.json',
algorithm: 'SHA256'
},
featureFlags: {
compact_query_text: true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this an option and not just always on? (not related to the blog post, just wondering if you know)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be hard to switch over hundreds of query texts in a single commit? Just guessing

}
```

### Typesafe updates now support missing field handlers

One of the few breaking changes in this release, the [missing field handlers](https://relay.dev/docs/guided-tour/reusing-cached-data/filling-in-missing-data) function signature has been changed. The `record` argument to the handler used to recieve a `Record` type (which is an untyped grabbag containing whatever the store has) but we now pass a `ReadOnlyRecordProxy` which is much more typesafe and provides a better interface. Furthermore, we also replaced the `field` argument of type `NormalizationLinkedField` with `CommonLinkedField`, which both a `ReaderLinkedField` and `NormalizationLinkedField` "implement".
ernieturner marked this conversation as resolved.
Show resolved Hide resolved
ernieturner marked this conversation as resolved.
Show resolved Hide resolved

### Flow type improvements

Flow users will now get types inferred from `graphql` literals with more Relay APIs. No longer do Flow users need to explicitly type the return value of the `usePreloadedQuery`, `useQueryLoader`, `useRefetchableFragment`, `usePaginationFragment`, and `useBlockingPaginationFragment` API methods.
ernieturner marked this conversation as resolved.
Show resolved Hide resolved

## What's new in the Relay VSCode Extension v2?

The latest version of the [Relay VSCode Extension](https://marketplace.visualstudio.com/items?itemName=meta.relay) adds support for workspaces which contain multiple Relay configs in various subdirectories. Instead of only supporting a single Relay project at a time, you can now configure the `relay.projects` configuration to point to multiple Relay projects in a single workspace. This allows you to seamlessly switch between multiple projects and still be able to get field autocompletion, go to definition on fragments spreads, etc. And with the `relay.autoStartCompiler` you can have the Relay compiler run in watch mode on all projects in your workspace in parallel.

If you were previously using the `relay.rootDirectory` or `relay.pathToConfig` configuration options, those have been deprecated and you'll want to migrate those to the `relay.projects` configuration instead.

Before:

```json
// VSCode Settings
"relay.rootDirectory": "path/to/app",
"relay.pathToConfig": "config/relay.config.js"
```

After:

```json
// VSCode Settings
"relay.projects": [{
"name": "my-app",
"rootDirectory": "path/to/app",
"pathToConfig": "config/relay.config.js"
}]
```

Note that your existing settings will continue to work in this release, but you should migrate to the new settings as they'll be removed in a future release.

## What's next?

TODO..
ernieturner marked this conversation as resolved.
Show resolved Hide resolved

Happy Querying!