index.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. 'use strict';
  2. const express = require('express');
  3. const graphqlHTTP = require('express-graphql');
  4. const {
  5. GraphQLSchema,
  6. GraphQLObjectType,
  7. GraphQLID,
  8. GraphQLString,
  9. GraphQLInt,
  10. GraphQLBoolean
  11. } = require('graphql');
  12. const { getVideoById } = require('./src/data/index');
  13. const PORT = process.env.PORT || 3000;
  14. const server = express();
  15. const videoType = new GraphQLObjectType({
  16. name: 'Video',
  17. description: 'A video on egghead.io',
  18. fields: {
  19. id: {
  20. type: GraphQLID,
  21. description: 'The ID of the video'
  22. },
  23. title: {
  24. type: GraphQLString,
  25. description: 'The title of the video'
  26. },
  27. duration: {
  28. type: GraphQLInt,
  29. description: 'The duration of the video, in seconds',
  30. },
  31. watched: {
  32. type: GraphQLBoolean,
  33. description: 'Whether or not the viewer has watched the video'
  34. }
  35. }
  36. });
  37. const queryType = new GraphQLObjectType({
  38. name: 'QueryType',
  39. description: 'The root query type',
  40. fields: {
  41. video: {
  42. type: videoType,
  43. args: {
  44. id: {
  45. type: GraphQLID,
  46. description: 'The ID of the video'
  47. }
  48. },
  49. resolve: (_, args) => {
  50. return getVideoById(args.id);
  51. }
  52. }
  53. }
  54. });
  55. const schema = new GraphQLSchema({
  56. query: queryType
  57. // Also available:
  58. // mutation: ...
  59. // subscription: ...
  60. });
  61. server.use('/graphql', graphqlHTTP({
  62. schema,
  63. graphiql: true
  64. }));
  65. server.listen(PORT, () => {
  66. console.log(`Listening on http://localhost:${PORT}`);
  67. });