Skip to content

Instantly share code, notes, and snippets.

@arunoda
Last active January 7, 2017 19:55
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arunoda/c29128be2c5e979475ec to your computer and use it in GitHub Desktop.
Save arunoda/c29128be2c5e979475ec to your computer and use it in GitHub Desktop.
Schema file with the Posts and Authors
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;
@raphaelsaunier
Copy link

There's a small inconsistency between this and the tutorial, where the posts field is expected to be named posts and not getPosts.

@dalerka
Copy link

dalerka commented Jul 4, 2016

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);

@baldmountain
Copy link

baldmountain commented Nov 8, 2016

Both _.find() and AuthorList.find() work so it isn't wrong, just different. :)

@gpbaculio
Copy link

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