Created
July 22, 2019 03:31
-
-
Save deadkff01/4dcef8a7d872e93b3a60ebf7c758c1e7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { GraphQLObjectType, GraphQLList, GraphQLSchema, GraphQLInt, GraphQLString, GraphQLBoolean } = require('graphql') | |
const axios = require('axios') | |
// Launch Type | |
const LaunchType = new GraphQLObjectType({ | |
name: 'Launch', | |
fields: () => ({ | |
flight_number: { type: GraphQLInt }, | |
mission_name: { type: GraphQLString }, | |
launch_year: { type: GraphQLString }, | |
launch_date_local: { type: GraphQLString }, | |
launch_success: { type: GraphQLBoolean }, | |
rocket: { type: RocketType }, | |
}), | |
}) | |
// rocket type | |
const RocketType = new GraphQLObjectType({ | |
name: 'Rocket', | |
fields: () => ({ | |
rocket_id: { type: GraphQLString }, | |
rocket_name: { type: GraphQLString }, | |
rocket_type: { type: GraphQLString }, | |
}), | |
}) | |
const RootQuery = new GraphQLObjectType({ | |
name: 'RootQueryType', | |
fields: { | |
launches: { | |
type: new GraphQLList(LaunchType), | |
async resolve(parent, args) { | |
const request = await axios.get('https://api.spacexdata.com/v3/launches') | |
return request.data | |
}, | |
}, | |
launch: { | |
type: LaunchType, | |
args: { | |
flight_number: { type: GraphQLInt }, | |
}, | |
async resolve(parent, args) { | |
const request = await axios.get(`https://api.spacexdata.com/v3/launches/${args.flight_number}`) | |
return request.data | |
}, | |
}, | |
rockets: { | |
type: new GraphQLList(RocketType), | |
async resolve(parent, args) { | |
const request = await axios.get('https://api.spacexdata.com/v3/rockets') | |
return request.data | |
}, | |
}, | |
rocket: { | |
type: RocketType, | |
args: { | |
id: { type: GraphQLInt }, | |
}, | |
async resolve(parent, args) { | |
const request = await axios.get(`https://api.spacexdata.com/v3/rockets/${args.id}`) | |
return request.data | |
}, | |
}, | |
}, | |
}) | |
module.exports = new GraphQLSchema({ | |
query: RootQuery, | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment