Skip to content

Instantly share code, notes, and snippets.

@byurhannurula
Created April 28, 2019 14:31
Show Gist options
  • Save byurhannurula/ef6301c06cb350f8156136e3aad7c963 to your computer and use it in GitHub Desktop.
Save byurhannurula/ef6301c06cb350f8156136e3aad7c963 to your computer and use it in GitHub Desktop.
import mongoose, { Schema } from 'mongoose'
const userSchema = new Schema({
name: String,
bio: String,
email: String,
password: String,
googleId: String,
githubId: String,
avatar: String,
sessions: [{
type: Schema.Types.ObjectId,
ref: 'Session',
}],
socialLinks: [{
type: String,
}],
},
{
timestamps: true,
},
)
const sessionSchema = new Schema({
name: String,
cardSet: String,
createdBy: {
type: Schema.Types.ObjectId,
ref: 'User',
},
polls: [{
type: Schema.Types.ObjectId,
ref: 'Poll',
}],
members: [{
type: Schema.Types.ObjectId,
ref: 'User',
}],
},
{
timestamps: true,
},
)
const User = mongoose.model('User', userSchema)
const Session = mongoose.model('Session', sessionSchema)
export default [User, Session]
import { isAuthenticated } from '../utils'
export default {
Query: {
// Users
me: (parent, args, { req, models }, info) => {},
getUser: (parent, { id }, { models }, info) => {},
getUsers: async (parent, args, { req, models }, info) => {},
// Sessions
getSession: (parent, { id }, { req, models }, info) => {
isAuthenticated(req)
return models.Session.findById(id)
},
getSessions: async (parent, args, { req, models }, info) => {
isAuthenticated(req)
const sessions = await models.Session.find({}).populate({
path: 'createdBy',
model: 'User',
})
return sessions
},
},
Mutation: {
signUp: async (parent, args, { req, models }, info) => {},
signIn: async (parent, args, { req, models }, info) => {},
signOut: (parent, args, { req, res }, info) => {},
// Sessions
startSession: async (parent, { name, cardSet }, { req, models }, info) => {
isAuthenticated(req)
const session = await new models.Session({
name,
cardSet,
createdBy: req.session.userId,
}).save()
return session
},
},
}
import { gql } from 'apollo-server-express'
const schema = gql`
type Query {
getSession(id: ID!): Session
getSessions: [Session]
}
type Mutation {
startSession(name: String!, cardSet: String): Session!
}
type User {
id: ID!
name: String!
bio: String
email: String!
avatar: String
password: String!
socialLinks: String
sessions: [Session!]
createdAt: String!
updatedAt: String!
}
type Session {
id: ID!
name: String!
cardSet: String!
createdBy: User!
polls: [Poll]
members: [User]
createdAt: String!
updatedAt: String!
}
`
export default schema
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment