Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. let books = [
  2. { name: 'Name of the Wind', genre: 'Horror', id: '1', authorID: '3' },
  3. { name: 'The Final Empire', genre: 'Fantasy', id: '2', authorID: '1' },
  4. { name: 'The Long Earth', genre: 'Sci-Fi', id: '3', authorID: '2' },
  5. ];
  6. const RootQuery = new GraphQLObjectType({
  7. name: 'RootQueryType',
  8. fields: {
  9. book: {
  10. type: BookType,
  11. args: { id: { type: GraphQLString } },
  12. //this forEach is not working
  13. resolve(parent, args){
  14. books.forEach( function(book) {
  15. if(book.id == args.id) {
  16. console.log(book);
  17. return book;
  18. }
  19. });
  20. }
  21. }
  22. }
  23. });
  24.  
  25. request:
  26. {
  27. book(id: "2") {
  28. name
  29. genre
  30. }
  31. }
  32. response:
  33. {
  34. "data": {
  35. "book": null
  36. }
  37. }
  38.  
  39. return books.find(function(book) {
  40. return book.id == args.id;
  41. });
  42.  
  43. let books = [
  44. { name: 'Name of the Wind', genre: 'Horror', id: '1', authorID: '3' },
  45. { name: 'The Final Empire', genre: 'Fantasy', id: '2', authorID: '1' },
  46. { name: 'The Long Earth', genre: 'Sci-Fi', id: '3', authorID: '2' },
  47. ];
  48. let bookIds = new Set(books.map(({id}) => id));
  49.  
  50. return bookIds.has(args.id);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement