-
-
Save lswest/d2118f4fa0225b80993acb7337fdefc2 to your computer and use it in GitHub Desktop.
FCM 152 GraphQL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
}, | |
}, | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import goGameResolver from './goGames' | |
export default [ | |
goGameResolver, | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import goGameSchema from './goGames' | |
import {gql} from 'apollo-server' | |
const linkSchema = gql` | |
type Query { | |
_: Boolean | |
} | |
type Mutation { | |
_: Boolean | |
} | |
` | |
export default [ | |
goGameSchema, | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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