index.js 865 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. 'user strict';
  2. const { graphql, buildSchema } = require('graphql');
  3. const schema = buildSchema(`
  4. type Video {
  5. id: ID,
  6. title: String,
  7. duration: Int,
  8. watched: Boolean
  9. }
  10. type Query {
  11. video: Video
  12. videos: [Video]
  13. }
  14. type Schema {
  15. query: Query
  16. }
  17. `);
  18. const videoA = {
  19. id: 'a',
  20. title: 'Create a GraphQL schema',
  21. duration: 120,
  22. watched: true
  23. };
  24. const videoB = {
  25. id: 'b',
  26. title: 'Ember.js CLI',
  27. duration: 240,
  28. watched: false
  29. };
  30. const videos = [videoA, videoB];
  31. const resolvers = {
  32. video: () => ({
  33. id: () => '1',
  34. title: () => 'Foo',
  35. duration: () => 180,
  36. watched: () => true
  37. }),
  38. videos: () => videos
  39. };
  40. const query = `
  41. query myFirstQuery {
  42. videos {
  43. id,
  44. title,
  45. duration,
  46. watched
  47. }
  48. }
  49. `;
  50. graphql(schema, query, resolvers)
  51. .then((result) => console.log(result))
  52. .catch((error) => console.log(error));