Added graphql

This commit is contained in:
Kasper Juul Hermansen 2022-08-20 22:29:54 +02:00
parent 94986d336e
commit 476816a5d9
Signed by: kjuulh
GPG Key ID: 0F95C140730F2F23
27 changed files with 3414 additions and 409 deletions

50
.gitignore vendored
View File

@ -1,24 +1,36 @@
# Logs # See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
logs
*.log # dependencies
/node_modules
/.pnp
.pnp.js
# testing
/coverage
# next.js
/.next/
/out/
# production
/build
# misc
.DS_Store
*.pem
# debug
npm-debug.log* npm-debug.log*
yarn-debug.log* yarn-debug.log*
yarn-error.log* yarn-error.log*
pnpm-debug.log* .pnpm-debug.log*
lerna-debug.log*
node_modules # local env files
dist .env*.local
dist-ssr
*.local
# Editor directories and files # vercel
.vscode/* .vercel
!.vscode/extensions.json
.idea # typescript
.DS_Store *.tsbuildinfo
*.suo next-env.d.ts
*.ntvs*
*.njsproj
*.sln
*.sw?

27
README.md Normal file
View File

@ -0,0 +1,27 @@
# Next.js + Tailwind CSS Example
This example shows how to use [Tailwind CSS](https://tailwindcss.com/) [(v3.0)](https://tailwindcss.com/blog/tailwindcss-v3) with Next.js. It follows the steps outlined in the official [Tailwind docs](https://tailwindcss.com/docs/guides/nextjs).
## Deploy your own
Deploy the example using [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=next-example) or preview live with [StackBlitz](https://stackblitz.com/github/vercel/next.js/tree/canary/examples/with-tailwindcss)
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/git/external?repository-url=https://github.com/vercel/next.js/tree/canary/examples/with-tailwindcss&project-name=with-tailwindcss&repository-name=with-tailwindcss)
## How to use
Execute [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app) with [npm](https://docs.npmjs.com/cli/init), [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/), or [pnpm](https://pnpm.io) to bootstrap the example:
```bash
npx create-next-app --example with-tailwindcss with-tailwindcss-app
```
```bash
yarn create next-app --example with-tailwindcss with-tailwindcss-app
```
```bash
pnpm create next-app --example with-tailwindcss with-tailwindcss-app
```
Deploy it to the cloud with [Vercel](https://vercel.com/new?utm_source=github&utm_medium=readme&utm_campaign=next-example) ([Documentation](https://nextjs.org/docs/deployment)).

11
codegen.yml Normal file
View File

@ -0,0 +1,11 @@
schema: http://127.0.0.1:3001/
documents: './src/lib/**/*.graphql'
generates:
./src/lib/graphql/generated.ts:
plugins:
- typescript
- typescript-operations
- typescript-react-apollo
config:
withHooks: true
fetcher: fetch

10
cuddle.yaml Normal file
View File

@ -0,0 +1,10 @@
# yaml-language-server: $schema=https://git.front.kjuulh.io/kjuulh/cuddle/raw/branch/main/schemas/base.json
base: false
vars:
service: "cibus-frontend"
scripts:
generate_graphql:
type: shell

View File

@ -1,13 +0,0 @@
<!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>

4
next.config.js Normal file
View File

@ -0,0 +1,4 @@
/** @type {import('next').NextConfig} */
module.exports = {
reactStrictMode: true,
}

View File

@ -1,25 +1,32 @@
{ {
"name": "cibus-front",
"private": true, "private": true,
"version": "0.0.0",
"type": "module",
"scripts": { "scripts": {
"dev": "vite", "dev": "next dev",
"build": "tsc && vite build", "build": "next build",
"preview": "vite preview" "start": "next start",
"generate:graphql": "graphql-codegen"
}, },
"dependencies": { "dependencies": {
"react": "^18.2.0", "@apollo/client": "^3.6.9",
"react-dom": "^18.2.0" "graphql": "^16.6.0",
"graphql-tag": "^2.12.6",
"next": "latest",
"react": "18.1.0",
"react-dom": "18.1.0"
}, },
"devDependencies": { "devDependencies": {
"@types/react": "^18.0.15", "@graphql-codegen/cli": "^2.11.6",
"@types/react-dom": "^18.0.6", "@graphql-codegen/typed-document-node": "^2.3.3",
"@vitejs/plugin-react": "^2.0.0", "@graphql-codegen/typescript": "^2.7.3",
"autoprefixer": "^10.4.8", "@graphql-codegen/typescript-operations": "^2.5.3",
"postcss": "^8.4.16", "@graphql-codegen/typescript-react-apollo": "^3.3.3",
"tailwindcss": "^3.1.8", "@graphql-codegen/typescript-react-query": "^4.0.1",
"typescript": "^4.6.4", "@types/node": "17.0.35",
"vite": "^3.0.0" "@types/react": "18.0.9",
"@types/react-dom": "18.0.5",
"autoprefixer": "^10.4.7",
"postcss": "^8.4.14",
"tailwindcss": "^3.1.2",
"typescript": "4.7.2"
} }
} }

File diff suppressed because it is too large Load Diff

BIN
public/favicon.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

4
public/vercel.svg Normal file
View File

@ -0,0 +1,4 @@
<svg width="283" height="64" viewBox="0 0 283 64" fill="none"
xmlns="http://www.w3.org/2000/svg">
<path d="M141.04 16c-11.04 0-19 7.2-19 18s8.96 18 20 18c6.67 0 12.55-2.64 16.19-7.09l-7.65-4.42c-2.02 2.21-5.09 3.5-8.54 3.5-4.79 0-8.86-2.5-10.37-6.5h28.02c.22-1.12.35-2.28.35-3.5 0-10.79-7.96-17.99-19-17.99zm-9.46 14.5c1.25-3.99 4.67-6.5 9.45-6.5 4.79 0 8.21 2.51 9.45 6.5h-18.9zM248.72 16c-11.04 0-19 7.2-19 18s8.96 18 20 18c6.67 0 12.55-2.64 16.19-7.09l-7.65-4.42c-2.02 2.21-5.09 3.5-8.54 3.5-4.79 0-8.86-2.5-10.37-6.5h28.02c.22-1.12.35-2.28.35-3.5 0-10.79-7.96-17.99-19-17.99zm-9.45 14.5c1.25-3.99 4.67-6.5 9.45-6.5 4.79 0 8.21 2.51 9.45 6.5h-18.9zM200.24 34c0 6 3.92 10 10 10 4.12 0 7.21-1.87 8.8-4.92l7.68 4.43c-3.18 5.3-9.14 8.49-16.48 8.49-11.05 0-19-7.2-19-18s7.96-18 19-18c7.34 0 13.29 3.19 16.48 8.49l-7.68 4.43c-1.59-3.05-4.68-4.92-8.8-4.92-6.07 0-10 4-10 10zm82.48-29v46h-9V5h9zM36.95 0L73.9 64H0L36.95 0zm92.38 5l-27.71 48L73.91 5H84.3l17.32 30 17.32-30h10.39zm58.91 12v9.69c-1-.29-2.06-.49-3.2-.49-5.81 0-10 4-10 10V51h-9V17h9v9.2c0-5.08 5.91-9.2 13.2-9.2z" fill="#000"/>
</svg>

After

Width:  |  Height:  |  Size: 1.1 KiB

View File

@ -1 +0,0 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="31.88" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 257"><defs><linearGradient id="IconifyId1813088fe1fbc01fb466" x1="-.828%" x2="57.636%" y1="7.652%" y2="78.411%"><stop offset="0%" stop-color="#41D1FF"></stop><stop offset="100%" stop-color="#BD34FE"></stop></linearGradient><linearGradient id="IconifyId1813088fe1fbc01fb467" x1="43.376%" x2="50.316%" y1="2.242%" y2="89.03%"><stop offset="0%" stop-color="#FFEA83"></stop><stop offset="8.333%" stop-color="#FFDD35"></stop><stop offset="100%" stop-color="#FFA800"></stop></linearGradient></defs><path fill="url(#IconifyId1813088fe1fbc01fb466)" d="M255.153 37.938L134.897 252.976c-2.483 4.44-8.862 4.466-11.382.048L.875 37.958c-2.746-4.814 1.371-10.646 6.827-9.67l120.385 21.517a6.537 6.537 0 0 0 2.322-.004l117.867-21.483c5.438-.991 9.574 4.796 6.877 9.62Z"></path><path fill="url(#IconifyId1813088fe1fbc01fb467)" d="M185.432.063L96.44 17.501a3.268 3.268 0 0 0-2.634 3.014l-5.474 92.456a3.268 3.268 0 0 0 3.997 3.378l24.777-5.718c2.318-.535 4.413 1.507 3.936 3.838l-7.361 36.047c-.495 2.426 1.782 4.5 4.151 3.78l15.304-4.649c2.372-.72 4.652 1.36 4.15 3.788l-11.698 56.621c-.732 3.542 3.979 5.473 5.943 2.437l1.313-2.028l72.516-144.72c1.215-2.423-.88-5.186-3.54-4.672l-25.505 4.922c-2.396.462-4.435-1.77-3.759-4.114l16.646-57.705c.677-2.35-1.37-4.583-3.769-4.113Z"></path></svg>

Before

Width:  |  Height:  |  Size: 1.5 KiB

5
scripts/download.sh Executable file
View File

@ -0,0 +1,5 @@
#!/bin/bash
set -e
pnpm i

9
scripts/generate_graphql.sh Executable file
View File

@ -0,0 +1,9 @@
#!/bin/bash
set -e
cuddle_cli x download
pnpm run generate:graphql
git diff src/lib/graphql/generated.ts

View File

@ -1,9 +0,0 @@
function App() {
return (
<div className="App">
<p className="text-3xl font-bold underline">Some text</p>
</div>
)
}
export default App

59
src/lib/graphql/apollo.ts Normal file
View File

@ -0,0 +1,59 @@
import { IncomingMessage, ServerResponse } from 'http'
import { useMemo } from 'react'
import {
ApolloClient,
InMemoryCache,
NormalizedCacheObject,
} from '@apollo/client'
let apolloClient: ApolloClient<NormalizedCacheObject> | undefined
export type ResolverContext = {
req?: IncomingMessage
res?: ServerResponse
}
function createIsomorphLink(_: ResolverContext = {}) {
if (typeof window === 'undefined') {
} else {
const { HttpLink } = require('@apollo/client')
return new HttpLink({
uri: 'http://127.0.0.1:3001/',
credentials: 'same-origin',
})
}
}
function createApolloClient(context?: ResolverContext) {
return new ApolloClient({
ssrMode: typeof window === 'undefined',
link: createIsomorphLink(context),
cache: new InMemoryCache(),
})
}
export function initializeApollo(
initialState: any = null,
// Pages with Next.js data fetching methods, like `getStaticProps`, can send
// a custom context which will be used by `SchemaLink` to server render pages
context?: ResolverContext
) {
const _apolloClient = apolloClient ?? createApolloClient(context)
// If your page has Next.js data fetching methods that use Apollo Client, the initial state
// get hydrated here
if (initialState) {
_apolloClient.cache.restore(initialState)
}
// For SSG and SSR always create a new Apollo Client
if (typeof window === 'undefined') return _apolloClient
// Create the Apollo Client once in the client
if (!apolloClient) apolloClient = _apolloClient
return _apolloClient
}
export function useApollo(initialState: any) {
const store = useMemo(() => initializeApollo(initialState), [initialState])
return store
}

View File

@ -0,0 +1,5 @@
query GetUpcoming {
getUpcoming {
name
}
}

View File

