Created
March 28, 2017 23:56
-
-
Save sarkistlt/09b6ea043f27faad4cec99b6c1a5fb34 to your computer and use it in GitHub Desktop.
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
/** | |
* MONGOSE SCHEMA | |
* [questions-schema.js] | |
*/ | |
import mongoose from 'mongoose'; | |
const selectSchema = { | |
value: String, | |
label: String | |
}; | |
const answerSchema = mongoose.Schema({ | |
createdAt: mongoose.Schema.Types.Date, | |
updatedAt: mongoose.Schema.Types.Date, | |
answerId: String, | |
title: String, | |
displayOrder: Number, | |
answersImage: String, | |
selectText: String, | |
notSelectText: String, | |
positiveTip: String, | |
negativeTip: String, | |
recommended: [selectSchema], | |
exclude: [selectSchema], | |
isPublished: Boolean | |
}); | |
export const questionSchema = mongoose.Schema({ | |
mongooseId: mongoose.Schema.Types.ObjectId, | |
simpleId: String, | |
question: String, | |
defRecommended: [selectSchema], | |
createdAt: mongoose.Schema.Types.Date, | |
updatedAt: mongoose.Schema.Types.Date, | |
isPublished: Boolean, | |
multipleChoice: Boolean, | |
noneOfThese: Boolean, | |
answers: [answerSchema] | |
}); | |
export const Questions = mongoose.model('questions', questionSchema); | |
//////////////////////////////////////////////////////////////////////////////////////////////////// | |
/** | |
* GRAPHQL TYPE | |
* [questions-type.js] | |
*/ | |
import {GraphQLString} from 'graphql'; | |
import MTGQL from 'mongoose-schema-to-graphql'; | |
import { questionSchema } from './questions-schema'; | |
let queryConfig = { | |
name: 'questionType', | |
description: 'Question schema', | |
class: 'GraphQLObjectType', | |
schema: questionSchema, | |
exclude: ['mongooseId'] | |
}; | |
let mutationConfig = { | |
name: 'questionMutationType', | |
description: 'Question Mutation schema', | |
class: 'GraphQLObjectType', | |
schema: questionSchema, | |
exclude: ['mongooseId'], | |
props: { | |
defRecommended: {type: GraphQLString}, | |
answers: {type: GraphQLString} | |
} | |
}; | |
export const questionType = MTGQL(queryConfig); | |
//////////////////////////////////////////////////////////////////////////////////////////////////// | |
/** | |
* GRAPHQL QUERY OBJECT | |
* [schema-queries.js] | |
*/ | |
import { questionType } from './questions-type'; | |
import { Questions } from './questions-schema'; | |
let queries = new GraphQLObjectType({ | |
name: 'Query', | |
fields: () => ({ | |
getQuestions: { | |
type: new GraphQLList(questionType), | |
args: { | |
_id: { | |
type: GraphQLString | |
}, | |
simpleId: { | |
type: GraphQLString | |
}, | |
isPublished: { | |
type: GraphQLBoolean | |
}, | |
answerId: { | |
type: GraphQLString | |
} | |
}, | |
resolve: (_, args) => Questions.find(args) | |
}, | |
}) | |
}); | |
export default queries; | |
//////////////////////////////////////////////////////////////////////////////////////////////////// | |
/** | |
* GRAPHQL SCHEMA | |
* [schema.js] | |
*/ | |
import { GraphQLSchema } from 'graphql/type'; | |
import queries from './schema-queries'; | |
import mutations from './schema-mutations'; | |
const schema = new GraphQLSchema({ | |
query, | |
mutation, | |
}); | |
export default schema; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment