1
+ var express = require ( 'express' ) ;
2
+ var graphqlHTTP = require ( 'express-graphql' ) ;
3
+ var { GraphQLObjectType, GraphQLString, GraphQLInt, GraphQLSchema, GraphQLList } = require ( 'graphql' ) ;
4
+ const fetch = require ( 'node-fetch' ) ;
5
+ var GraphQLJSON = require ( 'graphql-type-json' ) ;
6
+ var cors = require ( 'cors' ) ;
7
+
8
+ //#4 Define the Rocket Object
9
+
10
+ var RocketType = new GraphQLObjectType ( {
11
+ name : 'Rocket' ,
12
+ fields : {
13
+ info :{ type :GraphQLJSON , resolve :res => res }
14
+ }
15
+ } ) ;
16
+
17
+
18
+ //#3 Define the Launch Object
19
+
20
+ var LaunchType = new GraphQLObjectType ( {
21
+ name : 'Launch' ,
22
+ fields : {
23
+ mission_name : { type : GraphQLString , resolve :response => response . mission_name } ,
24
+ flight_number : { type : GraphQLInt , resolve :response => response . flight_number } ,
25
+ rocket_info : { type : RocketType , resolve :response => fetch ( 'https://api.spacexdata.com/v3/rockets/' + response . rocket . rocket_id ) . then ( res => res . json ( ) ) }
26
+ }
27
+ } ) ;
28
+
29
+ //#2 Define the Query Object
30
+
31
+ var Query = new GraphQLObjectType ( {
32
+ name : 'Query' ,
33
+ fields : {
34
+ launch : { type : new GraphQLList ( LaunchType ) ,
35
+ args :{ flight_number :{ type :GraphQLInt } } ,
36
+ resolve :( root , args ) => fetch ( 'https://api.spacexdata.com/v3/launches/' ) . then ( res => res . json ( ) )
37
+ }
38
+ }
39
+ } ) ;
40
+
41
+ //#1 Define the Schema
42
+ var schema = new GraphQLSchema ( {
43
+ query :Query
44
+ } )
45
+
46
+ //#0 Make and express Server with GraphQL middleware.
47
+ var app = express ( ) ;
48
+ app . use ( cors ( ) )
49
+ app . use ( '/graphql' , graphqlHTTP ( {
50
+ schema : schema ,
51
+ graphiql : true ,
52
+ } ) ) ;
53
+ app . listen ( 4000 , ( ) => console . log ( 'Now browse to localhost:4000/graphql' ) ) ;
0 commit comments