@ -0,0 +1,80 @@
import { gql } from '@apollo/client';
import * as Apollo from '@apollo/client';
export type Maybe<T> = T | null;
export type InputMaybe<T> = Maybe<T>;
export type Exact<T extends { [key: string]: unknown }> = { [K in keyof T]: T[K] };
export type MakeOptional<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]?: Maybe<T[SubKey]> };
export type MakeMaybe<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]: Maybe<T[SubKey]> };
const defaultOptions = {} as const;
/** All built-in and custom scalars, mapped to their actual values */
export type Scalars = {
ID: string;
String: string;
Boolean: boolean;
Int: number;
Float: number;
};
export type Event = {
__typename?: 'Event';
date: EventDate;
description?: Maybe<Array<Scalars['String']>>;
id: Scalars['String'];
location?: Maybe<Scalars['String']>;
name: Scalars['String'];
};
export type EventDate = {
__typename?: 'EventDate';
day: Scalars['Int'];
hour: Scalars['Int'];
minute: Scalars['Int'];
month: Scalars['Int'];
year: Scalars['Int'];
};
export type QueryRoot = {
__typename?: 'QueryRoot';
getUpcoming: Array<Event>;
};
export type GetUpcomingQueryVariables = Exact<{ [key: string]: never; }>;
export type GetUpcomingQuery = { __typename?: 'QueryRoot', getUpcoming: Array<{ __typename?: 'Event', name: string }> };
export const GetUpcomingDocument = gql`
query GetUpcoming {
getUpcoming {
name
}
}
`;
/**
* __useGetUpcomingQuery__
*
* To run a query within a React component, call `useGetUpcomingQuery` and pass it any options that fit your needs.
* When your component renders, `useGetUpcomingQuery` returns an object from Apollo Client that contains loading, error, and data properties
* you can use to render your UI.
*
* @param baseOptions options that will be passed into the query, supported options are listed on: https://www.apollographql.com/docs/react/api/react-hooks/#options;
*
* @example
* const { data, loading, error } = useGetUpcomingQuery({
* variables: {
* },
* });
*/
export function useGetUpcomingQuery(baseOptions?: Apollo.QueryHookOptions<GetUpcomingQuery, GetUpcomingQueryVariables>) {
const options = {...defaultOptions, ...baseOptions}
return Apollo.useQuery<GetUpcomingQuery, GetUpcomingQueryVariables>(GetUpcomingDocument, options);
}
export function useGetUpcomingLazyQuery(baseOptions?: Apollo.LazyQueryHookOptions<GetUpcomingQuery, GetUpcomingQueryVariables>) {
const options = {...defaultOptions, ...baseOptions}
return Apollo.useLazyQuery<GetUpcomingQuery, GetUpcomingQueryVariables>(GetUpcomingDocument, options);
}
export type GetUpcomingQueryHookResult = ReturnType<typeof useGetUpcomingQuery>;
export type GetUpcomingLazyQueryHookResult = ReturnType<typeof useGetUpcomingLazyQuery>;
export type GetUpcomingQueryResult = Apollo.QueryResult<GetUpcomingQuery, GetUpcomingQueryVariables>;

View File

@ -1,10 +0,0 @@
import React from 'react'
import ReactDOM from 'react-dom/client'
import App from './App'
import "./root.css"
ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
<React.StrictMode>
<App />
</React.StrictMode>
)

12
src/pages/_app.tsx Normal file
View File

@ -0,0 +1,12 @@
import '../styles/globals.css'
import type { AppProps } from 'next/app'
import { useApollo } from '../lib/graphql/apollo'
import { ApolloProvider } from '@apollo/client'
function MyApp({ Component, pageProps }: AppProps) {
const apolloClient = useApollo(pageProps.initialApolloState)
return <ApolloProvider client={apolloClient}><Component {...pageProps} /></ApolloProvider>
}
export default MyApp

95
src/pages/index.tsx Normal file
View File

