-
Notifications
You must be signed in to change notification settings - Fork 4
Autogenerate schema types from supgergraph.graphql #68
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,8 +7,66 @@ The initial aim of this app to provide a science facing UI for the waffle projec | |
| To run this locally: | ||
|
|
||
| 1. Start the devcontainer in VSCode | ||
| 2. To make sure you have the most up-to-date environment either run `pnpm install` in the container or rebuild the container (via the Ctrl+Shift+P menu in VSCode) | ||
| 3. In a terminal in this devcontainer run either: | ||
| 1. To make sure you have the most up-to-date environment either run `pnpm install` in the container or rebuild the container (via the Ctrl+Shift+P menu in VSCode) | ||
| 1. In a terminal in this devcontainer run either: | ||
| - `VITE_QUEUE_MODE=local turbo dev --filter @atlas/i15-1` if running a local queue server on http://127.0.0.1:8001, **OR** | ||
| - `turbo dev --filter @atlas/i15-1` to use mocked backends | ||
| 4. Navigate to http://localhost:5173/ | ||
| 1. Navigate to http://localhost:5173/ | ||
|
|
||
| ## Updating Supergraph Schema Types | ||
|
|
||
| 1. Navigate to app containing folder (i.e. `.../atlas/apps/i15-1`) | ||
| 1. Update the `supergraph.graphql` schema from most recent `supergraph.graphql` schema from https://github.com/DiamondLightSource/graph-federation/releases | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could: Presumably we can automate this step too so that we just run something that pulls the latest schema and generates? Happy to spin into new issue
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is limited by how (stable) graph updates - currently this is a manual process to move to the latest release so the latest |
||
| 1. Generate types by running `pnpm run generate:graphql` | ||
|
|
||
| The available types are generated based on queries detected in the documents (`[./src/**/*.ts*]`), so if a new query is added you will need to generate the types again. Type names will be automatically generated based on your query name. To use, follow the example below: | ||
|
|
||
| 1. Add a new query | ||
|
|
||
| ```ts | ||
| import { gql } from "@apollo/client"; | ||
|
|
||
| export const myNewQuery = gql` | ||
| query GetSessionPlaylist($proposal: Int!, $session: Int!) { | ||
| instrumentSession( | ||
| proposalNumber: $proposal | ||
| instrumentSessionNumber: $session | ||
| ) { | ||
| state | ||
| } | ||
| } | ||
| `; | ||
| ``` | ||
|
|
||
| 2. Generate types by running `pnpm run generate:graphql`. This will generate a new `.generated.ts` file with the types. | ||
| 3. Use query and types in your component. | ||
|
|
||
| ```ts | ||
| import { useQuery } from "@apollo/client/react"; | ||
| import type { TypedDocumentNode } from "@apollo/client"; | ||
|
|
||
| import { myNewQuery } from "../graphql/myNewQuery"; | ||
| import type { | ||
| myNewQueryVariables, | ||
| myNewQuery, | ||
| } from "../graphql/getSessionPlaylistQuery.generated"; | ||
|
|
||
| const GET_EXPERIMENTS: TypedDocumentNode< | ||
| myNewQuery, | ||
| myNewQueryVariables | ||
| > = myNewQuery; | ||
|
|
||
| export function QueryData() { | ||
| const { data, loading, error } = useQuery(GET_EXPERIMENTS, { | ||
| variables: { | ||
| proposal: 44163, | ||
| session: 3, | ||
| }, | ||
| }); | ||
|
|
||
| if (loading) return <p>Loading...</p>; | ||
| if (error) return <p>Error: {error.message}</p>; | ||
|
|
||
| return data?.instrumentSession?.state || []; | ||
| } | ||
| ``` | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import type { CodegenConfig } from "@graphql-codegen/cli"; | ||
|
|
||
| const config: CodegenConfig = { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should: Can we call this file |
||
| overwrite: true, | ||
|
|
||
| schema: "./src/graphql/supergraph.graphql", | ||
| documents: ["./src/**/*.{ts,tsx}"], | ||
| ignoreNoDocuments: true, | ||
|
|
||
| generates: { | ||
| "./src/graphql/__generated__/": { | ||
| plugins: ["typescript-operations"], | ||
| config: { | ||
| generateOperationTypes: false, | ||
| }, | ||
| }, | ||
|
|
||
| "./src/": { | ||
| preset: "near-operation-file", | ||
| plugins: ["typescript-operations"], | ||
| config: { | ||
| importSchemaTypesFrom: "./src/graphql/__generated__/", | ||
| nonOptionalTypename: true, | ||
| skipTypeNameForRoot: true, | ||
| }, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| export default config; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| /** Internal type. DO NOT USE DIRECTLY. */ | ||
| type Exact<T extends { [key: string]: unknown }> = { [K in keyof T]: T[K] }; | ||
| /** Internal type. DO NOT USE DIRECTLY. */ | ||
| export type Incremental<T> = T | { [P in keyof T]?: P extends ' $fragmentName' | '__typename' ? T[P] : never }; | ||
| export type GetSessionPlaylistQueryVariables = Exact<{ | ||
| proposal: number; | ||
| session: number; | ||
| }>; | ||
|
|
||
|
|
||
| export type GetSessionPlaylistQuery = { instrumentSession: { __typename: 'InstrumentSession', experiments: { __typename: 'ExperimentConnection', edges: Array<{ __typename: 'ExperimentEdge', node: { __typename: 'Experiment', name: string, sample: { __typename: 'Sample', name: string, data: unknown }, experimentDefinition: { __typename: 'ExperimentDefinition', name: string, data: unknown } } }> } } | null }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| import { gql } from "@apollo/client"; | ||
|
|
||
| export const getSessionPlaylistQuery = gql` | ||
| query GetSessionPlaylist($proposal: Int!, $session: Int!) { | ||
| instrumentSession( | ||
| proposalNumber: $proposal | ||
| instrumentSessionNumber: $session | ||
| ) { | ||
| experiments { | ||
| edges { | ||
| node { | ||
| name | ||
| sample { | ||
| name | ||
| data | ||
| } | ||
| experimentDefinition { | ||
| name | ||
| data | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| `; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Numbering of these bullet points is a bit strange