Repository: https://github.com/maio/graphql-basics-workshop
const graphql = require('graphql');
const storage = require('../storage/storage.js');
const {
GraphQLSchema,
GraphQLObjectType,
GraphQLList,
GraphQLString,
GraphQLNonNull
} = graphql;
const AuthorType = new GraphQLObjectType({
name: 'Author',
fields: () => ({
id: {type: GraphQLString},
name: {type: GraphQLString}
})
});
const BookType = new GraphQLObjectType({
name: 'Book',
fields: () => ({
id: {type: GraphQLString},
name: {type: GraphQLString}
})
});
const Query = new GraphQLObjectType({
name: 'Query',
fields: {
authors: {
type: new GraphQLList(AuthorType),
description: 'Returns list of all authors.',
resolve () {
return storage.getAllAuthors();
}
},
author: {
type: AuthorType,
args: {
id: { type: GraphQLString }
},
resolve (parent, args) {
return storage.getAuthorById(args.id);
}
},
books: {
type: new GraphQLList(BookType),
description: 'Returns list of all books.',
resolve () {
return storage.getAllBooks();
}
},
book: {
type: BookType,
args: {
id: { type: GraphQLString }
},
resolve (parent, args) {
return storage.getBookById(args.id);
}
},
greeting: {
type: GraphQLString,
description: 'Nice greeting.',
args: {
name: {
type: new GraphQLNonNull(GraphQLString),
description: 'Name of the person to greet.'
}
},
resolve (parent, args) {
return 'Hello ' + args.name + '!';
}
},
}
});
module.exports = new GraphQLSchema({
query: Query
});
- More APIs
** Example: GDOM