Skip to content

Commit

Permalink
setup frontend graphql client (#87)
Browse files Browse the repository at this point in the history
## Description

Sets up [Apollo Client](https://www.apollographql.com/docs/react/) for
the frontend's GraphQL client. This sets up:

- Apollo client with in memory cache
- Sets up codegen tool for generating TypeScript types from the GraphQL
schema
- Implements example of fetching GraphQL data on `/datasets` route
  • Loading branch information
codemonkey800 authored Oct 20, 2023
1 parent 1709ab6 commit 1e18a16
Show file tree
Hide file tree
Showing 10 changed files with 2,158 additions and 160 deletions.
1 change: 1 addition & 0 deletions frontend/.dockerignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
__generated__
.env.example
.eslintignore
.gitignore
Expand Down
1 change: 1 addition & 0 deletions frontend/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
__generated__
node_modules/
3 changes: 2 additions & 1 deletion frontend/.prettierignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
__generated__/
build/
public/build/
pnpm-lock.yaml
public/build/
1 change: 1 addition & 0 deletions frontend/packages/data-portal/.eslintignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.cache/
app/__generated__
app/**/*.module.css.d.ts
build/
node_modules/
Expand Down
11 changes: 11 additions & 0 deletions frontend/packages/data-portal/app/apollo.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { ApolloClient, createHttpLink, InMemoryCache } from '@apollo/client'

import { ENVIRONMENT_CONTEXT_DEFAULT_VALUE } from './context/Environment.context'

export const apolloClient = new ApolloClient({
ssrMode: true,
cache: new InMemoryCache(),
link: createHttpLink({
uri: process.env.API_URL ?? ENVIRONMENT_CONTEXT_DEFAULT_VALUE.API_URL,
}),
})
30 changes: 30 additions & 0 deletions frontend/packages/data-portal/app/routes/datasets.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,35 @@
import { useLoaderData } from '@remix-run/react'
import { json } from '@remix-run/server-runtime'
import { useEffect } from 'react'

import { gql } from 'app/__generated__'
import { apolloClient } from 'app/apollo.server'
import { Demo } from 'app/components/Demo'

const GET_ALL_DATASETS_QUERY = gql(`
query GetDatasets {
datasets {
authors {
name
}
}
}
`)

export async function loader() {
const { data } = await apolloClient.query({
query: GET_ALL_DATASETS_QUERY,
})

return json(data.datasets)
}

export default function AllDatasetsPage() {
const data = useLoaderData<typeof loader>()

useEffect(() => {
console.log('dataset authors', data)
}, [data])

return <Demo>All Datasets Page</Demo>
}
23 changes: 23 additions & 0 deletions frontend/packages/data-portal/codegen.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { CodegenConfig } from '@graphql-codegen/cli'

const SCHEMA_URL =
process.env.API_URL ||
'https://graphql.cryoetdataportal.cziscience.com/v1/graphql'

const config: CodegenConfig = {
schema: SCHEMA_URL,
documents: ['app/**/*.{ts,tsx}'],
generates: {
'./app/__generated__/': {
preset: 'client',
plugins: [],
presetConfig: {
gqlTagName: 'gql',
},
},
},
ignoreNoDocuments: true,
}

// eslint-disable-next-line import/no-default-export
export default config
16 changes: 12 additions & 4 deletions frontend/packages/data-portal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
"name": "@cryoet-data-portal/data-portal",
"type": "module",
"scripts": {
"build": "run-p -l build:*",
"build:remix": "remix build",
"build": "run-s -l build:*",
"build:codegen": "graphql-codegen",
"build:tcm": "tcm -p 'app/**/*.module.css'",
"clean": "del build public/build .cache/ node_modules/.cache/ app/**/*.css.d.ts",
"dev": "run-p -l dev:*",
"build:remix": "remix build",
"clean": "del build public/build .cache/ node_modules/.cache/",
"dev": "pnpm build:codegen && run-p -l dev:*",
"dev:codegen": "pnpm build:codegen -w",
"dev:remix": "PORT=8080 remix dev",
"dev:tcm": "pnpm build:tcm -w",
"lint": "eslint --report-unused-disable-directives --max-warnings 0 .",
Expand All @@ -16,12 +18,16 @@
"test": "jest"
},
"dependencies": {
"@apollo/client": "^3.8.4",
"@babel/runtime": "^7.23.1",
"@czi-sds/components": "^18.1.0",
"@emotion/cache": "^11.11.0",
"@emotion/react": "^11.11.1",
"@emotion/server": "^11.11.0",
"@emotion/styled": "^11.11.0",
"@graphql-codegen/cli": "^5.0.0",
"@graphql-codegen/client-preset": "^4.1.0",
"@graphql-typed-document-node/core": "^3.2.0",
"@mui/base": "^5.0.0-beta.17",
"@mui/icons-material": "^5.14.11",
"@mui/lab": "^5.0.0-alpha.146",
Expand All @@ -34,6 +40,7 @@
"@remix-run/server-runtime": "^2.0.1",
"clsx": "^2.0.0",
"framer-motion": "^10.16.4",
"graphql": "^16.8.1",
"isbot": "^3.7.0",
"jotai": "^2.4.3",
"lodash": "^4.17.21",
Expand All @@ -46,6 +53,7 @@
"tailwind-merge": "^1.14.0"
},
"devDependencies": {
"@parcel/watcher": "^2.3.0",
"@remix-run/dev": "^2.0.1",
"@tailwindcss/typography": "^0.5.10",
"@testing-library/jest-dom": "^6.1.3",
Expand Down
15 changes: 14 additions & 1 deletion frontend/packages/data-portal/remix.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@
* @type {import('@remix-run/dev').AppConfig}
*/
export default {
serverDependenciesToBundle: [/@mui\/.*/],
serverDependenciesToBundle: [
// Apollo
'graphql-tag',
'optimism',
'symbol-observable',
'ts-invariant',
'tslib',
'zen-observable-ts',
/@apollo\/.*/,
/@wry\/.*/,

// Material UI
/@mui\/.*/,
],
serverMinify: true,
}
Loading

0 comments on commit 1e18a16

Please sign in to comment.