Skip to content

Instantly share code, notes, and snippets.

@RyanCCollins
Last active February 20, 2017 23:09
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 RyanCCollins/fd1e62cb80635ce27fff187c2e8d1cd5 to your computer and use it in GitHub Desktop.
Save RyanCCollins/fd1e62cb80635ce27fff187c2e8d1cd5 to your computer and use it in GitHub Desktop.
// Comment Model Schema and related GraphQL Schema
// From /server/db/models/comment.ts
import mongoose from 'mongoose';
const CommentSchema = new mongoose.Schema({
author: String,
body: String,
post: {
type: String,
ref: 'Post',
required: true,
},
});
export default mongoose.model('Comment', CommentSchema);
// From server/graph/types/comment/comment.ts
import {
GraphQLObjectType,
GraphQLNonNull,
GraphQLString,
GraphQLID,
} from 'graphql';
export default new GraphQLObjectType({
name: 'Comment',
fields: () => ({
author: { type: GraphQLString },
body: { type: GraphQLString },
post: { type: new GraphQLNonNull(GraphQLID) },
}),
});
// Post Model Schema and related GraphQL Schema
// From /server/db/models/post.ts
import mongoose from 'mongoose';
const PostSchema = new mongoose.Schema({
title: {
type: String,
},
image: {
type: String,
},
content: {
type: String,
},
comments: [{ type: String, ref: 'Comment' }],
});
export default mongoose.model('Post', PostSchema);po
// From server/graph/types/post/post.ts
import {
GraphQLObjectType,
GraphQLList,
GraphQLString,
GraphQLID,
} from 'graphql';
import commentType from '../comment/comment';
export default new GraphQLObjectType({
name: 'Post',
fields: () => ({
_id: { type: GraphQLID },
title: { type: GraphQLString },
content: { type: GraphQLString },
comments: { type: new GraphQLList(commentType) },
image: { type: GraphQLString },
}),
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment