Skip to content

Commit d0eaeea

Browse files
authored
Merge pull request #1102 from cometkim/typegen
Stricter GraphQL typing using gatsby-plugin-typegen
2 parents f744f13 + c8a1456 commit d0eaeea

File tree

17 files changed

+912
-141
lines changed

17 files changed

+912
-141
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,6 @@ yarn-error.log
7171

7272
# Swap files
7373
*.swp
74+
75+
# Codegen stuff
76+
src/__generated__/

gatsby-config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,5 +115,11 @@ module.exports = {
115115
],
116116
},
117117
},
118+
{
119+
resolve: "gatsby-plugin-typegen",
120+
options: {
121+
outputPath: "src/__generated__/gatsby-types.d.ts",
122+
},
123+
},
118124
],
119125
}

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
"clean": "gatsby clean",
1212
"test": "echo \"Write tests! -> https://gatsby.dev/unit-testing\" && exit 1"
1313
},
14+
"resolutions": {
15+
"graphql": "15.6.0"
16+
},
1417
"dependencies": {
1518
"@graphql-tools/schema": "8.2.0",
1619
"@weknow/gatsby-remark-twitter": "0.2.3",
@@ -23,6 +26,7 @@
2326
"gatsby-plugin-google-analytics": "3.14.0",
2427
"gatsby-plugin-less": "5.14.0",
2528
"gatsby-plugin-react-helmet": "4.14.0",
29+
"gatsby-plugin-typegen": "^2.2.4",
2630
"gatsby-plugin-webfonts": "2.1.1",
2731
"gatsby-source-filesystem": "3.14.0",
2832
"gatsby-transformer-remark": "4.11.0",

src/components/BlogSidebar/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ interface Props {
77
}
88

99
const BlogSidebar = ({ posts, currentPermalink }: Props) => {
10-
const allTags = useStaticQuery(graphql`
10+
const allTags = useStaticQuery<GatsbyTypes.allTagsQuery>(graphql`
1111
query allTags {
1212
allMarkdownRemark {
1313
group(field: frontmatter___tags) {
@@ -28,7 +28,7 @@ const BlogSidebar = ({ posts, currentPermalink }: Props) => {
2828
<div className="nav-docs-section categories">
2929
<h3>Categories</h3>
3030
<ul>
31-
{allTags.map(({ fieldValue }: { fieldValue: string }, i: number) => {
31+
{allTags.map(({ fieldValue = '' }, i: number) => {
3232
const tag = fieldValue[0].toUpperCase() + fieldValue.substring(1)
3333
return (
3434
<li key={i}>

src/components/Footer/index.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ interface FooterLinks {
1313
subsections: LinkItem[]
1414
}
1515

16-
const getLinks = (sourcePath: string): FooterLinks[] => [
16+
const getLinks = (sourcePath?: string): FooterLinks[] => [
1717
{
1818
text: "Learn",
1919
href: "/learn/",
@@ -70,18 +70,18 @@ const getLinks = (sourcePath: string): FooterLinks[] => [
7070
{ text: "Logo and Brand Guidelines", href: "/brand" },
7171
{ text: "Code of Conduct", href: "/codeofconduct/" },
7272
{ text: "Contact Us", href: "/foundation/contact/" },
73-
{
73+
sourcePath && {
7474
text: "Edit this page",
7575
href:
7676
"https://github.com/graphql/graphql.github.io/edit/source/" +
7777
sourcePath,
7878
icon: "/img/edit.svg",
7979
},
80-
],
80+
].filter(Boolean) as LinkItem[],
8181
},
8282
]
8383

84-
const Footer = ({ sourcePath }: { sourcePath: string }) => {
84+
const Footer = ({ sourcePath }: { sourcePath?: string }) => {
8585
return (
8686
<div>
8787
<footer>

src/components/Layout/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import Seo from "../Seo"
66
import "../../assets/css/style.less"
77

88
interface PageContext {
9-
sourcePath: string
9+
sourcePath?: string
1010
}
1111

1212
interface Props {

src/components/Seo/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ interface Props {
88
}
99

1010
const Seo = ({ title, description }: Props): JSX.Element => {
11-
const data = useStaticQuery(graphql`
11+
const data = useStaticQuery<GatsbyTypes.SeoQueryQuery>(graphql`
1212
query SeoQuery {
1313
site {
1414
siteMetadata {

src/pages/blog.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ import Layout from "../components/Layout"
33
import BlogPost from "../components/BlogPost"
44
import BlogSidebar from "../components/BlogSidebar"
55
import { graphql } from "gatsby"
6+
import type { PageProps } from "gatsby";
67

7-
export default ({ pageContext, data }: any) => {
8+
export default ({ pageContext, data }: PageProps<GatsbyTypes.GetAllBlogPostsQuery, GatsbyTypes.SitePageContext>) => {
89
const posts = data.allMarkdownRemark.edges
9-
.map((e: any) => e.node)
10-
.sort((a: any, b: any) => {
11-
const aDate = new Date(a.frontmatter.date)
12-
const bDate = new Date(b.frontmatter.date)
10+
.map(e => e.node)
11+
.sort((a, b) => {
12+
const aDate = new Date(a?.frontmatter?.date ?? 0)
13+
const bDate = new Date(b?.frontmatter?.date ?? 0)
1314
if (aDate > bDate) {
1415
return -1
1516
} else if (aDate < bDate) {
@@ -64,7 +65,7 @@ export default ({ pageContext, data }: any) => {
6465
}
6566

6667
export const query = graphql`
67-
query getAllBlogPosts {
68+
query GetAllBlogPosts {
6869
allMarkdownRemark(
6970
filter: { frontmatter: { permalink: { regex: "/blog/" } } }
7071
) {

src/pages/brand.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import React, { useState } from "react"
2+
import type { PageProps } from "gatsby"
23
import Layout from "../components/Layout"
34

4-
export default ({ pageContext }) => {
5+
export default ({ pageContext }: PageProps<object, GatsbyTypes.SitePageContext>) => {
56
return (
67
<Layout
78
title="GraphQL logo, brand guidelines and assets"

src/pages/code.tsx

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,33 @@
1+
import type { PageProps } from "gatsby"
12
import { AnchorLink } from "gatsby-plugin-anchor-links"
23
import React, { useState } from "react"
34
import Layout from "../components/Layout"
45
import Marked from "../components/Marked"
56
import { toSlug } from "../utils/slug"
67

7-
export function buildLanguagesMenu(pageContext: any) {
8+
export function buildLanguagesMenu(pageContext: GatsbyTypes.SitePageContext) {
89
return (
910
<div className="language-boxes">
10-
{pageContext.languageList.map(({ name: languageName }) => {
11-
const slug = toSlug(languageName)
12-
return (
13-
<AnchorLink
14-
to={`#${slug}`}
15-
className="article language-box"
16-
title={languageName}
17-
>
18-
<span className="article_title">{languageName}</span>
19-
</AnchorLink>
20-
)
21-
})}
11+
{pageContext.languageList
12+
?.map(langeuage => langeuage?.name!).filter(Boolean)
13+
.map(languageName => {
14+
const slug = toSlug(languageName)
15+
return (
16+
<AnchorLink
17+
to={`#${slug}`}
18+
className="article language-box"
19+
title={languageName}
20+
>
21+
<span className="article_title">{languageName}</span>
22+
</AnchorLink>
23+
)
24+
})
25+
}
2226
</div>
2327
)
2428
}
2529

26-
export function buildLibraryContent(library: any, pageContext: any) {
30+
export function buildLibraryContent(library: any, pageContext: GatsbyTypes.SitePageContext) {
2731
const [ overflown, setOverflown ] = useState(false);
2832
const [ expanded, setExpanded ] = useState(false);
2933
return (
@@ -113,7 +117,7 @@ export function buildLibraryContent(library: any, pageContext: any) {
113117
)
114118
}
115119

116-
export function buildLibraryList(libraries: any[], pageContext: any) {
120+
export function buildLibraryList(libraries: readonly any[], pageContext: any) {
117121
return (
118122
<div className="library-list">
119123
{libraries.map(library => buildLibraryContent(library, pageContext))}
@@ -194,7 +198,7 @@ export function buildLanguagesContent(pageContext: any) {
194198
return <div className="languages-content">{elements}</div>
195199
}
196200

197-
export default ({ pageContext }: any) => {
201+
export default ({ pageContext }: PageProps<object, GatsbyTypes.SitePageContext>) => {
198202
return (
199203
<Layout title="GraphQL Code Libraries, Tools and Services" className="code" pageContext={pageContext}>
200204
<div className="code-hero">
@@ -240,15 +244,15 @@ export default ({ pageContext }: any) => {
240244
#
241245
</AnchorLink>
242246
</h2>
243-
{buildLibraryList(pageContext.otherLibraries.Tools, pageContext)}
247+
{buildLibraryList(pageContext.otherLibraries?.Tools ?? [], pageContext)}
244248
<h2>
245249
<a className="anchor" id="services"></a>
246250
Services
247251
<AnchorLink className="hash-link" to="#services">
248252
#
249253
</AnchorLink>
250254
</h2>
251-
{buildLibraryList(pageContext.otherLibraries.Services, pageContext)}
255+
{buildLibraryList(pageContext.otherLibraries?.Services ?? [], pageContext)}
252256
</div>
253257
</div>
254258
<p>Want to improve this page? See the <a href="https://github.com/graphql/graphql.github.io/blob/source/notes/ContributingToCodePage.md">docs here</a>.</p>

0 commit comments

Comments
 (0)