Skip to content

Commit

Permalink
Suspense integration for React bindings (#1044)
Browse files Browse the repository at this point in the history
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Apr 9, 2023
1 parent 89ad90b commit 987a6f2
Show file tree
Hide file tree
Showing 68 changed files with 4,255 additions and 600 deletions.
2 changes: 1 addition & 1 deletion .changeset/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
"access": "public",
"baseBranch": "main",
"updateInternalDependencies": "patch",
"ignore": ["e2e-api", "e2e-next", "e2e-kit", "e2e-svelte", "site"]
"ignore": ["e2e-api", "e2e-next", "e2e-kit", "e2e-svelte", "site", "e2e-react-vite"]
}
5 changes: 5 additions & 0 deletions .changeset/popular-rocks-join.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'houdini-react': minor
---

Add suspense integration
2 changes: 1 addition & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,5 @@ module.exports = {
sourceType: 'module',
ecmaVersion: 2020,
},
plugins: ['unused-imports'],
plugins: ['unused-imports', 'react', 'react-hooks'],
}
24 changes: 24 additions & 0 deletions e2e/react-vite/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
36 changes: 36 additions & 0 deletions e2e/react-vite/houdini.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/// <references types="houdini-svelte">
/// <references types="houdini-plugin-svelte-global-stores">

/** @type {import('houdini').ConfigFile} */
const config = {
schemaPath: '../_api/*.graphql',
defaultPartial: true,
scalars: {
DateTime: {
type: 'Date',
// turn the api's response into that type
unmarshal(val) {
return new Date(val)
},
// turn the value into something the API can use
marshal(val) {
return val.getTime()
},
},
File: {
type: 'File',
},
},

types: {
RentedBook: {
keys: ['userId', 'bookId'],
},
},

plugins: {
'houdini-react': {},
},
}

export default config
13 changes: 13 additions & 0 deletions e2e/react-vite/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + React + TS</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
33 changes: 33 additions & 0 deletions e2e/react-vite/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "e2e-react-vite",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"build:": "cd ../../ && ((run build && cd -) || (cd - && exit 1))",
"build:dev": "pnpm build: && pnpm dev",
"build:test": "pnpm build: && pnpm test",
"build:build": "pnpm build: && pnpm build",
"web": "vite ",
"api": "cross-env TZ=utc e2e-api",
"dev": "concurrently \"pnpm run web\" \"pnpm run api\" -n \"web,api\" -c \"green,magenta\"",
"build": "tsc && vite build",
"preview": "vite preview"
},
"dependencies": {
"houdini": "workspace:^",
"houdini-react": "workspace:^",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@types/react": "^18.0.27",
"@types/react-dom": "^18.0.10",
"@vitejs/plugin-react": "^3.1.0",
"concurrently": "7.1.0",
"cross-env": "^7.0.3",
"e2e-api": "workspace:^",
"typescript": "^4.9.3",
"vite": "^4.1.0"
}
}
1 change: 1 addition & 0 deletions e2e/react-vite/public/vite.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 42 additions & 0 deletions e2e/react-vite/src/App.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#root {
max-width: 1280px;
margin: 0 auto;
padding: 2rem;
text-align: center;
}

