Skip to content

Instantly share code, notes, and snippets.

@simenbrekken
Created June 12, 2016 06:39
Show Gist options
  • Save simenbrekken/ec51bed6a044fad56bb7ebbb4cf811e0 to your computer and use it in GitHub Desktop.
Save simenbrekken/ec51bed6a044fad56bb7ebbb4cf811e0 to your computer and use it in GitHub Desktop.
Flight schema
/* eslint-disable no-use-before-define */
import {
GraphQLBoolean,
GraphQLID,
GraphQLInt,
GraphQLInterfaceType,
GraphQLList,
GraphQLNonNull,
GraphQLObjectType,
GraphQLSchema,
GraphQLString,
} from 'graphql'
export default ({ resolvers, mocks }) => {
const createResolver = type => {
const resolver = resolvers[type] || mocks[type]
if (!resolver) {
throw new Error(`No resolver or mock found for type '${type}'`)
}
return resolver
}
const IATA = GraphQLString // TODO: new GraphQLScalarType
const ISODate = GraphQLString
const FlightStatus = GraphQLString
const FlightID = new GraphQLObjectType({
name: 'FlightID',
fields: {
flightId: { type: GraphQLString },
airlineIata: { type: IATA },
airlineName: { type: GraphQLString },
},
})
const StatusFields = {
status: { type: GraphQLString },
statusDescription: { type: GraphQLString },
}
const Status = new GraphQLInterfaceType({
name: 'Status',
fields: StatusFields,
resolveType: value => ('belt' in value ? BeltStatus : GateStatus),
})
const BeltStatus = new GraphQLObjectType({
name: 'BeltStatus',
interfaces: [Status],
fields: {
...StatusFields,
belt: { type: GraphQLString },
start: { type: GraphQLString },
startLocal: { type: GraphQLString },
stop: { type: GraphQLString },
stopLocal: { type: GraphQLString },
},
})
const GateStatus = new GraphQLObjectType({
name: 'GateStatus',
interfaces: [Status],
fields: {
...StatusFields,
gate: { type: GraphQLString },
},
})
const AirportFlightStatusFields = {
airportIata: { type: IATA },
airportName: { type: GraphQLString },
statusCode: { type: GraphQLString },
statusCodeDescription: { type: GraphQLString },
statusTime: { type: GraphQLString },
statusTimeLocal: { type: GraphQLString },
isDelayed: { type: GraphQLBoolean },
}
const AirportFlightStatus = new GraphQLInterfaceType({
name: 'AirportFlightStatus',
fields: AirportFlightStatusFields,
resolveType: value => ('flightArrivalDate' in value ? ArrivalStatus : DepartureStatus),
})
const ArrivalStatus = new GraphQLObjectType({
name: 'ArrivalStatus',
interfaces: [AirportFlightStatus],
fields: {
...AirportFlightStatusFields,
id: {
type: GraphQLID,
resolve: arrival => arrival.airportIata + arrival.flightArrivalDate,
},
belt: { type: BeltStatus },
flightArrivalDate: { type: ISODate },
flightArrivalDateLocal: { type: GraphQLString },
},
})
const DepartureStatus = new GraphQLObjectType({
name: 'DepartureStatus',
interfaces: [AirportFlightStatus],
fields: {
...AirportFlightStatusFields,
id: {
type: GraphQLID,
resolve: departure => departure.airportIata + departure.flightDepartureDate,
},
gate: { type: GateStatus },
checkInZones: { type: new GraphQLList(GraphQLString) },
flightDepartureDate: { type: ISODate },
flightDepartureDateLocal: { type: GraphQLString },
},
})
const Flight = new GraphQLObjectType({
name: 'Flight',
fields: {
id: { type: GraphQLID },
cursor: {
type: GraphQLString,
resolve: flight => flight.id,
},
flightIds: { type: new GraphQLList(FlightID) },
departure: { type: DepartureStatus },
arrival: { type: ArrivalStatus },
flightStatus: { type: FlightStatus },
flightStatusTime: { type: GraphQLString },
flightStatusTimeLocal: { type: GraphQLString },
isDelayed: { type: GraphQLBoolean },
isDomestic: { type: GraphQLBoolean },
},
})
const PaginationFields = {
before: { type: GraphQLString },
after: { type: GraphQLString },
first: { type: GraphQLInt },
last: { type: GraphQLInt },
}
const Query = new GraphQLObjectType({ // TODO: const Query = createType('Query')
name: 'Query',
fields: {
flight: {
type: Flight,
args: {
id: { type: GraphQLID },
},
resolve: createResolver('Flight'), // TODO: Walk the tree and do this automatically
},
flights: {
type: new GraphQLList(Flight),
args: {
...PaginationFields,
fromIata: { type: IATA },
toIata: { type: IATA },
fromDate: { type: new GraphQLNonNull(ISODate) },
toDate: { type: new GraphQLNonNull(ISODate) },
},
resolve: createResolver('Flights'),
},
},
})
return new GraphQLSchema({
query: Query,
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment