Skip to content

Instantly share code, notes, and snippets.

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 chuck0523/8dbcdb2b3b197826006dbf46377899a3 to your computer and use it in GitHub Desktop.
Save chuck0523/8dbcdb2b3b197826006dbf46377899a3 to your computer and use it in GitHub Desktop.
// データ
import PostsList from './data/posts';
import AuthorsList from './data/authors';
// GraphQLのデフォルト型
import {
GraphQLString,
GraphQLList,
GraphQLObjectType,
GraphQLNonNull,
GraphQLSchema
} from 'graphql';
/**
独自型
**/
// Author
const Author = new GraphQLObjectType({
name: "Author",
description: "This represents an author",
fields: () => ({
_id: {type: new GraphQLNonNull(GraphQLString)},
name: {type: GraphQLString}
})
})
// Post type
const Post = new GraphQLObjectType({
name: "Post",
description: "This represents a Post",
fields: () => ({
_id: {type: new GraphQLNonNull(GraphQLString)},
title: {
type: new GraphQLNonNull(GraphQLString),
resolve: function(post) {
return post.title || "Does not exist"
}
},
author: {
type: Author,
resolve: function(post) {
// 受け取ったPostとマッチするAuthorを返す。
return AuthorsList.find(a => a._id == post.author)
}
},
content: {type: GraphQLString}
})
})
// ルートクエリ
const Query = new GraphQLObjectType({
name: 'BlogSchema',
description: 'Root of the Blog Schema',
fields: () => ({
posts: {
type: new GraphQLList(Post),
resolve: function() {
return PostsList
}
}
})
});
// The Schema
const Schema = new GraphQLSchema({
query: Query
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment