Skip to content

Instantly share code, notes, and snippets.

@robrichard
robrichard / example-usage.ts
Last active May 19, 2023 14:25
React Server Component Relay Example
import { renderToPipeableStream } from 'react-server-dom-webpack/server.node';
import { runWithAsyncLocalStorageRelay } from './runWithAsyncLocalStorageRelay';
function handler(req, res, next) {
const relayEnvironment = createRelayEnvironment(req, res);
const { Component, props } = getComponent(req);
runWithAsyncLocalStorageRelay(relayEnvironment, () => {
const { pipe } = renderToPipeableStream(
<Component {...props} />,
@robrichard
robrichard / README.md
Last active June 2, 2023 14:47
defer-stream proposal 2023-04-14

Summary of the latest "no-overlapping-branches" proposal by @benjie & @robrichard

Features

  • No overlapping branching.
  • No duplicate delivery of any fields
  • Reduces the risk of response amplification
  • No complex WeakMap/similar caching shenanigans.
  • No query rewriting.
  • No look-ahead.
@robrichard
robrichard / query.graphql
Created July 17, 2020 16:12
Example of case where path is not enough to determine @stream
query HeroFriendsQuery {
human(id: "1000") {
friends @stream(initialCount: 2, label: "HumanFriends") {
id
...NameFragment @defer(label: "DeferName")
}
}
}
fragment NameFragment on Character {
@robrichard
robrichard / defer-array.md
Last active February 25, 2020 07:59
Relay Compatible `@defer` & `@stream`

Using @defer under an array field will return multiple patches. Patches are unique combinations of label and path

{
  items {
    id
    ...frag @defer(label: "my-label")
  }
}
@robrichard
robrichard / persistedQuery.js
Last active January 8, 2019 00:09
express-graphql persisted query
const express = require('express');
const bodyParser = require('body-parser');
const graphqlHTTP = require('express-graphql');
const app = express();
app.use(bodyParser.json());
async function getPersistedQueryMiddleware(req, res, next) {
if (!req.body.id) {
next();
@robrichard
robrichard / react-relay.js
Created November 1, 2017 16:40
Mock for relay modern
import React from 'react';
import PropTypes from 'prop-types';
const relayMock = jest.genMockFromModule('react-relay');
const relayChildContextTypes = {
relay: PropTypes.object
};
const relayEnvironment = {
lookup: jest.fn()
class MyForm extends React.Component {
constructor(props) {
super(props);
this.state = {
input: {
email: "",
name: ""
},
blurred: {
email: false,
class MyForm extends React.Component {
constructor(props) {
super(props);
this.state = {
input: {
email: "",
name: ""
}
};
}
class MyForm extends React.Component {
constructor(props) {
super(props);
this.state = {
input: {
email: "",
name: ""
}
};
}
class MyForm extends React.Component {
render() {
return (
<form onSubmit={() => /* save your form here */}>
<div>
<input name="email" placeholder="email"/>
</div>
<div>
<input name="name" placeholder="name"/>
</div>