index.js 976 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. 'user strict';
  2. const express = require('express');
  3. const graphqlHTTP = require('express-graphql');
  4. const { buildSchema } = require('graphql');
  5. const PORT = process.env.PORT || 3000;
  6. const server = express();
  7. const schema = buildSchema(`
  8. type Video {
  9. id: ID,
  10. title: String,
  11. duration: Int,
  12. watched: Boolean
  13. }
  14. type Query {
  15. video: Video
  16. videos: [Video]
  17. }
  18. type Schema {
  19. query: Query
  20. }
  21. `);
  22. const videoA = {
  23. id: 'a',
  24. title: 'Create a GraphQL schema',
  25. duration: 120,
  26. watched: true
  27. };
  28. const videoB = {
  29. id: 'b',
  30. title: 'Ember.js CLI',
  31. duration: 240,
  32. watched: false
  33. };
  34. const videos = [videoA, videoB];
  35. const resolvers = {
  36. video: () => ({
  37. id: () => '1',
  38. title: () => 'Foo',
  39. duration: () => 180,
  40. watched: () => true
  41. }),
  42. videos: () => videos
  43. };
  44. server.use('/graphql', graphqlHTTP({
  45. schema,
  46. graphiql: true,
  47. rootValue: resolvers
  48. }));
  49. server.listen(PORT, () => {
  50. console.log(`Listening on http://localhost:${PORT}`);
  51. });