Repository: StephenGrider/Lyrical-GraphQL
Branch: master
Commit: b0d53fd8c362
Files: 18
Total size: 8.2 KB
Directory structure:
gitextract_i0e0v96c/
├── .babelrc
├── .gitignore
├── README.md
├── client/
│ ├── index.html
│ ├── index.js
│ └── style/
│ └── style.css
├── index.js
├── package.json
├── server/
│ ├── models/
│ │ ├── index.js
│ │ ├── lyric.js
│ │ └── song.js
│ ├── schema/
│ │ ├── lyric_type.js
│ │ ├── mutations.js
│ │ ├── root_query_type.js
│ │ ├── schema.js
│ │ └── song_type.js
│ └── server.js
└── webpack.config.js
================================================
FILE CONTENTS
================================================
================================================
FILE: .babelrc
================================================
{
"presets": ["env", "react"]
}
================================================
FILE: .gitignore
================================================
node_modules
.DS_STORE
================================================
FILE: README.md
================================================
# Lyrical-GraphQL
Starter project from a GraphQL course on Udemy.com
### Setup
- Run `npm install --legacy-peer-deps` in the root of the project to install dependencies
- Access the application at `localhost:4000` in your browser
================================================
FILE: client/index.html
================================================
================================================
FILE: client/index.js
================================================
import React from 'react';
import ReactDOM from 'react-dom';
const Root = () => {
return Lyrical
};
ReactDOM.render(
,
document.querySelector('#root')
);
================================================
FILE: client/style/style.css
================================================
================================================
FILE: index.js
================================================
const app = require('./server/server');
app.listen(4000, () => {
console.log('Listening');
});
================================================
FILE: package.json
================================================
{
"name": "lyrical",
"version": "1.0.0",
"description": "Starter point for a graphQL course",
"main": "index.js",
"repository": {
"type": "git",
"url": "https://github.com/StephenGrider/Lyrical-GraphQL"
},
"scripts": {
"dev": "nodemon index.js --ignore client"
},
"author": "",
"license": "ISC",
"dependencies": {
"apollo-client": "^0.8.1",
"axios": "^0.15.3",
"babel-core": "^6.22.1",
"babel-loader": "^6.2.10",
"babel-preset-env": "^1.1.8",
"babel-preset-react": "^6.22.0",
"body-parser": "^1.16.0",
"connect-mongo": "^1.3.2",
"css-loader": "^0.26.1",
"express": "^4.14.0",
"express-graphql": "0.6.1",
"express-session": "^1.15.0",
"graphql": "^0.8.2",
"html-webpack-plugin": "^2.26.0",
"lodash": "^4.17.4",
"mongoose": "^7.3.1",
"nodemon": "^2.0.22",
"passport": "^0.3.2",
"passport-local": "^1.0.0",
"react": "^15.4.2",
"react-apollo": "^0.9.0",
"react-dom": "^15.4.2",
"react-router": "^3.0.2",
"style-loader": "^0.13.1",
"webpack": "^2.2.0",
"webpack-dev-middleware": "^1.9.0"
}
}
================================================
FILE: server/models/index.js
================================================
require('./song');
require('./lyric');
================================================
FILE: server/models/lyric.js
================================================
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const LyricSchema = new Schema({
song: {
type: Schema.Types.ObjectId,
ref: 'song'
},
likes: { type: Number, default: 0 },
content: { type: String }
});
LyricSchema.statics.like = function(id) {
const Lyric = mongoose.model('lyric');
return Lyric.findById(id)
.then(lyric => {
++lyric.likes;
return lyric.save();
})
}
mongoose.model('lyric', LyricSchema);
================================================
FILE: server/models/song.js
================================================
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const SongSchema = new Schema({
title: { type: String },
user: {
type: Schema.Types.ObjectId,
ref: 'user'
},
lyrics: [{
type: Schema.Types.ObjectId,
ref: 'lyric'
}]
});
SongSchema.statics.addLyric = function(id, content) {
const Lyric = mongoose.model('lyric');
return this.findById(id)
.then(song => {
const lyric = new Lyric({ content, song })
song.lyrics.push(lyric)
return Promise.all([lyric.save(), song.save()])
.then(([lyric, song]) => song);
});
}
SongSchema.statics.findLyrics = function(id) {
return this.findById(id)
.populate('lyrics')
.then(song => song.lyrics);
}
mongoose.model('song', SongSchema);
================================================
FILE: server/schema/lyric_type.js
================================================
const mongoose = require('mongoose');
const graphql = require('graphql');
const {
GraphQLObjectType,
GraphQLList,
GraphQLID,
GraphQLInt,
GraphQLString
} = graphql;
const Lyric = mongoose.model('lyric');
const LyricType = new GraphQLObjectType({
name: 'LyricType',
fields: () => ({
id: { type: GraphQLID },
likes: { type: GraphQLInt },
content: { type: GraphQLString },
song: {
type: require('./song_type'),
resolve(parentValue) {
return Lyric.findById(parentValue).populate('song')
.then(lyric => {
console.log(lyric)
return lyric.song
});
}
}
})
});
module.exports = LyricType;
================================================
FILE: server/schema/mutations.js
================================================
const graphql = require('graphql');
const { GraphQLObjectType, GraphQLString, GraphQLID } = graphql;
const mongoose = require('mongoose');
const Song = mongoose.model('song');
const Lyric = mongoose.model('lyric');
const SongType = require('./song_type');
const LyricType = require('./lyric_type');
const mutation = new GraphQLObjectType({
name: 'Mutation',
fields: {
addSong: {
type: SongType,
args: {
title: { type: GraphQLString }
},
resolve(parentValue, { title }) {
return new Song({ title }).save();
}
},
addLyricToSong: {
type: SongType,
args: {
content: { type: GraphQLString },
songId: { type: GraphQLID }
},
resolve(parentValue, { content, songId }) {
return Song.addLyric(songId, content);
}
},
likeLyric: {
type: LyricType,
args: { id: { type: GraphQLID } },
resolve(parentValue, { id }) {
return Lyric.like(id);
}
},
deleteSong: {
type: SongType,
args: { id: { type: GraphQLID } },
resolve(parentValue, { id }) {
return Song.findByIdAndRemove(id);
}
}
}
});
module.exports = mutation;
================================================
FILE: server/schema/root_query_type.js
================================================
const mongoose = require('mongoose');
const graphql = require('graphql');
const { GraphQLObjectType, GraphQLList, GraphQLID, GraphQLNonNull } = graphql;
const SongType = require('./song_type');
const LyricType = require('./lyric_type');
const Lyric = mongoose.model('lyric');
const Song = mongoose.model('song');
const RootQuery = new GraphQLObjectType({
name: 'RootQueryType',
fields: () => ({
songs: {
type: new GraphQLList(SongType),
resolve() {
return Song.find({});
}
},
song: {
type: SongType,
args: { id: { type: new GraphQLNonNull(GraphQLID) } },
resolve(parentValue, { id }) {
return Song.findById(id);
}
},
lyric: {
type: LyricType,
args: { id: { type: new GraphQLNonNull(GraphQLID) } },
resolve(parnetValue, { id }) {
return Lyric.findById(id);
}
}
})
});
module.exports = RootQuery;
================================================
FILE: server/schema/schema.js
================================================
const _ = require('lodash');
const graphql = require('graphql');
const { GraphQLSchema } = graphql;
const RootQueryType = require('./root_query_type');
const mutations = require('./mutations');
module.exports = new GraphQLSchema({
query: RootQueryType,
mutation: mutations
});
================================================
FILE: server/schema/song_type.js
================================================
const mongoose = require('mongoose');
const graphql = require('graphql');
const { GraphQLObjectType, GraphQLString, GraphQLID, GraphQLList } = graphql;
const LyricType = require('./lyric_type');
const Song = mongoose.model('song');
const SongType = new GraphQLObjectType({
name: 'SongType',
fields: () => ({
id: { type: GraphQLID },
title: { type: GraphQLString },
lyrics: {
type: new GraphQLList(LyricType),
resolve(parentValue) {
return Song.findLyrics(parentValue.id);
}
}
})
});
module.exports = SongType;
================================================
FILE: server/server.js
================================================
const express = require('express');
const models = require('./models');
const expressGraphQL = require('express-graphql');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const schema = require('./schema/schema');
const app = express();
// Replace with your Mongo Atlas URI
const MONGO_URI = '';
if (!MONGO_URI) {
throw new Error('You must provide a Mongo Atlas URI');
}
mongoose.Promise = global.Promise;
mongoose.connect(MONGO_URI);
mongoose.connection
.once('open', () => console.log('Connected to Mongo Atlas instance.'))
.on('error', (error) =>
console.log('Error connecting to Mongo Atlas:', error)
);
app.use(bodyParser.json());
app.use(
'/graphql',
expressGraphQL({
schema,
graphiql: true
})
);
const webpackMiddleware = require('webpack-dev-middleware');
const webpack = require('webpack');
const webpackConfig = require('../webpack.config.js');
app.use(webpackMiddleware(webpack(webpackConfig)));
module.exports = app;
================================================
FILE: webpack.config.js
================================================
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
entry: './client/index.js',
output: {
path: '/',
filename: 'bundle.js'
},
module: {
rules: [
{
use: 'babel-loader',
test: /\.js$/,
exclude: /node_modules/
},
{
use: ['style-loader', 'css-loader'],
test: /\.css$/
}
]
},
plugins: [
new HtmlWebpackPlugin({
template: 'client/index.html'
})
]
};