Skip to content

Commit 0744cfd

Browse files
authored
chore(docs): Add fragments to GraphQL typegen (#36386)
initial
1 parent c5ad03a commit 0744cfd

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

docs/docs/how-to/local-development/graphql-typegen.md

+48
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,54 @@ export const createSchemaCustomization: GatsbyNode["createSchemaCustomization"]
105105

106106
Read the [Customizing the GraphQL schema guide](/docs/reference/graphql-data-layer/schema-customization/) to learn how to explicitly define types in your site or source plugin.
107107

108+
### GraphQL fragments
109+
110+
Fragments allow you to reuse parts of GraphQL queries throughout your site and collocate specific parts of a query to individual files. Learn more about it in the [Using GraphQL fragments guide](/docs/reference/graphql-data-layer/using-graphql-fragments/).
111+
112+
In the context of GraphQL Typegen fragments enable you to have individual TypeScript types for nested parts of your queries since each fragment will be its own TypeScript type. You then can use these types to e.g. type arguments of components consuming the GraphQL data.
113+
114+
Here's an example (also used in [using-graphql-typegen](https://github.com/gatsbyjs/gatsby/tree/master/examples/using-graphql-typegen)) with a `Info` component that gets the `buildTime` as an argument. This `Info` component and its `SiteInformation` fragment is used in the `src/pages/index.tsx` file then:
115+
116+
```tsx:title=src/components/info.tsx
117+
import * as React from "react"
118+
import { graphql } from "gatsby"
119+
120+
// highlight-next-line
121+
const Info = ({ buildTime }: { buildTime?: any }) => {
122+
return (
123+
<p>
124+
Build time: {buildTime}
125+
</p>
126+
)
127+
}
128+
129+
export default Info
130+
131+
// highlight-start
132+
export const query = graphql`
133+
fragment SiteInformation on Site {
134+
buildTime
135+
}
136+
`
137+
// highlight-end
138+
```
139+
140+
```graphql:title=src/pages/index.tsx
141+
# Rest of the page above...
142+
143+
query IndexPage {
144+
site {
145+
...SiteInformation
146+
}
147+
}
148+
```
149+
150+
This way a `SiteInformationFragment` TypeScript type will be created that you can use in the `Info` component:
151+
152+
```tsx:title=src/components/info.tsx
153+
const Info = ({ buildTime }: { buildTime?: Queries.SiteInformationFragment["buildTime"] }) => {}
154+
```
155+
108156
### Tips
109157

110158
- When adding a new key to your GraphQL query you'll need to save the file before new TypeScript types are generated. The autogenerated files are only updated on file saves.

examples/using-graphql-typegen/src/components/info.tsx

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import * as React from "react"
22
import { graphql } from "gatsby"
33

4-
5-
const Info = ({ buildTime }: { buildTime?: string | null }) => {
4+
const Info = ({ buildTime }: { buildTime?: Queries.SiteInformationFragment["buildTime"] }) => {
65
return (
76
<p>
87
Build time: {buildTime}

0 commit comments

Comments
 (0)