I would like to write a new web application in React that uses Relay to communicate with a GraphQL server. Recently JSX support was introduced for TypeScript. But there is no typings declaration file (.d.ts) for Relay on DefinitelyTyped. How can I write my application in TypeScript without having to write a typings file for Relay first?
Asked
Active
Viewed 2,082 times
1 Answers
3
Typescript doesn't require you have typings for all the libraries you use, it's just highly recommended. To use Relay without typings:
import * as React from "react"
let Relay = require("react-relay")
interface HelloProps extends React.Props<HelloComponent> {
viewer: {
message: string
}
}
class HelloComponent extends React.Component<HelloProps,void> {
render() {
return <div>Hello { this.props.viewer.message }</div>
}
}
let Hello = Relay.createContainer(HelloComponent, {
fragments: {
viewer: () => Relay.QL`fragment on Viewer { message }`
}
})
Then use Hello
as a regular Relay component. The idea is that require
is a function that takes a string and returns any
so you can then use Relay
variable as you would in vanilla Javascript.
If you're not using Node.JS typings, you might need to add a typing for require
. Somewhere in your d.ts files, add:
interface NodeRequireFunction {
(id: string): any;
}
declare var require: NodeRequireFunction;

Gunchars
- 9,555
- 3
- 28
- 27
-
Thanks for pointing out I need to use the Node.js typings. But now I get the following error: "error TS2503: Cannot find namespace 'Relay'". – Korneel Dec 08 '15 at 09:11
-
`interface HelloProps extends Relay.Props
` should be `interface HelloProps extends React.Props – Korneel Dec 08 '15 at 12:51` -
When I try to define a route with `class MyRoute extends Relay.Route {/* ... */}` TSC outputs `error TS2507: Type 'any' is not a constructor function type.`. How would you solve that error? – Korneel Dec 08 '15 at 15:49
-
@Korneel, this is where you'll either need to add **some** typings or avoid using ES6 classes. Custom routes are not required to be class instances, so another alternative is just using plain objects, as seen in the first example here: https://facebook.github.io/relay/docs/guides-routes.html#routes-and-queries – Gunchars Dec 08 '15 at 16:53
-
Thank you. It works when the route is defined as an object. Still, what would the .d.ts file with typings for Relay.Route look like? I have tried to create such a file but didn't succeed. – Korneel Dec 08 '15 at 20:13
-
Here are the typings that I'm using - https://gist.github.com/guncha/c5425166a18123995898. They're pretty ad-hoc so expect things to break. You can see that the Route is just a class that takes some variables. Relay is changing pretty rapidly, so it might take a while until we have stable typings. Until then it's best to figure out how to write them yourself. – Gunchars Dec 08 '15 at 20:40
-
1Thanks a lot! Really :) I had to add `var DefaultNetworkLayer;` to the typings you provided in order to be able to set de default network layer. I will definitely have to do some research on writing custom typings files. – Korneel Dec 08 '15 at 21:54
-
How do you transform Relay.QL`fragment on Viewer { message }` to ES5 so the browser can understand it? The Babel Relay plugin does that, but because the TypeScript compiler already transpiles it into something different, the Babel Relay plugin doesn't transform the Relay queries so the browser throws an error: Uncaught Invariant Violation: RelayQL: Unexpected invocation at runtime. Either the Babel transform was not set up, or it failed to identify this call site. Make sure it is being used verbatim as `Relay.QL`. – Korneel Jan 15 '16 at 15:58
-
http://stackoverflow.com/questions/34815578/how-to-transpile-a-relay-query-from-typescript-to-es5 – Korneel Jan 15 '16 at 16:28