Skip to content

Commit 3770481

Browse files
committed
💻 code
1 parent 876204e commit 3770481

File tree

13 files changed

+233
-11
lines changed

13 files changed

+233
-11
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const firebase = require('../firebase');
2+
3+
const DB = firebase.database();
4+
5+
const createVideo = ({ id, title, duration, released }) => {
6+
return new Promise((resolve, reject) => {
7+
const ref = DB.ref('videos');
8+
const video = { id, title, duration, released };
9+
10+
ref.child(id).set({
11+
title,
12+
duration,
13+
released
14+
}, error => {
15+
if (error) {
16+
reject(new Error('Data could not be saved.'))
17+
} else {
18+
resolve(video);
19+
}
20+
});
21+
});
22+
}
23+
24+
module.exports = createVideo;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const firebase = require('../firebase');
2+
const DB = firebase.database();
3+
4+
const getVideoById = id => new Promise((resolve, reject) => {
5+
var ref = DB.ref(`videos`);
6+
7+
ref.child(id).once('value', data => data.exists()
8+
? resolve(Object.assign({}, data.val(), { id: data.key }))
9+
: reject(new Error(`Video with ID '${id}' doesnt found`))
10+
);
11+
});
12+
13+
module.exports = getVideoById;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const firebase = require('../firebase');
2+
const toArray = require('./utils');
3+
4+
const DB = firebase.database();
5+
6+
const getVideos = args => new Promise(resolve => {
7+
const ref = DB.ref('videos');
8+
9+
ref.once('value', data => {
10+
const videos = toArray(data);
11+
12+
if (args.watched !== undefined) {
13+
resolve(videos.filter(video => video.watched === args.watched));
14+
} else {
15+
resolve(videos);
16+
}
17+
});
18+
});
19+
20+
module.exports = getVideos;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const isObject = obj => {
2+
return Object.prototype.toString(obj) === '[object Object]' ? true : false;
3+
};
4+
5+
const toArray = snapshot => {
6+
let arr = [];
7+
snapshot.forEach(childSnapshot => {
8+
let val = childSnapshot.val();
9+
if(isObject(val)){
10+
val.id = childSnapshot.key;
11+
}
12+
arr.push(val);
13+
});
14+
return arr;
15+
};
16+
17+
module.exports = toArray;

functions/graphql/firebase.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const functions = require('firebase-functions');
2+
const admin = require('firebase-admin');
3+
4+
module.exports = admin.initializeApp(functions.config().firebase);

functions/graphql/index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const functions = require('firebase-functions');
2+
const cors = require('cors')({ origin: true });
3+
4+
const { graphql } = require('graphql');
5+
6+
const Schema = require('./schema');
7+
8+
module.exports = functions.https.onRequest((req, res) => cors(req, res, () => {
9+
if (req.method === 'POST') {
10+
return graphql(Schema, req.body.query, null, {}, req.body.variables)
11+
.then(result => {
12+
res.send(result);
13+
})
14+
.catch(err => res.send(new Error('hubo un error madafaker')))
15+
} else {
16+
return graphql(Schema, req.body.query, null, {}, req.body.variables)
17+
.then(result => res.send(result))
18+
.catch(err => res.send(new Error('hubo un error madafaker')));
19+
}
20+
}));

functions/graphql/schema.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const { GraphQLSchema } = require( 'graphql');
2+
3+
const queryType = require('./types/query');
4+
const mutationType = require('./types/mutation');
5+
6+
const Schema = new GraphQLSchema({
7+
query: queryType,
8+
mutation: mutationType
9+
});
10+
11+
module.exports = Schema;
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const {
2+
GraphQLObjectType,
3+
GraphQLString,
4+
GraphQLInt,
5+
GraphQLBoolean,
6+
GraphQLNonNull
7+
} = require('graphql');
8+
9+
const createVideo = require('../database/create-video');
10+
11+
const videoType = require('./video');
12+
13+
const MutationType = new GraphQLObjectType({
14+
name: 'mutation',
15+
description: 'The root mutation type',
16+
fields: {
17+
createVideo: {
18+
type: videoType,
19+
args: {
20+
id: {
21+
type: new GraphQLNonNull(GraphQLString),
22+
description: 'the ID of the video'
23+
},
24+
title: {
25+
type: new GraphQLNonNull(GraphQLString),
26+
description: 'the title of the video'
27+
},
28+
duration: {
29+
type: new GraphQLNonNull(GraphQLInt),
30+
description: 'the duration of the video'
31+
},
32+
released: {
33+
type: new GraphQLNonNull(GraphQLBoolean),
34+
description: 'the ID of the video'
35+
}
36+
},
37+
resolve: (_, args) => createVideo(args)
38+
}
39+
}
40+
});
41+
42+
module.exports = MutationType;

functions/graphql/types/query.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const {
2+
GraphQLObjectType,
3+
GraphQLID,
4+
GraphQLBoolean,
5+
GraphQLNonNull,
6+
GraphQLList
7+
} = require('graphql');
8+
9+
const videoType = require('./video');
10+
11+
const getVideo= require('../database/get-video');
12+
const getVideos = require('../database/get-videos');
13+
14+
const QueryType = new GraphQLObjectType({
15+
name: 'QueryType',
16+
description: 'The root query type',
17+
fields: {
18+
videos: {
19+
type: new GraphQLList(videoType),
20+
args: {
21+
watched: {
22+
type: GraphQLBoolean,
23+
description: 'if the video has been watched'
24+
}
25+
},
26+
resolve: (_, args) => getVideos(args)
27+
},
28+
video: {
29+
type: videoType,
30+
args: {
31+
id: {
32+
type: new GraphQLNonNull(GraphQLID),
33+
description: 'the ID of the video'
34+
}
35+
},
36+
resolve: (_, args) => getVideo(args.id)
37+
}
38+
}
39+
});
40+
41+
module.exports = QueryType;

functions/graphql/types/video.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const {
2+
GraphQLObjectType,
3+
GraphQLID,
4+
GraphQLString,
5+
GraphQLInt,
6+
GraphQLBoolean
7+
} = require('graphql');
8+
9+
const videoType = new GraphQLObjectType({
10+
name: 'Video',
11+
description: 'A video type',
12+
fields: {
13+
id: {
14+
type: GraphQLID,
15+
description: 'the ID of the video'
16+
},
17+
title: {
18+
type: GraphQLString,
19+
description: 'the title of the video'
20+
},
21+
duration: {
22+
type: GraphQLInt,
23+
description: 'the duration of video, in seconds'
24+
},
25+
watched: {
26+
type: GraphQLBoolean,
27+
description: 'if video has been watched'
28+
},
29+
}
30+
});
31+
32+
module.exports = videoType;

0 commit comments

Comments
 (0)