Skip to content

Commit

Permalink
Graphql using apolo
Browse files Browse the repository at this point in the history
  • Loading branch information
nkbtemmy2 committed Feb 26, 2024
1 parent d0a4de8 commit 2f8f1c9
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 2 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
node_modules
.DS_Store
.env
.env
client/yarn.lock
server/yarn.lock
40 changes: 39 additions & 1 deletion client/src/containers/track-card.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,54 @@ import styled from '@emotion/styled';
import { colors, mq } from '../styles';
import { humanReadableTimeFromSeconds } from '../utils/helpers';
import { Link } from 'react-router-dom';
import { gql, useMutation } from '@apollo/client';



/**
* Track Card component renders basic info in a card format
* for each track populating the tracks grid homepage.
*/
const INCREMENT_TRACK_VIEWS = gql`
mutation IncrementTrackViews($incrementTrackViewsId: ID!) {
incrementTrackViews(id: $incrementTrackViewsId) {
code
success
message
track {
id
numberOfViews
}
}
}
`
const TrackCard = ({ track }) => {
const { title, thumbnail, author, length, modulesCount, id } = track;
// const [incrementTrackViews ]= useMutation(INCREMENT_TRACK_VIEWS, {
// variables:{ incrementTrackViewsId: id},
// onCompleted: (data)=>{
// console.log(data);
// }
// })
const [incrementTrackViews, { data, loading, error }] = useMutation(INCREMENT_TRACK_VIEWS, {
variables: { incrementTrackViewsId: id },
onCompleted: (data) => {
console.log(data);
},
onError: (error) => {
console.error("Error incrementing track views:", error);
}
});

if (error) {
console.error("Error found:", error);
}



return (
<CardContainer to={`/track/${id}`}>
<CardContainer to={`/track/${id}`}
onClick={() => incrementTrackViews()}>
<CardContent>
<CardImageContainer>
<CardImage src={thumbnail} alt={title} />
Expand Down
4 changes: 4 additions & 0 deletions server/src/datasources/track-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ class TrackAPI extends RESTDataSource {
getModule(moduleId) {
return this.get(`module/${moduleId}`);
}

incrementTrackViews(trackId){
return this.patch(`track/${trackId}/numberofViews`);
}
}

module.exports = TrackAPI;
21 changes: 21 additions & 0 deletions server/src/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,27 @@ const resolvers = {
return dataSources.trackAPI.getModule(id);
},
},
Mutation: {
// increments a track's numberOfViews property
incrementTrackViews: async (_, { id }, { dataSources }) => {
try {
const track = await dataSources.trackAPI.incrementTrackViews(id);
return {
code: 200,
success: true,
message: `Successfully incremented number of views for track ${id}`,
track,
};
} catch (err) {
return {
code: err.extensions.response.status,
success: false,
message: err.extensions.response.body,
track: null,
};
}
},
},
Track: {
author: ({ authorId }, _, { dataSources }) => {
return dataSources.trackAPI.getAuthor(authorId);
Expand Down
17 changes: 17 additions & 0 deletions server/src/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,21 @@ const typeDefs = gql`
module(id: ID!): Module!
}
type Mutation{
incrementTrackViews(id: ID!): IncrementTrackViewsResponse!
}
type IncrementTrackViewsResponse{
"Similar to HTTP status code, represents the status of the mutation"
code: Int!
"Indicates whether the mutation was successful"
success: Boolean!
"Human-readable message for the UI"
message: String!
"Newly updated track after a successful mutation"
track: Track
}
"A track is a group of Modules that teaches about a specific topic"
type Track {
id: ID!
Expand Down Expand Up @@ -52,6 +67,8 @@ const typeDefs = gql`
"The module's video url, for video-based modules"
videoUrl: String
}
`;

module.exports = typeDefs;

0 comments on commit 2f8f1c9

Please sign in to comment.