• This repository has been archived on 27/Sep/2023
  • Stars
    star
    241
  • Rank 167,643 (Top 4 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created almost 7 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

⛔️ Obsolete - A language plugin for Relay that adds TypeScript support, including emitting type definitions.

Obsolete

This repository is obsolete as relay@13 now supports TypeScript directly.

relay-compiler-language-typescript

Build Status

A language plugin for Relay that adds TypeScript support, including emitting type definitions.

Installation

Add the package to your dev dependencies:

yarn add graphql relay-compiler --dev
yarn add typescript relay-compiler-language-typescript --dev

Note: Starting with version 15.0.0 relay-compiler-language-typescript requires a minimum TypeScript version of 4.5.0 being installed in your project.

Configuration

relay-compiler

Then configure your relay-compiler script to use it, like so:

{
  "scripts": {
    "relay":
      "relay-compiler --src ./src --schema data/schema.graphql --language typescript --artifactDirectory ./src/__generated__"
  }
}

This is going to store all artifacts in a single directory, which you also need to instruct babel-plugin-relay to use in your .babelrc:

{
  "plugins": [["relay", { "artifactDirectory": "./src/__generated__" }]]
}

TypeScript

Also be sure to configure the TypeScript compiler to transpile to ES2015 modules (or higher) and leave transpilation to CommonJS modules (if required) up to Babel with the following tsconfig.json settings:

{
  "compilerOptions": {
    "module": "ES2015", // ES2015 or higher
    "target": "ES2020"  // best use the highest target setting compatible with your Babel setup
  }
}

The reason for this is that tsc would otherwise generate code where the imported graphql function is being namespaced (react_relay_1 in this example):

react_relay_1.createFragmentContainer(
  MyComponent,
  react_relay_1.graphql`
  ...
`
);

… which makes it impossible for babel-plugin-relay to find the locations where the graphql function is being used.

The generated code uses ES2015 module syntax if module is set to ES2015 or higher in your tsconfig.json. Note that the eagerESModules option from relay-compiler has no effect on the generated code if module is ES2015 or higher.

Custom Headers

If you need to add a custom header to generated files, perhaps for a custom linter or to get boilerplate license code in, that can be passed in also in compilerOptions as banner:

{
  "compilerOptions": {
    "banner": "/* © 2021 Example.org - @generated code */"
  }
}

Problems

React Hot Loader

React Hot Loader is known to not always work well with generated code such as our typing artifacts, which will lead to loading modules with TypeScript types into the browser and break. As a maintainer of RHL pointed out in a similar issue:

The problem - hot reloading is not "complete"

So until RHL will be made “complete” this project can’t gurantee to always work well with it, nor is it our control to do anything about that.

Also see

License

This package is available under the MIT license. See the included LICENSE file for details.

More Repositories

1

react-router-relay

[Deprecated] Relay Classic integration for React Router
JavaScript
559
star
2

relay-hooks

Use Relay as React hooks
TypeScript
541
star
3

react-relay-network-modern

Relay Modern Network Layer with middlewares — cache, auth, retry, batch, logger, SSR
JavaScript
326
star
4

react-relay-network-layer

ReactRelayNetworkLayer with middlewares and query batching for Relay Classic.
JavaScript
276
star
5

found-relay

Relay integration for Found
JavaScript
276
star
6

relay-local-schema

Use Relay without a GraphQL server
JavaScript
253
star
7

relay-subscriptions

[Deprecated] Subscription support for Relay Classic
JavaScript
176
star
8

react-relay-network-modern-ssr

SSR middleware for react-relay-network-modern
JavaScript
67
star
9

Relay.swift

Relay for GraphQL, ported to Swift and SwiftUI
Swift
65
star
10

relay-compiler-webpack-plugin

Automatically run the Relay Compiler from Webpack
JavaScript
65
star
11

relay-query-lookup-renderer

Same as Relay Modern's QueryRenderer, but will check the store for data before fetching
JavaScript
52
star
12

fetch-multipart-graphql

Cross browser function to fetch and parse streaming multipart graphql responses
JavaScript
51
star
13

vscode-apollo-relay

Simple configuration of vscode-apollo for Relay projects.
TypeScript
49
star
14

react-relay-mutation

Higher-level React mutation API for Relay
TypeScript
40
star
15

relay-commit-mutation-promise

Promise wrapper for Relay Modern's commit mutation
JavaScript
27
star
16

typescript-relayjs-examples

RelayJS typescript examples
TypeScript
24
star
17

relay-connection-handler-plus

Relay connection handler with additional functionality
TypeScript
21
star
18

relay-context-provider

JavaScript
16
star
19

relay-graphql-js

Relay tooling based on graphql-js
TypeScript
15
star
20

tslint-plugin-relay

tslint plugin for relay-compiler-language-typescript tool
TypeScript
6
star