index.js 3.0 KB

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