1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- 'user strict';
- const express = require('express');
- const graphqlHTTP = require('express-graphql');
- const {
- GraphQLSchema,
- GraphQLObjectType,
- GraphQLID,
- GraphQLString,
- GraphQLInt,
- GraphQLBoolean
- } = require('graphql');
- 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,
- resolve: () => new Promise((resolve) => {
- resolve({
- id: 'a',
- title: 'GraphQL',
- duration: 180,
- watched: false
- });
- })
- }
- }
- });
- const schema = new GraphQLSchema({
- query: queryType,
- // Also available:
- // mutation: ...
- // subscription: ...
- });
- const videoA = {
- id: 'a',
- title: 'Create a GraphQL schema',
- duration: 120,
- watched: true
- };
- const videoB = {
- id: 'b',
- title: 'Ember.js CLI',
- duration: 240,
- watched: false
- };
- const videos = [videoA, videoB];
- const resolvers = {
- video: () => ({
- id: () => '1',
- title: () => 'Foo',
- duration: () => 180,
- watched: () => true
- }),
- videos: () => videos
- };
- server.use('/graphql', graphqlHTTP({
- schema,
- graphiql: true
- }));
- server.listen(PORT, () => {
- console.log(`Listening on http://localhost:${PORT}`);
- });
|