facebook / facebook/relay

Cannot setup network layer to handle subscriptions using example from docs. Relay 12

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

Description

When following the example on how to set up the network layer to handle subscriptions from the [docs site](https://relay.dev/docs/guided-tour/updating-data/graphql-subscriptions/#configuring-the-network-layer) I am receiving multiple errors trying to set up the network layer in a `create-react-app` app initialized in typescript.

The first Error I get is.
```
TS2345: Argument of type 'Observable>' is not assignable to parameter of type 'ObservableFromValue>'.
Type 'Observable>' is not assignable to type 'Subscribable>'.
The types returned by 'subscribe(...)' are incompatible between these types.
Property 'closed' is missing in type '{ unsubscribe: () => void; }' but required in type 'Subscription'.
42 | });
43 |
> 44 | return Observable.from(subscribeObservable);
| ^^^^^^^^^^^^^^^^^^^
45 | };
```
I was able to solve this issue by changing
```ts
const subscribe = (request: { text: any; name: any }, variables: any) => {
const subscribeObservable = subscriptionClient.request({
query: request.text,
operationName: request.name,
variables,
});

return Observable.from(subscribeObservable);
};
```
to
```ts
const subscribe = (request: { text: any; name: any }, variables: any) => {
const subscribeObservable = subscriptionClient
.request({
query: request.text,
operationName: request.name,
variables,
})
.subscribe({
next: (data: any) => {
console.log(data);
},
error: (error: any) => {
console.log(error);
},
complete: () => {
console.log("complete");
},
});

return Observable.from(subscribeObservable);
};
```
And now the issue I am getting is:
```
TS2345: Argument of type '(request: { text: any; name: any;}, variables: any) => Observable<{ unsubscribe: () => void; }>' is not assignable to parameter of type 'SubscribeFunction'.
Type 'RelayObservable<{ unsubscribe: () => void; }>' is not assignable to type 'RelayObservable | Disposable'.
Type 'RelayObservable<{ unsubscribe: () => void; }>' is not assignable to type 'RelayObservable'.
Type '{ unsubscribe: () => void; }' is not assignable to type 'GraphQLResponse'.
Type '{ unsubscribe: () => void; }' is missing the following properties from type 'readonly GraphQLSingularResponse[]': length, concat, join, slice, and 18 more.
58 |
59 | const environment = new Environment({
> 60 | network: Network.create(fetchQuery, subscribe),
| ^^^^^^^^^
61 | store: new Store(new RecordSource()),
62 | });
```

Below are the contents of the file where I am trying to set up the network layer.

```ts
import {
Environment,
Network,
RecordSource,
RequestParameters,
Store,
Variables,
Observable,
} from "relay-runtime";

import { SubscriptionClient } from "subscriptions-transport-ws";

async function fetchQuery(operation: RequestParameters, variables: Variables) {
const response = await fetch(
process.env.REACT_APP_GRAPHQL_URL || "http://localhost:8080/api/query",
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
query: operation.text,
variables,
}),
}
);
return await response.json();
}

const websocketURL =
process.env.REACT_APP_GRAPHQL_URL || "ws://localhost:8080/api/query";

const subscriptionClient = new SubscriptionClient(websocketURL, {
reconnect: true,
});

const subscribe = (request: { text: any; name: any }, variables: any) => {
const subscribeObservable = subscriptionClient
.request({
query: request.text,
operationName: request.name,
variables,
})
.subscribe({
next: (data: any) => {
console.log(data);
},
error: (error: any) => {
console.log(error);
},
complete: () => {
console.log("complete");
},
});

return Observable.from(subscribeObservable);
};

const environment = new Environment({
network: Network.create(fetchQuery, subscribe),
store: new Store(new RecordSource()),
});

export default environment;
```

Contributor guide

Open the contributing guide

Research direction

Start with the network-layer subscription example at the linked Relay documentation page and reproduce it in the TypeScript create-react-app setup shown in the issue. Trace the Observable and Network.create types behind the reported errors; done means the documented example type-checks and configures subscriptions without the shown TypeScript failures.

Written by the indexing model from the issue text.

Assessment

Tech stack
graphql, react, typescript
Domain
api, documentation
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.