All Projects → zth → relay-store-types-generator

zth / relay-store-types-generator

Licence: MIT license
Generate types for the Relay store from your GraphQL schema.

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to relay-store-types-generator

pyladies-courseware
Homework/task submit and review web app · based on React and Python aiohttp
Stars: ✭ 14 (-17.65%)
Mutual labels:  relay, relay-modern
react-relay-appsync
AppSync for Relay
Stars: ✭ 19 (+11.76%)
Mutual labels:  relay, relay-modern
relay-compiler-plus
Custom relay compiler which supports persisted queries
Stars: ✭ 68 (+300%)
Mutual labels:  relay, relay-modern
isomorphic-relay-app
Example isomorphic React-Relay-(Modern / Classic)-Router app and helper lib that connects to Artsy's GraphQL service
Stars: ✭ 13 (-23.53%)
Mutual labels:  relay, relay-modern
React Firebase Starter
Boilerplate (seed) project for creating web apps with React.js, GraphQL.js and Relay
Stars: ✭ 4,366 (+25582.35%)
Mutual labels:  relay, relay-modern
graphql-ufc-api
GraphQL server for UFC's public API
Stars: ✭ 26 (+52.94%)
Mutual labels:  relay-modern
graphql-ts-client
Typescript DSL for GraphQL.
Stars: ✭ 124 (+629.41%)
Mutual labels:  relay
ssocks
sSocks fork for windows support; original: https://sourceforge.net/projects/ssocks/
Stars: ✭ 141 (+729.41%)
Mutual labels:  relay
next-dashboard
A complete React/Redux/Relay/Next.js dashboard example
Stars: ✭ 65 (+282.35%)
Mutual labels:  relay-modern
conduit
Conduit - Open Source 0x Relayer API implemented in NodeJS
Stars: ✭ 18 (+5.88%)
Mutual labels:  relay
VineRelayStore
🔥 VineRelay is Content Management Boilerplate which enables you to quickly scaffold a shop with the basic features using React, Relay and GraphQL.
Stars: ✭ 16 (-5.88%)
Mutual labels:  relay
qweechat
Qt remote GUI for WeeChat.
Stars: ✭ 56 (+229.41%)
Mutual labels:  relay
Three-Factor-Security-Door
What do you get when you mix a Raspberry Pi, a MySQL database, an RFID reader, an LCD touchscreen, a relay switch, an electronic door strike and a Twilio SMS account?
Stars: ✭ 49 (+188.24%)
Mutual labels:  relay
ReSift
A state management library for data fetches in React
Stars: ✭ 39 (+129.41%)
Mutual labels:  relay
apollo-federation-relay
A demo of Apollo Federation and global Node resolution
Stars: ✭ 44 (+158.82%)
Mutual labels:  relay
server
Serve one or more react apps! - Custom routes, HotReloading, Authenticated Apps/routes, Relay, Webpack..
Stars: ✭ 20 (+17.65%)
Mutual labels:  relay
srtrelay
SRT relay server for distributing media streams to multiple clients.
Stars: ✭ 81 (+376.47%)
Mutual labels:  relay
vscode-graphiql-explorer
Use GraphiQL + GraphiQL Explorer to build your GraphQL operations, right from inside of VSCode.
Stars: ✭ 35 (+105.88%)
Mutual labels:  relay
zola-api
Zola’s API.
Stars: ✭ 23 (+35.29%)
Mutual labels:  relay
NoPMsBot
https://telegram.dog/ShriMADhaBot
Stars: ✭ 127 (+647.06%)
Mutual labels:  relay

relay-store-types-generator

THIS IS HIGHLY EXPERIMENTAL AND PROBABLY NOT READY FOR ACTUAL USE YET

Generate types for the Relay store from your GraphQL schema. More type safety for your Relay store with zero runtime cost and a very small set of changes needed for your code.

Currently only generates Flow types, TypeScript mode coming soon.

The idea

When you write queries, mutations and fragments, Relay generates types for you that you can use for type-safety. Relay also lets you manually interact with its store through updaters after mutations, commitLocalUpdate and so on. But, when interacting with the store you're left with a generalized API with general types.

This package generates types for Relay's store tailored to your specific GraphQL schema, so that your interaction with the store can be fully type safe, just like interacting with the data Relay gives you.

Example

Imagine the following schema:

schema {
  query: Query
}

type Query {
  bestFriend: User
}

type User {
  firstName: String!
  age: Int!
}

Instead of doing this:

commitLocalUpdate(environment, store => {
  const root = store.getRoot(); // Gets you a RecordProxy
  const bestFriend = root.getLinkedRecord('bestFriend'); // Also gets you a RecordProxy, but nullable
  if (bestFriend) {
    const age = bestFriend.getValue('agee'); // This is allowed even though it's misspelled
    bestFriend.setValue(123, 'firstName'); // This is allowed as well, even though firstName is supposed to be a string
  }
});

...cast your store to Store$RecordSourceSelectorProxy like this:

import type { Store$RecordSourceSelectorProxy } from '../path/to/generated/relay-store-types.js.flow';

// Cast to typed version of the store from the generated type file
commitLocalUpdate(environment, (store: Store$RecordSourceSelectorProxy) => {
  const root = store.getRoot(); // Gets you a RecordProxy$Query with a shape corresponding to your root query
  const bestFriend = root.getLinkedRecord('bestFriend'); // Returns a ?RecordProxy$User since this is a user
  if (bestFriend) {
    const age = bestFriend.getValue('agee'); // This will not be allowed since there's no getValue method for "agee" on RecordProxy$User
    bestFriend.setValue(123, 'firstName'); // This won't be allowed either, because the method that accepts "firstName" as key expects the value to be ?string
  }
});

Usage

yarn add --dev relay-store-types-generator

./node_moduels/.bin/relay-store-types-generator --flow --schema ./path/to/schema.graphql --out-dir ./path/to/output/dir --custom-scalars-path ./path/to/file/exporting/custom/scalars

# You can add it to package.json
...
"scripts": {
    "generate:relay-store-types": "relay-store-types-generator --flow --schema ./path/to/schema.graphql --out-dir ./path/to/output/dir --custom-scalars-path ./path/to/file/exporting/custom/scalars"
...

# ...and then run like
yarn generate:relay-store-types
relay-store-types-generator

Options:
  --schema [path]               Path to schema.graphql
  --custom-scalars-path [path]  Path to file exporting custom scalars.
  --out-dir [path]              Path to directory to output type file.
  --flow                        Output Flow types.
  --typescript                  Output TypeScript types.
  -h, --help                    output usage information

Preferably set this up to run after whatever you use to persist the introspection of your GraphQL schema. That way you always have a fresh version.

FAQ

What if I start using this and find it's not for me, do I need to do a lot of invasive changes to my code to get this to work?

Not at all! That's a primary feature. Wherever you interact with the Relay store and want to do so in a type-safe way, just cast the store to the generated store type, like (store: Store$RecordSourceSelectorProxy) => .... Want to go back? Remove the cast and work with the store as usual!

What about my custom scalars, have you forgot about them?

I'm hurt you even ask! You can pass a path to a file exporting your custom scalars. Example:

relay-store-types-generator --flow --schema ./schema.graphql --out-dir ./types --custom-scalars-path ./src/customScalars.js

# customScalars.js
module.exports = {
  "Datetime": "string",
  "Cursor": "string",
  "BigInt": "number",
  "Flag": "boolean"
};

There's lots of types generated... Can I reduce the size some way?

Currently no, but I have a few ideas for how to reduce the size of the generated types. However, ultimately, generating types for a large GraphQL schema will always result in lots of types, since the number of combinations of keys/types/methods and so on the types need to cover are large.

TODO

  • TypeScript mode
  • Type args for every field
  • Type filters for connections
  • Optimize amount of generated code
  • Handle subscriptions (?)
  • Parse extensions in schema
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].