index.js 2.8 KB

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