123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- 'use strict';
- const express = require('express');
- const graphqlHTTP = require('express-graphql');
- const {
- GraphQLBoolean,
- GraphQLID,
- GraphQLInt,
- GraphQLList,
- GraphQLNonNull,
- GraphQLObjectType,
- GraphQLSchema,
- GraphQLString,
- } = require('graphql');
- const {
- getVideoById,
- getVideos
- } = require('./src/data/index');
- const PORT = process.env.PORT || 3000;
- const server = express();
- const videoType = new GraphQLObjectType({
- name: 'Video',
- description: 'A video on egghead.io',
- fields: {
- id: {
- type: GraphQLID,
- description: 'The ID of the video'
- },
- title: {
- type: GraphQLString,
- description: 'The title of the video'
- },
- duration: {
- type: GraphQLInt,
- description: 'The duration of the video, in seconds',
- },
- watched: {
- type: GraphQLBoolean,
- description: 'Whether or not the viewer has watched the video'
- }
- }
- });
- const queryType = new GraphQLObjectType({
- name: 'QueryType',
- description: 'The root query type',
- fields: {
- video: {
- type: videoType,
- args: {
- id: {
- type: new GraphQLNonNull(GraphQLID),
- description: 'The ID of the video'
- }
- },
- resolve: (_, args) => {
- return getVideoById(args.id);
- }
- },
- videos: {
- type: new GraphQLList(videoType),
- // General form:
- // resolve: () => getVideos()
- // Shortcut:
- resolve: getVideos
- }
- }
- });
- const schema = new GraphQLSchema({
- query: queryType
- // Also available:
- // mutation: ...
- // subscription: ...
- });
- server.use('/graphql', graphqlHTTP({
- schema,
- graphiql: true
- }));
- server.listen(PORT, () => {
- console.log(`Listening on http://localhost:${PORT}`);
- });
|