@ -0,0 +1,95 @@
import type { NextPage } from 'next'
import Head from 'next/head'
import Image from 'next/image'
import { useGetUpcomingQuery } from '../lib/graphql/generated'
const Home: NextPage = () => {
const { data } = useGetUpcomingQuery()
console.log(data)
return (
<div className="flex min-h-screen flex-col items-center justify-center py-2">
<Head>
<title>Create Next App</title>
<link rel="icon" href="/favicon.ico" />
</Head>
<main className="flex w-full flex-1 flex-col items-center justify-center px-20 text-center">
<h1 className="text-6xl font-bold">
Welcome to{' '}
<a className="text-blue-600" href="https://nextjs.org">
Next.js!
</a>
</h1>
<p className="mt-3 text-2xl">
Get started by editing{' '}
<code className="rounded-md bg-gray-100 p-3 font-mono text-lg">
pages/index.tsx
</code>
</p>
<div className="mt-6 flex max-w-4xl flex-wrap items-center justify-around sm:w-full">
<a
href="https://nextjs.org/docs"
className="mt-6 w-96 rounded-xl border p-6 text-left hover:text-blue-600 focus:text-blue-600"
>
<h3 className="text-2xl font-bold">Documentation &rarr;</h3>
<p className="mt-4 text-xl">
Find in-depth information about Next.js features and its API.
</p>
</a>
<a
href="https://nextjs.org/learn"
className="mt-6 w-96 rounded-xl border p-6 text-left hover:text-blue-600 focus:text-blue-600"
>
<h3 className="text-2xl font-bold">Learn &rarr;</h3>
<p className="mt-4 text-xl">
Learn about Next.js in an interactive course with quizzes!
</p>
</a>
<a
href="https://github.com/vercel/next.js/tree/canary/examples"
className="mt-6 w-96 rounded-xl border p-6 text-left hover:text-blue-600 focus:text-blue-600"
>
<h3 className="text-2xl font-bold">Examples &rarr;</h3>
<p className="mt-4 text-xl">
Discover and deploy boilerplate example Next.js projects.
</p>
</a>
<a
href="https://vercel.com/import?filter=next.js&utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
className="mt-6 w-96 rounded-xl border p-6 text-left hover:text-blue-600 focus:text-blue-600"
>
<h3 className="text-2xl font-bold">Deploy &rarr;</h3>
<p className="mt-4 text-xl">
Instantly deploy your Next.js site to a public URL with Vercel.
</p>
</a>
</div>
</main>
<footer className="flex h-24 w-full items-center justify-center border-t">
<a
className="flex items-center justify-center gap-2"
href="https://vercel.com?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
target="_blank"
rel="noopener noreferrer"
>
Powered by{' '}
<Image src="/vercel.svg" alt="Vercel Logo" width={72} height={16} />
</a>
</footer>
</div>
)
}
export default Home
function GetUpcomingDocument(GetUpcomingDocument: any): { data: any } {
throw new Error('Function not implemented.')
}

1
src/vite-env.d.ts vendored
View File

@ -1 +0,0 @@
/// <reference types="vite/client" />

View File

@ -1,6 +1,9 @@
/** @type {import('tailwindcss').Config} */ /** @type {import('tailwindcss').Config} */
module.exports = { module.exports = {
content: ["./index.html", "./src/**/*.{ts,tsx}"], content: [
'./src/pages/**/*.{js,ts,jsx,tsx}',
'./arc/components/**/*.{js,ts,jsx,tsx}',
],
theme: { theme: {
extend: {}, extend: {},
}, },

View File

@ -1,21 +1,30 @@
{ {
"compilerOptions": { "compilerOptions": {
"target": "ESNext", "target": "es5",
"useDefineForClassFields": true, "lib": [
"lib": ["DOM", "DOM.Iterable", "ESNext"], "dom",
"allowJs": false, "dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true, "skipLibCheck": true,
"esModuleInterop": false,
"allowSyntheticDefaultImports": true,
"strict": true, "strict": true,
"forceConsistentCasingInFileNames": true, "forceConsistentCasingInFileNames": true,
"module": "ESNext", "noEmit": true,
"moduleResolution": "Node", "esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true, "resolveJsonModule": true,
"isolatedModules": true, "isolatedModules": true,
"noEmit": true, "jsx": "preserve",
"jsx": "react-jsx" "incremental": true
}, },
"include": ["src"], "include": [
"references": [{ "path": "./tsconfig.node.json" }] "next-env.d.ts",
"**/*.ts",
"**/*.tsx"
],
"exclude": [
"node_modules"
]
} }

View File

@ -1,9 +0,0 @@
{
"compilerOptions": {
"composite": true,
"module": "ESNext",
"moduleResolution": "Node",
"allowSyntheticDefaultImports": true
},
"include": ["vite.config.ts"]
}

View File

@ -1,7 +0,0 @@
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [react()]
})