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

feat: add vike-react-apollo #134

Merged
merged 1 commit into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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