Skip to content

Instantly share code, notes, and snippets.

@lswest
Created December 7, 2019 12:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lswest/d2118f4fa0225b80993acb7337fdefc2 to your computer and use it in GitHub Desktop.
Save lswest/d2118f4fa0225b80993acb7337fdefc2 to your computer and use it in GitHub Desktop.
FCM 152 GraphQL
import mongoose from 'mongoose'
const goSchema = new mongoose.Schema(
{
_id: {
type: mongoose.Schema.Types.ObjectId,
},
Title: {
type: String,
required: true,
},
PlayedDate: {
type: String,
required: true,
},
Server: {
type: String,
},
Black: {
type: String,
},
White: {
type: String,
},
Komi: {
type: String,
},
Result: {
type: String,
},
MyWin: {
type: Boolean,
},
SGF: {
type: String,
},
},
{collection: 'goGames'},
)
const goGame = mongoose.model('goGame', goSchema)
export default goGame
import mongoose from 'mongoose'
export default {
Query: {
goGame: async (parent, args, {models: {goGameModel}}, info) => {
const goGame = await goGameModel.findOne(args).exec()
return goGame
},
allGoGames: async (parent, args, {models: {goGameModel}}, info) => {
let allGoGames
if (args.Limit != null) {
let limitedArgs = Object.assign({}, args)
delete limitedArgs.Limit
allGoGames = await goGameModel
.find(limitedArgs)
.limit(args.Limit)
.exec()
} else allGoGames = await goGameModel.find(args).exec()
return allGoGames
},
},
Mutation: {
createGoGame: async (parent, args, {models: {goGameModel}}, info) => {
args._id = mongoose.Types.ObjectId()
const goGame = await goGameModel.create(args)
return goGame
},
},
}
import goGameResolver from './goGames'
export default [
goGameResolver,
]
import {gql} from 'apollo-server'
export default gql`
type GoGame {
_id: String
Title: String
PlayedDate: String
Server: String
Black: String
White: String
Komi: String
Result: String
MyWin: Boolean
SGF: String
}
extend type Query {
goGame(Title: String, _id: String): GoGame
allGoGames(
Title: String
PlayedDate: String
Server: String
Black: String
White: String
MyWin: Boolean
_id: String
Limit: Int
): [GoGame]
}
extend type Mutation {
createGoGame(
Title: String
PlayedDate: String
Server: String
Black: String
White: String
Komi: String
Result: String
MyWin: Boolean
SGF: String
): GoGame
}
`
import goGameSchema from './goGames'
import {gql} from 'apollo-server'
const linkSchema = gql`
type Query {
_: Boolean
}
type Mutation {
_: Boolean
}
`
export default [
goGameSchema,
]
import express from 'express'
import mongoose from 'mongoose'
import {ApolloServer} from 'apollo-server-express'
import schemas from './schemas'
import resolvers from './resolvers'
import goGameModel from './models/goGames'
const app = express()
const server = new ApolloServer({
typeDefs: schemas,
resolvers,
context: async ({req}) => {
if (req) {
return {
models: {
goGameModel,
},
}
}
},
})
server.applyMiddleware({app, path: '/graphql'})
app.listen(5000, () => {
mongoose.connect('{MONGO_URL}', {
useNewUrlParser: true,
useUnifiedTopology: true,
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment