__root 1 |__client 2 |__public 3 |__middleware 4 |__server
let couponSchema = mongoose.Schema({ couponCode: Array, description: String, discountAmount: String, minimumAmount: String, singleUseOnly: Boolean, createdAt: mongoose.Schema.Types.Date, updatedAt: mongoose.Schema.Types.Date, expirationDate: mongoose.Schema.Types.Date });
let couponType = new GraphQLObjectType({ name: 'couponType', description: 'single use coupon', fields: { _id: {type: GraphQLString}, couponCode: {type: new GraphQLList(GraphQLString)}, description: {type: GraphQLString}, discountAmount: {type: GraphQLString}, minimumAmount: {type: GraphQLString}, singleUseOnly: {type: GraphQLBoolean}, createdAt: {type: GraphQLString}, updatedAt: {type: GraphQLString}, expirationDate: {type: GraphQLString} } });
npm i mongoose-schema-to-graphql --save
let configs = { name: 'couponType', // graphQL description: 'Coupon base schema', // graphQL class: 'GraphQLObjectType', // graphQL schema: couponSchema, // Mongoose exclude: ['_id'], // props: { price: {type: GraphQLFloat} } // }
import mongoose from 'mongoose'; let selectObj = { value: String, label: String }; let answerSchema = mongoose.Schema({ createdAt: mongoose.Schema.Types.Date, updatedAt: mongoose.Schema.Types.Date, title: String, answersImage: String, recommended: [selectObj], isPublished: Boolean }); export let questionSchema = mongoose.Schema({ question: String, defRecommended: [selectObj], createdAt: mongoose.Schema.Types.Date, updatedAt: mongoose.Schema.Types.Date, isPublished: Boolean, multipleChoice: Boolean, answers: [answerSchema] });
import MTGQL from 'mongoose-schema-to-graphql'; import {questionSchema} from './dbSchemas'; let config = { name: 'questionType', description: 'Question collection\'s type', class: 'GraphQLObjectType', schema: questionSchema, exclude: ['_id'] }; export let questionType = MTGQL(config);
import { GraphQLObjectType, GraphQLString, GraphQLBoolean, GraphQLList, GraphQLInt } from 'graphql'; let selectType = new GraphQLObjectType({ name: 'selectType', fields: { value: {type: GraphQLString}, label: {type: GraphQLString} } }); let answerType = new GraphQLObjectType({ name: 'answerType', description: 'answer type for question', fields: { title: {type: GraphQLString}, answersImage: {type: GraphQLString}, recommended: {type: new GraphQLList(selectType)}, createdAt: {type: GraphQLString}, updatedAt: {type: GraphQLString}, isPublished: {type: GraphQLBoolean} } }); export let questionType = new GraphQLObjectType({ name: 'questionType', description: 'Question collection\'s type', fields: { question: {type: GraphQLString}, defRecommended: {type: new GraphQLList(selectType)}, createdAt: {type: GraphQLString}, updatedAt: {type: GraphQLString}, isPublished: {type: GraphQLBoolean}, multipleChoice: {type: GraphQLBoolean}, answers: {type: new GraphQLList(answerType)} } });
Source: https://habr.com/ru/post/310820/
All Articles