'use strict'; const express = require('express'); const graphqlHTTP = require('express-graphql'); const { GraphQLBoolean, GraphQLID, GraphQLInt, GraphQLList, GraphQLNonNull, GraphQLObjectType, GraphQLSchema, GraphQLString, } = require('graphql'); const { createVideo, 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 } } }); /* Example mutation use: mutation M { createVideo( title: "A new hope", duration: 7260, released: true ) { id, title } } */ const mutationType = new GraphQLObjectType({ name: 'Mutation', description: "The root Mutation type", fields: { createVideo: { type: videoType, args: { title: { type: new GraphQLNonNull(GraphQLString), description: 'The title of the video' }, duration: { type: new GraphQLNonNull(GraphQLInt), description: 'The duration of the video, in seconds' }, released: { type: new GraphQLNonNull(GraphQLBoolean), description: "Whether or not the video is released" } }, resolve: (_, args) => { return createVideo(args); } } } }); const schema = new GraphQLSchema({ mutation: mutationType, query: queryType // Also available: // subscription: ... }); server.use('/graphql', graphqlHTTP({ schema, graphiql: true })); server.listen(PORT, () => { console.log(`Listening on http://localhost:${PORT}`); });