index.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. 'use strict';
  2. const express = require('express');
  3. const graphqlHTTP = require('express-graphql');
  4. const {
  5. GraphQLBoolean,
  6. GraphQLID,
  7. GraphQLInt,
  8. GraphQLList,
  9. GraphQLNonNull,
  10. GraphQLObjectType,
  11. GraphQLSchema,
  12. GraphQLString,
  13. } = require('graphql');
  14. const {
  15. createVideo,
  16. getVideoById,
  17. getVideos
  18. } = require('./src/data/index');
  19. const PORT = process.env.PORT || 3000;
  20. const server = express();
  21. const videoType = new GraphQLObjectType({
  22. name: 'Video',
  23. description: 'A video on egghead.io',
  24. fields: {
  25. id: {
  26. type: GraphQLID,
  27. description: 'The ID of the video'
  28. },
  29. title: {
  30. type: GraphQLString,
  31. description: 'The title of the video'
  32. },
  33. duration: {
  34. type: GraphQLInt,
  35. description: 'The duration of the video, in seconds',
  36. },
  37. watched: {
  38. type: GraphQLBoolean,
  39. description: 'Whether or not the viewer has watched the video'
  40. }
  41. }
  42. });
  43. const queryType = new GraphQLObjectType({
  44. name: 'QueryType',
  45. description: 'The root query type',
  46. fields: {
  47. video: {
  48. type: videoType,
  49. args: {
  50. id: {
  51. type: new GraphQLNonNull(GraphQLID),
  52. description: 'The ID of the video'
  53. }
  54. },
  55. resolve: (_, args) => {
  56. return getVideoById(args.id);
  57. }
  58. },
  59. videos: {
  60. type: new GraphQLList(videoType),
  61. // General form:
  62. // resolve: () => getVideos()
  63. // Shortcut:
  64. resolve: getVideos
  65. }
  66. }
  67. });
  68. /* Example mutation use:
  69. mutation M {
  70. createVideo(
  71. title: "A new hope",
  72. duration: 7260,
  73. released: true
  74. ) {
  75. id,
  76. title
  77. }
  78. }
  79. */
  80. const mutationType = new GraphQLObjectType({
  81. name: 'Mutation',
  82. description: "The root Mutation type",
  83. fields: {
  84. createVideo: {
  85. type: videoType,
  86. args: {
  87. title: {
  88. type: new GraphQLNonNull(GraphQLString),
  89. description: 'The title of the video'
  90. },
  91. duration: {
  92. type: new GraphQLNonNull(GraphQLInt),
  93. description: 'The duration of the video, in seconds'
  94. },
  95. released: {
  96. type: new GraphQLNonNull(GraphQLBoolean),
  97. description: "Whether or not the video is released"
  98. }
  99. },
  100. resolve: (_, args) => {
  101. return createVideo(args);
  102. }
  103. }
  104. }
  105. });
  106. const schema = new GraphQLSchema({
  107. mutation: mutationType,
  108. query: queryType
  109. // Also available:
  110. // subscription: ...
  111. });
  112. server.use('/graphql', graphqlHTTP({
  113. schema,
  114. graphiql: true
  115. }));
  116. server.listen(PORT, () => {
  117. console.log(`Listening on http://localhost:${PORT}`);
  118. });