Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let books = [
- { name: 'Name of the Wind', genre: 'Horror', id: '1', authorID: '3' },
- { name: 'The Final Empire', genre: 'Fantasy', id: '2', authorID: '1' },
- { name: 'The Long Earth', genre: 'Sci-Fi', id: '3', authorID: '2' },
- ];
- const RootQuery = new GraphQLObjectType({
- name: 'RootQueryType',
- fields: {
- book: {
- type: BookType,
- args: { id: { type: GraphQLString } },
- //this forEach is not working
- resolve(parent, args){
- books.forEach( function(book) {
- if(book.id == args.id) {
- console.log(book);
- return book;
- }
- });
- }
- }
- }
- });
- request:
- {
- book(id: "2") {
- name
- genre
- }
- }
- response:
- {
- "data": {
- "book": null
- }
- }
- return books.find(function(book) {
- return book.id == args.id;
- });
- let books = [
- { name: 'Name of the Wind', genre: 'Horror', id: '1', authorID: '3' },
- { name: 'The Final Empire', genre: 'Fantasy', id: '2', authorID: '1' },
- { name: 'The Long Earth', genre: 'Sci-Fi', id: '3', authorID: '2' },
- ];
- let bookIds = new Set(books.map(({id}) => id));
- return bookIds.has(args.id);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement