Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add vike-react-apollo #125

Closed
wants to merge 62 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
0328a7b
add vike-react-apollo
nitedani Jun 30, 2024
36572c1
minor
nitedani Jun 30, 2024
c75ecec
only send diffs
nitedani Jun 30, 2024
a749f25
@apollo/client-react-streaming
nitedani Jul 1, 2024
4002ccb
format package.json
nitedani Jul 1, 2024
de6449e
fix test
nitedani Jul 1, 2024
69f4068
minor
nitedani Jul 1, 2024
9d4dba6
add readme
nitedani Jul 1, 2024
417cdad
minor
nitedani Jul 1, 2024
42818fd
radme
nitedani Jul 1, 2024
3454965
readme
nitedani Jul 1, 2024
5f62138
fix types
nitedani Jul 1, 2024
5c868c4
minor
nitedani Jul 1, 2024
779926c
update
nitedani Jul 3, 2024
3c53de1
update react-streaming
nitedani Jul 3, 2024
c644f3c
minor
nitedani Jul 3, 2024
6cd7a42
update
nitedani Jul 4, 2024
8f2d353
update
nitedani Jul 4, 2024
8933700
update react-streaming
brillout Jul 4, 2024
eddd759
update readme, react-streaming
nitedani Jul 6, 2024
fcdddc6
readme
nitedani Jul 6, 2024
53edab7
readme
nitedani Jul 6, 2024
5144b34
readme
nitedani Jul 7, 2024
98ba36b
package json
nitedani Jul 7, 2024
2f90a9b
fix lockfile
nitedani Jul 7, 2024
2bd8824
refactor
nitedani Jul 7, 2024
5f03806
format
nitedani Jul 7, 2024
4c37776
readme
nitedani Jul 7, 2024
1118198
readme
nitedani Jul 7, 2024
031f10a
package.json
nitedani Jul 7, 2024
6a79692
readme
nitedani Jul 7, 2024
85e1d94
improve loading animation
brillout Jul 7, 2024
e137954
minor
brillout Jul 7, 2024
7b2dfec
minor
brillout Jul 7, 2024
60fb033
update readme
brillout Jul 7, 2024
f62bfe7
minor correction
brillout Jul 7, 2024
6991e44
minor
brillout Jul 7, 2024
5b4d2ab
typo
brillout Jul 7, 2024
13e3f40
update
nitedani Jul 7, 2024
fbe9b5e
minor
nitedani Jul 7, 2024
48df27a
minor refactor
brillout Jul 8, 2024
d56e556
typo
brillout Jul 8, 2024
1c0191c
docs: minor
brillout Jul 8, 2024
59f9176
revert vike-react-query readme changes
brillout Jul 8, 2024
d3c836b
polish readme
brillout Jul 8, 2024
043d4df
docs: spacing
brillout Jul 8, 2024
f03e0b0
docs: rewrite and develop withFallback() notes
brillout Jul 8, 2024
36a9ec9
docs: misc
brillout Jul 8, 2024
babf3cc
docs: polish
brillout Jul 8, 2024
31b00c2
minor refactor
brillout Jul 8, 2024
f54b4ca
update
nitedani Jul 8, 2024
ebff329
update
nitedani Jul 8, 2024
e83b83c
docs: add see also
brillout Jul 9, 2024
b9ecd19
Loading config
nitedani Jul 14, 2024
bb8c0cc
readme
nitedani Jul 14, 2024
21b0887
polish
brillout Jul 15, 2024
6e9d448
docs: minor fix
brillout Jul 15, 2024
db551c4
minor refactor
brillout Jul 15, 2024
4c70215
minor
brillout Jul 15, 2024
fc52b8a
comment
brillout Jul 15, 2024
4829ad1
minor refactor
brillout Jul 15, 2024
3913104
small ssr setting improvement
brillout Jul 15, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
- Docs: [README.md](packages/vike-react-query#readme)
- Version history: [CHANGELOG.md](packages/vike-react-query/CHANGELOG.md)
- Source code: [packages/vike-react-query/](packages/vike-react-query)
- `vike-react-apollo` ([Apollo GraphQL](https://www.apollographql.com/docs/react) integration)
- Docs: [README.md](packages/vike-react-apollo#readme)
- Version history: [CHANGELOG.md](packages/vike-react-apollo/CHANGELOG.md)
- Source code: [packages/vike-react-apollo/](packages/vike-react-apollo)

> [!NOTE]
> The source code is [small, simple, and highly polished](https://vike.dev/vike-react#under-the-hood). Contributing is easy and welcome, see [CONTRIBUTING.md](CONTRIBUTING.md) to get started.
2 changes: 2 additions & 0 deletions examples/apollo/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/node_modules/
/dist/
2 changes: 2 additions & 0 deletions examples/apollo/.test-dev.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { testRun } from './.testRun'
testRun('pnpm run dev')
2 changes: 2 additions & 0 deletions examples/apollo/.test-preview.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { testRun } from './.testRun'
testRun('pnpm run preview')
41 changes: 41 additions & 0 deletions examples/apollo/.testRun.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
export { testRun }

import { test, expect, run, fetchHtml, page, getServerUrl, autoRetry } from '@brillout/test-e2e'

function testRun(cmd: `pnpm run ${'dev' | 'preview'}`) {
run(cmd)

const content = 'United States'
const loading = 'Loading contries...'
test('HTML', async () => {
const html = await fetchHtml('/')
expect(getTitle(html)).toBe('My Vike + React App')
// fetchHtml() awaits the stream
expect(html).toContain(content)
})
test('DOM', async () => {
await page.goto(getServerUrl() + '/')
const body = await page.textContent('body')
// Playwright seems to await the HTML stream
expect(body).not.toContain(loading)
expect(body).toContain(content)
await testCounter()
})
}

function getTitle(html: string) {
const title = html.match(/<title>(.*?)<\/title>/i)?.[1]
return title
}

async function testCounter() {
// autoRetry() for awaiting client-side code loading & executing
await autoRetry(
async () => {
expect(await page.textContent('button')).toBe('Counter 0')
await page.click('button')
expect(await page.textContent('button')).toContain('Counter 1')
},
{ timeout: 5 * 1000 }
)
}
36 changes: 36 additions & 0 deletions examples/apollo/assets/logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions examples/apollo/layouts/HeadDefault.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export default HeadDefault

import React from 'react'
import logoUrl from '../assets/logo.svg'

function HeadDefault() {
return (
<>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="description" content="Demo showcasing Vike + React" />
<link rel="icon" href={logoUrl} />
</>
)
}
72 changes: 72 additions & 0 deletions examples/apollo/layouts/LayoutDefault.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
export default LayoutDefault

import './style.css'
import React from 'react'
import logoUrl from '../assets/logo.svg'

function LayoutDefault({ children }: { children: React.ReactNode }) {
return (
<div
style={{
display: 'flex',
maxWidth: 900,
margin: 'auto'
}}
>
<Sidebar>
<Logo />
</Sidebar>
<Content>{children}</Content>
</div>
)
}

function Sidebar({ children }: { children: React.ReactNode }) {
return (
<div
id="sidebar"
style={{
padding: 20,
flexShrink: 0,
display: 'flex',
flexDirection: 'column',
lineHeight: '1.8em',
borderRight: '2px solid #eee'
}}
>
{children}
</div>
)
}

function Content({ children }: { children: React.ReactNode }) {
return (
<div id="page-container">
<div
id="page-content"
style={{
padding: 20,
paddingBottom: 50,
minHeight: '100vh'
}}
>
{children}
</div>
</div>
)
}

function Logo() {
return (
<div
style={{
marginTop: 20,
marginBottom: 10
}}
>
<a href="/">
<img src={logoUrl} height={64} width={64} />
</a>
</div>
)
}
29 changes: 29 additions & 0 deletions examples/apollo/layouts/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* Links */
a {
text-decoration: none;
}
#sidebar a {
padding: 2px 10px;
margin-left: -10px;
}
#sidebar a.is-active {
background-color: #eee;
}

/* Reset */
body {
margin: 0;
font-family: sans-serif;
}
* {
box-sizing: border-box;
}

/* Page Transition Anmiation */
#page-content {
opacity: 1;
transition: opacity 0.3s ease-in-out;
}
body.page-is-transitioning #page-content {
opacity: 0;
}
23 changes: 23 additions & 0 deletions examples/apollo/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"scripts": {
"dev": "vite dev",
"build": "vite build",
"preview": "vite build && vite preview"
},
"dependencies": {
"@types/react": "^18.2.55",
"@types/react-dom": "^18.2.19",
"@vitejs/plugin-react": "^4.2.1",
"react": "18.2.0",
"react-dom": "18.2.0",
"typescript": "^5.3.3",
"vike": "^0.4.178",
"vike-react": "^0.4.17",
"vike-react-apollo": "^0.0.1",
"@apollo/client": "^3.10.8",
"@apollo/client-react-streaming": "^0.11.2",
"graphql": "^16.9.0",
"vite": "^5.1.1"
},
"type": "module"
}
9 changes: 9 additions & 0 deletions examples/apollo/pages/+ApolloClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { ApolloClient, InMemoryCache } from '@apollo/client-react-streaming'
import type { PageContext } from 'vike/types'

// Apollo GraphQL Client with artificial delay: https://gist.github.com/brillout/7d7db0fd6ce55b3b5e8f7ec893eeda01
export default (pageContext: PageContext) =>
new ApolloClient({
uri: 'https://countries.trevorblades.com',
cache: new InMemoryCache()
})
15 changes: 15 additions & 0 deletions examples/apollo/pages/+config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { Config } from 'vike/types'
import Layout from '../layouts/LayoutDefault'
import Head from '../layouts/HeadDefault'
import vikeReact from 'vike-react/config'
import vikeReactApollo from 'vike-react-apollo/config'

// Default configs (can be overridden by pages)
export default {
Layout,
Head,
// <title>
title: 'My Vike + React App',
extends: [vikeReact, vikeReactApollo],
passToClient: ['routeParams']
} satisfies Config
23 changes: 23 additions & 0 deletions examples/apollo/pages/_error/+Page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export default Page

import React from 'react'
import { usePageContext } from 'vike-react/usePageContext'

function Page() {
const { is404 } = usePageContext()
if (is404) {
return (
<>
<h1>404 Page Not Found</h1>
<p>This page could not be found.</p>
</>
)
} else {
return (
<>
<h1>500 Internal Server Error</h1>
<p>Something went wrong.</p>
</>
)
}
}
26 changes: 26 additions & 0 deletions examples/apollo/pages/index/+Page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export default Page

import React from 'react'
import { Counter } from './Counter'
import { Countries } from './Countries'

function Page() {
return (
<>
<h1>My Vike + React app</h1>
This page is:
<ul>
<li>Rendered to HTML.</li>
<li>
Interactive while loading. <Counter />
</li>
</ul>
<div>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin ullamcorper neque magna, a dapibus turpis
volutpat eget. Praesent et aliquam nisi. Integer congue nec ligula et sollicitudin.
</div>
<br />
<Countries />
</>
)
}
9 changes: 9 additions & 0 deletions examples/apollo/pages/index/Counter.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export { Counter }

import React, { useState } from 'react'

function Counter() {
const [count, setCount] = useState(0)

return <button onClick={() => setCount((count) => count + 1)}>Counter {count}</button>
}
24 changes: 24 additions & 0 deletions examples/apollo/pages/index/Countries.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export { Countries }

import { gql, useSuspenseQuery } from '@apollo/client/index.js'
import React from 'react'
import { withFallback } from 'vike-react-apollo'

const Countries = withFallback(() => {
const { data } = useSuspenseQuery<{ countries: { code: string; name: string }[] }>(gql`
{
countries {
code
name
}
}
`)

return (
<ul>
{data.countries.map((country) => (
<li key={country.code}>{country.name}</li>
))}
</ul>
)
})
8 changes: 8 additions & 0 deletions examples/apollo/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Example of using `vike-react-apollo`.

```bash
git clone [email protected]:vikejs/vike-react
cd vike-react/examples/apollo/
npm install
npm run dev
```
13 changes: 13 additions & 0 deletions examples/apollo/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"strict": true,
"module": "ES2020",
"moduleResolution": "Node",
"target": "ES2020",
"lib": ["DOM", "DOM.Iterable", "ESNext"],
"types": ["vite/client"],
"jsx": "react",
"skipLibCheck": true,
"esModuleInterop": true
}
}
7 changes: 7 additions & 0 deletions examples/apollo/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import react from '@vitejs/plugin-react'
import vike from 'vike/plugin'
import { UserConfig } from 'vite'

export default {
plugins: [react(), vike()]
} satisfies UserConfig
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"pnpm": {
"overrides": {
"vike-react": "link:./packages/vike-react/",
"vike-react-query": "link:./packages/vike-react-query/"
"vike-react-query": "link:./packages/vike-react-query/",
"vike-react-apollo": "link:./packages/vike-react-apollo/"
}
},
"devDependencies": {
Expand Down
2 changes: 2 additions & 0 deletions packages/vike-react-apollo/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/node_modules/
/dist/
Empty file.
Loading