facebook / facebook/relay

Cannot generate null value for type without mocking parent

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

Description

Assuming the following schema:

```
type Query {
fields: [Field!]!
}

interface Field {
id: ID!
}

type FooField implements Field {
sub: SubFoo
}

type SubFoo {
name: String!
}
```

and I have a query as follows:

```
query ExampleQuery {
fields {
...myFooFragment
}
}

fragment myFooFragment on FooField {
sub {
name
}
}
```

It's valid for this query to return `null` for the `sub` key.

When I mock that in a test as follows:

```
environment.mock.queueOperationResolver((operation) =>
MockPayloadGenerator.generate(operation, {
SubFoo: () => null,
})
);
```

I would expect this to set the `sub` key to null in the response. What I'm seeing, though, is a generated value:

```
{ sub: { name: '' } }
```

If I instead do this, it works...

```
environment.mock.queueOperationResolver((operation) =>
MockPayloadGenerator.generate(operation, {
FooField: () => ({ sub: null }),
})
);
```

I get the expected value:

```
{ sub: null }

```

But since not every key will have a nested key to null out, this isn't a general solution.

I think this line is in error; instead of using a nullish coalescing operator, it should be explicitly testing for undefined so that `null` values are accepted from the generator functions. https://github.com/facebook/relay/blame/98162d36249cbdb162155427c0d721192de78d81/packages/relay-test-utils/RelayMockPayloadGenerator.js#L712

Contributor guide

Open the contributing guide

Research direction

Start in packages/relay-test-utils/RelayMockPayloadGenerator.js at the referenced line around 712 and trace how generated type values are selected. Run the relevant relay-test-utils tests, then add coverage for a generator returning null and confirm the generated SubFoo field is null rather than populated.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
testing
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.