Skip to content

Commit 6742075

Browse files
Merge pull request #28 from howtographql/create-tutorials-mutation
Create tutorials mutation
2 parents b263080 + c5d8a45 commit 6742075

File tree

17 files changed

+1651
-926
lines changed

17 files changed

+1651
-926
lines changed

oss

Submodule oss updated 58 files

packages/gatsby-theme/gatsby-node.js

Lines changed: 63 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
// const componentWithMDXScope = require('gatsby-mdx/component-with-mdx-scope');
22
const {
33
getTutorialSlug,
4-
getTutorialOverviewSlug
5-
} = require("./src/utils/getTutorialSlug.js");
4+
getTutorialOverviewSlug,
5+
} = require('./src/utils/getTutorialSlug.js');
6+
7+
const { createApolloFetch } = require('apollo-fetch');
68

79
exports.createPages = async ({ graphql, actions }) => {
810
const { createPage } = actions;
911
const TutorialLayout = require.resolve(
10-
`./src/components/templates/Tutorial.tsx`
12+
`./src/components/templates/Tutorial.tsx`,
1113
);
1214

1315
const { data } = await graphql(`
@@ -33,30 +35,83 @@ exports.createPages = async ({ graphql, actions }) => {
3335
const tutorialPath = getTutorialSlug(node.fileAbsolutePath);
3436
const overviewPageSlug = getTutorialOverviewSlug(node.fileAbsolutePath);
3537
const overviewTemplate = require.resolve(
36-
"./src/components/templates/TutorialOverview.tsx"
38+
'./src/components/templates/TutorialOverview.tsx',
3739
);
40+
3841
//TODO: find a better way to ID posts & overviews Also a better way to query for them
3942
if (node.frontmatter.tutorialTitle) {
43+
//counts the number of chapters in each tutorial by counting how many files
44+
//there are that contain the overviewpage slug. Subtract one for the overview page.
45+
const numberofChapters =
46+
data.allMdx.edges.filter(chapterNode => {
47+
return chapterNode.node.fileAbsolutePath.includes(
48+
`${overviewPageSlug}/`,
49+
);
50+
}).length - 1;
51+
52+
const variables = {
53+
gatsbyID: node.frontmatter.id,
54+
name: node.frontmatter.tutorialTitle,
55+
numberofChapters: numberofChapters,
56+
};
57+
createPrismaNodes(variables);
58+
4059
return createPage({
4160
path: overviewPageSlug,
4261
component: overviewTemplate,
4362
context: {
4463
id: node.id,
45-
folderRegex: `/(${overviewPageSlug})/`
46-
}
64+
folderRegex: `/(${overviewPageSlug})/`,
65+
},
4766
});
4867
}
4968
createPage({
5069
path: tutorialPath,
5170
component: TutorialLayout, // node.fileAbsolutePath,
5271
context: {
5372
id: node.id,
54-
folderRegex: `/(${overviewPageSlug})/`
55-
}
73+
folderRegex: `/(${overviewPageSlug})/`,
74+
},
5675
});
5776
});
5877
};
5978

79+
const createPrismaNodes = async variables => {
80+
const uri =
81+
'https://howtographql-prod_howtographql-internal.prisma.sh/howtographql/dev';
82+
const apolloFetch = createApolloFetch({ uri });
83+
const query = `
84+
mutation upsertTutorial(
85+
$gatsbyID: String!
86+
$name: String!
87+
$numberofChapters: Int!
88+
) {
89+
upsertTutorial(
90+
where: { gatsbyID: $gatsbyID }
91+
create: {
92+
gatsbyID: $gatsbyID
93+
name: $name
94+
numberofChapters: $numberofChapters
95+
}
96+
update: {
97+
gatsbyID: $gatsbyID
98+
name: $name
99+
numberofChapters: $numberofChapters
100+
}
101+
) {
102+
id
103+
}
104+
}
105+
`;
106+
return apolloFetch({ query, variables })
107+
.then(response => {
108+
console.log(response);
109+
})
110+
.catch(error => {
111+
console.log(error);
112+
});
113+
};
114+
60115
// overviewpages = overviewpages
61116
// .filter(p => p !== overviewPageSlug)
62117
// .concat([overviewPageSlug]);

packages/gatsby-theme/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"@types/react-copy-to-clipboard": "^4.2.6",
4747
"apollo-cache-inmemory": "^1.5.1",
4848
"apollo-client": "^2.5.1",
49+
"apollo-fetch": "^0.7.0",
4950
"apollo-link-context": "^1.0.17",
5051
"apollo-link-http": "^1.5.14",
5152
"babel-plugin-styled-components": "^1.10.0",

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ type FrontMatter = {
2727
const TutorialListing: React.FunctionComponent<TutorialListingProps> = ({
2828
tutorial,
2929
}) => {
30-
const tutorialId = 'cjwb6f2hy7e4f0b14bxh1mar2';
30+
const tutorialId = 'cjwi9iv2klfsb0b14sqc9wpuj';
3131
return (
3232
<Query
3333
query={gql`
@@ -39,7 +39,7 @@ const TutorialListing: React.FunctionComponent<TutorialListingProps> = ({
3939
viewerUserTutorial {
4040
id
4141
upvoted
42-
saved
42+
bookmarked
4343
}
4444
}
4545
}
@@ -97,13 +97,13 @@ const TutorialListing: React.FunctionComponent<TutorialListingProps> = ({
9797
</Mutation>
9898
<Mutation
9999
mutation={gql`
100-
mutation SaveTutorial($id: ID!) {
101-
saveTutorial(tutorialId: $id) {
100+
mutation BookmarkTutorial($id: ID!) {
101+
bookmarkTutorial(tutorialId: $id) {
102102
code
103103
success
104104
userTutorial {
105105
id
106-
saved
106+
bookmarked
107107
}
108108
}
109109
}
@@ -112,12 +112,12 @@ const TutorialListing: React.FunctionComponent<TutorialListingProps> = ({
112112
id: tutorialId,
113113
}}
114114
>
115-
{save => {
115+
{bookmark => {
116116
return (
117117
<Button
118118
onClick={async () => {
119119
const mutationRes = await handleMutationResponse(
120-
save(),
120+
bookmark(),
121121
);
122122
if (mutationRes.error) {
123123
if (mutationRes.error === ApiErrors.AUTHORIZATION) {
@@ -128,7 +128,7 @@ const TutorialListing: React.FunctionComponent<TutorialListingProps> = ({
128128
}
129129
}}
130130
>
131-
Save
131+
Bookmark
132132
</Button>
133133
);
134134
}}

packages/gatsby-theme/src/pages/profile.tsx

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ type User = {
3636
githubHandle: string;
3737
bio: string;
3838
upvoted: [Tutorials];
39-
saved: [Tutorials];
39+
bookmarked: [Tutorials];
4040
};
4141

4242
type Tutorials = {
@@ -69,19 +69,25 @@ const ProfilePage: React.FunctionComponent<ProfileProps> = ({ user }) => {
6969
<button onClick={() => logoutUser()}> Log out </button>
7070
<Heading> Upvoted Tutorials </Heading>
7171
<ul>
72-
{user.upvoted.map(a => (
73-
<li key={a.tutorial.id}>
74-
<span>{a.tutorial.name}</span>
75-
</li>
76-
))}
72+
{user.upvoted.map(
73+
a =>
74+
a.tutorial && (
75+
<li key={a.tutorial.id}>
76+
<span>{a.tutorial.name}</span>
77+
</li>
78+
),
79+
)}
7780
</ul>
78-
<Heading> Saved Tutorials </Heading>
81+
<Heading> Bookmarked Tutorials </Heading>
7982
<ul>
80-
{user.saved.map(a => (
81-
<li key={a.tutorial.id}>
82-
<span>{a.tutorial.name}</span>
83-
</li>
84-
))}
83+
{user.bookmarked.map(
84+
a =>
85+
a.tutorial && (
86+
<li key={a.tutorial.id}>
87+
<span>{a.tutorial.name}</span>
88+
</li>
89+
),
90+
)}
8591
</ul>
8692
</Layout>
8793
);
@@ -103,7 +109,7 @@ const PROFILE_QUERY = gql`
103109
name
104110
}
105111
}
106-
saved: userTutorials(where: { saved: true }) {
112+
bookmarked: userTutorials(where: { bookmarked: true }) {
107113
tutorial {
108114
id
109115
name

0 commit comments

Comments
 (0)