.logo {
height: 6em;
padding: 1.5em;
will-change: filter;
transition: filter 300ms;
}
.logo:hover {
filter: drop-shadow(0 0 2em #646cffaa);
}
.logo.react:hover {
filter: drop-shadow(0 0 2em #61dafbaa);
}

@keyframes logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}

@media (prefers-reduced-motion: no-preference) {
a:nth-of-type(2) .logo {
animation: logo-spin infinite 20s linear;
}
}

.card {
padding: 2em;
}

.read-the-docs {
color: #888;
}
69 changes: 69 additions & 0 deletions e2e/react-vite/src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { useQuery, useFragment, graphql, HoudiniProvider, type UserInfo } from '$houdini'
import * as React from 'react'

import client from './client'

export default function App() {
return (
<HoudiniProvider client={client}>
<React.Suspense fallback="loading...">
<Query />
</React.Suspense>
</HoudiniProvider>
)
}

function Query() {
const [variables, setVariables] = React.useState({ id: '1' })
const [pending, start] = React.useTransition()

const data = useQuery(
graphql(`
query MyQuery($id: ID!) {
user(id: $id, snapshot: "react-vite-e2e") {
id
...UserInfo
}
}
`),
variables
)

return (
<>
<div>
<button
onClick={() =>
start(() => {
setVariables({ id: '2' })
})
}
>
fetch user 2 (no suspense)
</button>
<button onClick={() => setVariables({ id: '3' })}>fetch user 3 (suspend)</button>
{pending && 'pending'}
</div>

<Fragment user={data.user} />
</>
)
}

function Fragment({ user }: { user: UserInfo }) {
const data = useFragment(
user,
graphql(`
fragment UserInfo on User {
id
name
}
`)
)

return (
<div>
{data.id}: {data.name}
</div>
)
}
1 change: 1 addition & 0 deletions e2e/react-vite/src/assets/react.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions e2e/react-vite/src/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { HoudiniClient } from '$houdini'

// Export the Houdini client
export default new HoudiniClient({
url: 'http://localhost:4000/graphql',
plugins: [
() => ({
network(ctx, { next }) {
setTimeout(() => next(ctx), 500)
},
}),
],
})
69 changes: 69 additions & 0 deletions e2e/react-vite/src/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
:root {
font-family: Inter, system-ui, Avenir, Helvetica, Arial, sans-serif;
line-height: 1.5;
font-weight: 400;

color-scheme: light dark;
color: rgba(255, 255, 255, 0.87);
background-color: #242424;

font-synthesis: none;
text-rendering: optimizeLegibility;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
-webkit-text-size-adjust: 100%;
}

a {
font-weight: 500;
color: #646cff;
text-decoration: inherit;
}
a:hover {
color: #535bf2;
}

body {
margin: 0;
display: flex;
place-items: center;
min-width: 320px;
min-height: 100vh;
}

h1 {
font-size: 3.2em;
line-height: 1.1;
}

button {
border-radius: 8px;
border: 1px solid transparent;
padding: 0.6em 1.2em;
font-size: 1em;
font-weight: 500;
font-family: inherit;
background-color: #1a1a1a;
cursor: pointer;
transition: border-color 0.25s;
}
button:hover {
border-color: #646cff;
}
button:focus,
button:focus-visible {
outline: 4px auto -webkit-focus-ring-color;
}

@media (prefers-color-scheme: light) {
:root {
color: #213547;
background-color: #ffffff;
}
a:hover {
color: #747bff;
}
button {
background-color: #f9f9f9;
}
}
10 changes: 10 additions & 0 deletions e2e/react-vite/src/main.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from 'react'
import ReactDOM from 'react-dom/client'

import App from './App'

ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
<React.StrictMode>
<App />
</React.StrictMode>
)
1 change: 1 addition & 0 deletions e2e/react-vite/src/vite-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference types="vite/client" />
25 changes: 25 additions & 0 deletions e2e/react-vite/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"compilerOptions": {
"target": "ESNext",
"useDefineForClassFields": true,
"lib": ["DOM", "DOM.Iterable", "ESNext"],
"allowJs": false,
"skipLibCheck": true,
"esModuleInterop": false,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "ESNext",
"moduleResolution": "Node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx",
"paths": {
"$houdini": ["./$houdini"],
"$houdini/*": ["./$houdini/*"]
}
},
"include": ["src"],
"references": [{ "path": "./tsconfig.node.json" }]
}
9 changes: 9 additions & 0 deletions e2e/react-vite/tsconfig.node.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"compilerOptions": {
"composite": true,
"module": "ESNext",
"moduleResolution": "Node",
"allowSyntheticDefaultImports": true
},
"include": ["vite.config.ts"]
}
16 changes: 16 additions & 0 deletions e2e/react-vite/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import react from '@vitejs/plugin-react'
import { path } from 'houdini'
import houdini from 'houdini/vite'
import { defineConfig } from 'vite'

// https://vitejs.dev/config/
export default defineConfig({
plugins: [houdini(), react()],
// TODO: the vite plugin should do this
resolve: {
alias: {
$houdini: path.resolve('.', '/$houdini'),
'$houdini/*': path.resolve('.', '/$houdini', '*'),
},
},
})
Loading

0 comments on commit 987a6f2

Please sign in to comment.