Skip to content

Commit d8ff2e6

Browse files
Merge pull request #38 from howtographql/add-query-types
Add query types
2 parents 3ff9078 + 1eb427c commit d8ff2e6

File tree

14 files changed

+522
-232
lines changed

14 files changed

+522
-232
lines changed

packages/gatsby-theme/src/components/community/BookmarkMutation.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import { Mutation } from 'react-apollo';
33
import { loginUser } from '../../utils/auth';
44
import { BookmarkButton } from '../shared/buttons';
55
import { BookmarkTutorial } from '../../utils/queries';
6+
import { BookmarkTutorialMutation } from '../../graphqlTypes';
67
import { optionalChaining } from '../../utils/helpers';
7-
88
import { handleMutationResponse, ApiErrors } from '../../utils/errorHandling';
99

1010
const BookmarkMutation = ({ tutorial }) => (
11-
<Mutation
11+
<Mutation<BookmarkTutorialMutation>
1212
mutation={BookmarkTutorial}
1313
variables={{
1414
id: tutorial.id,

packages/gatsby-theme/src/components/community/TutorialListing.tsx

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,24 @@ import UpvoteMutation from './UpvoteMutation';
77
import BookmarkMutation from './BookmarkMutation';
88
import ProgressBanner from './ProgressBanner';
99
import { getTutorialbyGatsbyID } from '../../utils/queries';
10+
import { GetTutorialbyGatsbyIdQuery, Mdx } from '../../graphqlTypes';
1011

1112
type TutorialListingProps = {
12-
tutorial: Tutorial;
13-
};
14-
15-
type Tutorial = {
16-
id: string;
17-
fileAbsolutePath: string;
18-
frontmatter: FrontMatter;
19-
};
20-
21-
type FrontMatter = {
22-
id: string;
23-
tutorialTitle: string;
24-
description: string;
13+
tutorial: Mdx;
2514
};
2615

2716
const TutorialListing: React.FunctionComponent<TutorialListingProps> = ({
2817
tutorial,
2918
}) => {
30-
const gatsbyID = tutorial.frontmatter.id;
31-
let tutorialPath = getTutorialOverviewSlug(tutorial.fileAbsolutePath);
19+
const gatsbyID = tutorial!.frontmatter!.id;
20+
let tutorialPath = getTutorialOverviewSlug(tutorial!.fileAbsolutePath);
3221
return (
33-
<Query query={getTutorialbyGatsbyID} variables={{ gatsbyID: gatsbyID }}>
22+
<Query<GetTutorialbyGatsbyIdQuery>
23+
query={getTutorialbyGatsbyID}
24+
variables={{ gatsbyID: gatsbyID }}
25+
>
3426
{({ data }) => {
27+
const gatsbyTutorial = data!.getTutorialbyGatsbyID;
3528
return (
3629
<Card
3730
width={[1]}
@@ -43,20 +36,19 @@ const TutorialListing: React.FunctionComponent<TutorialListingProps> = ({
4336
>
4437
<Flex alignItems="center" justifyContent="center">
4538
<Box width={1 / 12}>
46-
{data.getTutorialbyGatsbyID && (
39+
{gatsbyTutorial && (
4740
<div>
48-
<UpvoteMutation tutorial={data.getTutorialbyGatsbyID} />
49-
<BookmarkMutation tutorial={data.getTutorialbyGatsbyID} />
50-
<ProgressBanner tutorial={data.getTutorialbyGatsbyID} />
41+
<UpvoteMutation tutorial={gatsbyTutorial} />
42+
<BookmarkMutation tutorial={gatsbyTutorial} />
43+
<ProgressBanner tutorial={gatsbyTutorial} />
5144
</div>
5245
)}
5346
</Box>
54-
5547
<Box width={11 / 12}>
5648
<Link to={tutorialPath}>
57-
<Heading>{tutorial.frontmatter.tutorialTitle}</Heading>
49+
<Heading>{tutorial!.frontmatter!.tutorialTitle}</Heading>
5850
</Link>
59-
<Text>{tutorial.frontmatter.description}</Text>
51+
<Text>{tutorial!.frontmatter!.description}</Text>
6052
</Box>
6153
</Flex>
6254
</Card>

packages/gatsby-theme/src/components/community/UpvoteMutation.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@ import { VoteButton } from '../shared/buttons';
66
import { Heading, Flex } from '../shared/base';
77
import { optionalChaining } from '../../utils/helpers';
88
import { UpvoteTutorial } from '../../utils/queries';
9+
import { UpvoteTutorialMutation } from '../../graphqlTypes';
910

1011
const UpvoteMutation = ({ tutorial }) => (
11-
<Mutation mutation={UpvoteTutorial} variables={{ id: tutorial.id }}>
12+
<Mutation<UpvoteTutorialMutation>
13+
mutation={UpvoteTutorial}
14+
variables={{ id: tutorial.id }}
15+
>
1216
{upvote => {
1317
let active = optionalChaining(() => tutorial.viewerUserTutorial.upvoted);
1418
let upvotes = optionalChaining(() => tutorial.upvotes);

packages/gatsby-theme/src/components/courses/CourseCard.tsx

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ import { Query } from 'react-apollo';
44
import ProgressBar from '../shared/ProgressBar';
55
import { TutorialButton } from '../shared/buttons';
66
import { getTutorialbyGatsbyID } from '../../utils/queries';
7+
import { GetTutorialbyGatsbyIdQuery, Maybe } from '../../graphqlTypes';
78
import { getTutorialOverviewSlug } from '../../utils/getTutorialSlug';
89
import { optionalChaining, percent } from '../../utils/helpers';
910

1011
type CourseCardProps = {
11-
tutorialTitle: string;
12-
fileAbsolutePath: string;
13-
gatsbyID: string;
12+
tutorialTitle: Maybe<string>;
13+
fileAbsolutePath: Maybe<string>;
14+
gatsbyID: Maybe<string>;
1415
};
1516

1617
const CourseCard: React.FunctionComponent<CourseCardProps> = ({
@@ -27,19 +28,23 @@ const CourseCard: React.FunctionComponent<CourseCardProps> = ({
2728
src="https://i.ibb.co/TcKwmwR/Icons.png"
2829
/>
2930
<h3>{tutorialTitle}</h3>
30-
<Query query={getTutorialbyGatsbyID} variables={{ gatsbyID: gatsbyID }}>
31+
<Query<GetTutorialbyGatsbyIdQuery>
32+
query={getTutorialbyGatsbyID}
33+
variables={{ gatsbyID: gatsbyID }}
34+
>
3135
{({ data }) => {
3236
let buttonText = 'Start Tutorial';
3337
let percentage = 0;
3438
if (
3539
optionalChaining(
3640
() =>
37-
data.getTutorialbyGatsbyID.viewerUserTutorial.currentChapter,
41+
data!.getTutorialbyGatsbyID!.viewerUserTutorial!
42+
.currentChapter,
3843
)
3944
) {
4045
percentage = percent(
41-
data.getTutorialbyGatsbyID.numberofChapters,
42-
data.getTutorialbyGatsbyID.viewerUserTutorial.currentChapter,
46+
data!.getTutorialbyGatsbyID!.numberofChapters,
47+
data!.getTutorialbyGatsbyID!.viewerUserTutorial!.currentChapter,
4348
);
4449
buttonText = 'Continue Tutorial';
4550
if (percentage === 100) {
Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as React from 'react';
22
import { Heading, Text, Flex, Box } from '../shared/base';
33
import CourseCard from './CourseCard';
4+
import { Mdx } from '../../graphqlTypes';
45

56
type CourseSectionProps = {
67
heading: string;
@@ -9,47 +10,33 @@ type CourseSectionProps = {
910
};
1011

1112
type QueryData = {
12-
node: Node;
13-
};
14-
15-
type Node = {
16-
id: string;
17-
fileAbsolutePath: string;
18-
frontmatter: Frontmatter;
19-
};
20-
21-
type Frontmatter = {
22-
id: string;
23-
tutorialTitle: string;
24-
description: string;
13+
node: Mdx;
2514
};
2615

2716
const CourseSection: React.FunctionComponent<CourseSectionProps> = ({
2817
heading,
2918
body,
3019
data,
31-
}) => {
32-
return (
33-
<Flex m={[1, 1, 1]}>
34-
<Box width={[0.2, 0.2, 0.2]}>
35-
<Heading> {heading} </Heading>
36-
<Text>{body}</Text>
37-
</Box>
38-
<Box width={[0.8, 0.8, 0.8]}>
39-
<Flex alignItems="top" justifyContent="center" flexWrap="wrap">
40-
{data.map(tutorial => (
41-
<Box width={[1, 0.8, 0.4]} key={tutorial.node.id}>
42-
<CourseCard
43-
gatsbyID={tutorial.node.frontmatter.id}
44-
tutorialTitle={tutorial.node.frontmatter.tutorialTitle}
45-
fileAbsolutePath={tutorial.node.fileAbsolutePath}
46-
/>
47-
</Box>
48-
))}
49-
</Flex>
50-
</Box>
51-
</Flex>
52-
);
53-
};
20+
}) => (
21+
<Flex m={[1, 1, 1]}>
22+
<Box width={[0.2, 0.2, 0.2]}>
23+
<Heading> {heading} </Heading>
24+
<Text>{body}</Text>
25+
</Box>
26+
<Box width={[0.8, 0.8, 0.8]}>
27+
<Flex alignItems="top" justifyContent="center" flexWrap="wrap">
28+
{data.map(tutorial => (
29+
<Box width={[1, 0.8, 0.4]} key={tutorial!.node!.id}>
30+
<CourseCard
31+
gatsbyID={tutorial!.node!.frontmatter!.id}
32+
tutorialTitle={tutorial!.node!.frontmatter!.tutorialTitle}
33+
fileAbsolutePath={tutorial!.node!.fileAbsolutePath}
34+
/>
35+
</Box>
36+
))}
37+
</Flex>
38+
</Box>
39+
</Flex>
40+
);
5441

5542
export default CourseSection;

packages/gatsby-theme/src/components/templates/Tutorial.tsx

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,18 @@ const TutorialLayout: React.FunctionComponent<TutorialLayoutProps> = ({
2222
}
2323

2424
//title of the chapter
25-
const { pageTitle } = optionalChaining(() => data.mdx.frontmatter);
25+
const { pageTitle } = optionalChaining(() => data!.mdx!.frontmatter);
2626
//title of the tutorial
2727
const tutorialTitle = optionalChaining(
28-
() => data.tutorialTitle.frontmatter.tutorialTitle,
28+
() => data!.tutorialTitle!.frontmatter!.tutorialTitle,
2929
);
3030
//gatsbyID to fetch user information about tutorial
31-
const gatsbyID = optionalChaining(() => data.tutorialTitle.frontmatter.id);
31+
const gatsbyID = optionalChaining(() => data!.tutorialTitle!.frontmatter!.id);
3232
//all chapters in this tutorial
3333
const chapters = optionalChaining(() =>
34-
data.pageTitles.edges.map(a => {
34+
data!.pageTitles!.edges!.map(a => {
3535
return {
36-
chapterTitle: optionalChaining(() => a.node.frontmatter.pageTitle),
36+
chapterTitle: optionalChaining(() => a!.node!.frontmatter!.pageTitle),
3737
chapterPath: getTutorialSlug(
3838
optionalChaining(() => a.node.fileAbsolutePath),
3939
),
@@ -42,7 +42,6 @@ const TutorialLayout: React.FunctionComponent<TutorialLayoutProps> = ({
4242
);
4343

4444
const { location } = props;
45-
4645
//all titles of chapters in this tutorial
4746
const chapterTitles = chapters.map(chapter => chapter.chapterTitle);
4847
//the number of this current chapter
@@ -87,7 +86,7 @@ const TutorialLayout: React.FunctionComponent<TutorialLayoutProps> = ({
8786
<a href={nextChapterPath}>
8887
<ChapterMutation
8988
gatsbyID={gatsbyID}
90-
currentChapter={currentChapterIndex + 1}
89+
chapter={currentChapterIndex + 1}
9190
/>
9291
</a>
9392
</Layout>

packages/gatsby-theme/src/components/templates/TutorialOverview.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ interface PageTemplateProps {
1515
}
1616

1717
const PageTemplate: React.FunctionComponent<PageTemplateProps> = ({ data }) => {
18-
let gatsbyID = optionalChaining(() => data.overview.frontmatter.id);
18+
let gatsbyID = optionalChaining(() => data!.overview!.frontmatter!.id);
1919

2020
// This is so that the start button can link to the user's current path
2121
// TO DO find a better way to pass in the which chapter the user is currently on
2222
const chapterPaths = optionalChaining(() =>
23-
data.allMdx.edges.map(a =>
23+
data!.allMdx!.edges.map(a =>
2424
getTutorialSlug(optionalChaining(() => a.node.fileAbsolutePath)),
2525
),
2626
);
@@ -29,11 +29,11 @@ const PageTemplate: React.FunctionComponent<PageTemplateProps> = ({ data }) => {
2929
<Layout>
3030
<Content>
3131
<Flex>
32-
<Box width={3 / 4} aligncontent="center">
32+
<Box width={3 / 4}>
3333
<TutorialHeader
3434
width={1 / 2}
35-
title={data!.overview!.frontmatter!.tutorialTitle}
36-
description={data!.overview!.frontmatter!.description}
35+
title={data!.overview!.frontmatter!.tutorialTitle || 'blank'}
36+
description={data!.overview!.frontmatter!.description || 'blank'}
3737
tags={['React', 'Apollo', 'Javascript']}
3838
/>
3939
</Box>

packages/gatsby-theme/src/components/tutorial/AuthorsProgressBox.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import AuthorList from './AuthorList';
33
import { authors } from '../../utils/sampleData';
44
import ProgressBar from '../shared/ProgressBar';
55
import { getTutorialbyGatsbyID } from '../../utils/queries';
6+
import { GetTutorialbyGatsbyIdQuery } from '../../graphqlTypes';
67
import { Query } from 'react-apollo';
78
import {
89
GithubButton,
@@ -13,20 +14,23 @@ import { Flex, Box } from '../shared/base';
1314
import { optionalChaining, percent } from '../../utils/helpers';
1415

1516
const AuthorsProgressBox = ({ gatsbyID, chapterPaths }) => (
16-
<Query query={getTutorialbyGatsbyID} variables={{ gatsbyID: gatsbyID }}>
17+
<Query<GetTutorialbyGatsbyIdQuery>
18+
query={getTutorialbyGatsbyID}
19+
variables={{ gatsbyID: gatsbyID }}
20+
>
1721
{({ data }) => {
1822
let buttonText = 'Start Tutorial';
1923
let percentage = 0;
2024
let currentChapter =
2125
optionalChaining(
22-
() => data.getTutorialbyGatsbyID.viewerUserTutorial.currentChapter,
26+
() => data!.getTutorialbyGatsbyID!.viewerUserTutorial!.currentChapter,
2327
) || 0;
2428
//This link you to the next chapter you have not done
2529
let currentChapterPath = chapterPaths[0];
2630
if (currentChapter) {
2731
percentage = percent(
28-
data.getTutorialbyGatsbyID.numberofChapters,
29-
data.getTutorialbyGatsbyID.viewerUserTutorial.currentChapter,
32+
data!.getTutorialbyGatsbyID!.numberofChapters,
33+
data!.getTutorialbyGatsbyID!.viewerUserTutorial!.currentChapter,
3034
);
3135
buttonText = 'Continue Tutorial';
3236

packages/gatsby-theme/src/components/tutorial/ChapterMutation.tsx

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
11
import * as React from 'react';
22
import { upsertCurrentChapter } from '../../utils/queries';
3+
import {
4+
UpsertCurrentChapterMutation,
5+
UpsertCurrentChapterMutationVariables,
6+
} from '../../graphqlTypes';
37
import { Mutation } from 'react-apollo';
48
import { loginUser } from '../../utils/auth';
59
import { handleMutationResponse, ApiErrors } from '../../utils/errorHandling';
610
import { Button } from '../shared/base';
711

8-
type ChapterMutationProps = {
9-
gatsbyID: any;
10-
currentChapter: any;
11-
};
12-
13-
const ChapterMutation: React.FunctionComponent<ChapterMutationProps> = ({
14-
gatsbyID,
15-
currentChapter,
16-
}) => (
17-
<Mutation
12+
const ChapterMutation: React.FunctionComponent<
13+
UpsertCurrentChapterMutationVariables
14+
> = ({ gatsbyID, chapter }) => (
15+
<Mutation<UpsertCurrentChapterMutation>
1816
mutation={upsertCurrentChapter}
1917
variables={{
2018
gatsbyID: gatsbyID,
21-
chapter: currentChapter,
19+
chapter: chapter,
2220
}}
2321
>
2422
{currentChapter => {

packages/gatsby-theme/src/components/tutorial/TutorialSidebar.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ import { SpectrumButton } from '../shared/buttons';
66

77
type SidebarProps = {
88
tutorialTitle: string;
9-
chapters: string[];
9+
chapters: Chapter[];
10+
};
11+
type Chapter = {
12+
chapterTitle: string;
13+
chapterPath: string;
1014
};
1115

1216
export const Sidebar: React.FunctionComponent<SidebarProps> = ({
@@ -22,8 +26,8 @@ export const Sidebar: React.FunctionComponent<SidebarProps> = ({
2226
</Card>
2327
<ul>
2428
{chapters.map(chapter => (
25-
<li key={chapter.chapterTitle}>
26-
<a href={chapter.chapterPath}>{chapter.chapterTitle}</a>
29+
<li key={chapter!.chapterTitle}>
30+
<a href={chapter!.chapterPath}>{chapter!.chapterTitle}</a>
2731
</li>
2832
))}
2933
</ul>

0 commit comments

Comments
 (0)