Skip to content

Instantly share code, notes, and snippets.

@frikille
Created July 19, 2015 21:11
Show Gist options
  • Save frikille/70789e565951d82d4560 to your computer and use it in GitHub Desktop.
Save frikille/70789e565951d82d4560 to your computer and use it in GitHub Desktop.
GraphQL schema with resolve function implementation and argument
import {
graphql,
GraphQLInt,
GraphQLString,
GraphQLFloat,
GraphQLBoolean,
GraphQLList,
GraphQLNonNull,
GraphQLObjectType,
GraphQLSchema
} from 'graphql';
import PostBlockType from './PostBlockType.js';
import PostLikeType from './PostLikeType.js';
import PostCommentType from './PostCommentType.js';
import JournalType from './JournalType.js';
import UserType from './UserType.js';
let PostType = new GraphQLObjectType({
name: 'Post',
description: 'A Post object',
fields: () => ({
id: {
type: GraphQLInt,
description: 'The id of a post'
},
title: {
type: GraphQLString,
description: 'The title of a post'
},
slug: {
type: GraphQLString,
description: 'The slug of a post'
},
publish_date: {
type: GraphQLString,
description: 'The publish_date of a post'
},
location: {
type: GraphQLString,
description: 'The location of a post'
},
user_id: {
type: GraphQLInt,
description: 'The user_id of a post'
},
journal_id: {
type: GraphQLInt,
description: 'The journal_id of a post'
},
post_state_id: {
type: GraphQLInt,
description: 'The post_state_id of a post'
},
gps_location: {
type: GraphQLString,
description: 'The gps_location of a post'
},
tags: {
type: GraphQLString,
description: 'The tags of a post'
},
blocks: {
type: new GraphQLList(PostBlockType),
description: 'The list of blocks of a post',
resolve: (post) => {
return Post.forge({id: post.id})
.fetch({withRelated: ['blocks']})
.then(p => p.toJSON().blocks);
}
},
likes: {
type: new GraphQLList(PostLikeType),
description: 'The list of likes of a post',
resolve: (post) => {
return Post.forge({id: post.id})
.fetch({withRelated: ['likes']})
.then(post => post.toJSON().likes);
}
},
comments: {
type: new GraphQLList(PostCommentType),
description: 'The list of comments of a post',
resolve: (post) => {
return Post.forge({id: post.id})
.fetch({withRelated: ['comments']})
.then(post => post.toJSON().comments);
}
},
journal: {
type: JournalType,
description: 'The journal of a post',
resolve: (post) => {
return Post.forge({id: post.id})
.fetch({withRelated: ['journal']})
.then(post => post.toJSON().journal);
}
},
author: {
type: UserType,
description: 'The author of a post',
resolve: (post) => {
return Post.forge({id: post.id})
.fetch({withRelated: ['author']})
.then(post => post.toJSON().author);
}
}
})
});
let QueryType = new GraphQLObjectType({
name: 'Query',
fields: () => ({
post: {
type: PostType,
args: {
id: {
name: 'id',
type: new GraphQLNonNull(GraphQLInt)
}
}
resolve: (root, {id}) => {
return Post.forge({id})
.then(post => post && post.toJSON());
}
}
})
});
let Schema = new GraphQLSchema({
query: QueryType
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment