facebook / facebook/relay

Mutations do not generate covariant/read only flow types

Open
#2,583 6 comments 2 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
19k
Forks
1.9k
PR merge metrics
No merged PRs in 30d

Description

Read queries generate flow types which are covariant/read-only. This makes sense: bad stuff starts to happen when you mutate objects that exist in the relay store!

However, mutations generate input types which seem somewhat inconsistent. Concretely, they use `$ReadOnlyArray`, but object properties are not marked as covariant.

This is somewhat inconvenient, because there are a number of cases where some or all of a mutation input may be very similar or even identical in shape to the result of a query. It would be nice to be able to define fewer types, but additionally it is not uncommon that one may ultimately want to pass the result of a GraphQL query through to a mutation. This typing prevents that.

Additionally, the typing here causes issues when providing objects with non-null properties to a mutation which accepts those properties as null. For instance, given a mutation which takes an input object typed as `{ foo: ?string }`, providing an object typed as `{ foo: string }` will cause a type error. But if the input object is typed `{+foo: ?string}`, this is no longer an issue.

My guess is that relay does not actually mutate the input objects it receives. If that's true, would it be possible to update the typing here to use covariant properties and encode that guarantee of non-mutation in the typing?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.