-
-
Save arunoda/c29128be2c5e979475ec to your computer and use it in GitHub Desktop.
Schema file with the Posts and Authors
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 * as _ from 'underscore'; | |
// This is the Dataset in our blog | |
import PostsList from './data/posts'; | |
import AuthorsList from './data/authors'; | |
import {CommentList, ReplyList} from './data/comments'; | |
import { | |
// These are the basic GraphQL types | |
GraphQLInt, | |
GraphQLFloat, | |
GraphQLString, | |
GraphQLList, | |
GraphQLObjectType, | |
GraphQLEnumType, | |
// This is used to create required fileds and arguments | |
GraphQLNonNull, | |
// This is the class we need to create the schema | |
GraphQLSchema, | |
} from 'graphql'; | |
/** | |
DEFINE YOUR TYPES BELOW | |
**/ | |
const Author = new GraphQLObjectType({ | |
name: "Author", | |
description: "This represent an author", | |
fields: () => ({ | |
_id: {type: new GraphQLNonNull(GraphQLString)}, | |
name: {type: GraphQLString} | |
}) | |
}); | |
const Post = new GraphQLObjectType({ | |
name: "Post", | |
description: "This represent a Post", | |
fields: () => ({ | |
_id: {type: new GraphQLNonNull(GraphQLString)}, | |
title: { | |
type: new GraphQLNonNull(GraphQLString), | |
resolve: function(post) { | |
return post.title || "Does not exists"; | |
} | |
}, | |
content: {type: GraphQLString}, | |
author: { | |
type: Author, | |
resolve: function(post) { | |
return _.find(AuthorsList, a => a._id == post.author); | |
} | |
} | |
}) | |
}); | |
// This is the Root Query | |
const Query = new GraphQLObjectType({ | |
name: 'BlogSchema', | |
description: "Root of the Blog Schema", | |
fields: () => ({ | |
posts: { | |
type: new GraphQLList(Post), | |
resolve: function() { | |
return PostsList | |
} | |
}, | |
echo: { | |
type: GraphQLString, | |
description: "Echo what you enter", | |
args: { | |
message: {type: GraphQLString} | |
}, | |
resolve: function(source, {message}) { | |
return {aa: 10}; | |
} | |
} | |
}) | |
}); | |
// This the Schema | |
const Schema = new GraphQLSchema({ | |
query: Query | |
}); | |
export default Schema; |
Also, the line 52
return _.find(AuthorsList, a => a._id == post.author);
is wrong in the tutorial:
return AuthorsList.find(a => a._id == post.author);
Both _.find() and AuthorList.find() work so it isn't wrong, just different. :)
If they both work I would not have error :/
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There's a small inconsistency between this and the tutorial, where the posts field is expected to be named
posts
and notgetPosts
.