Skip to content

Instantly share code, notes, and snippets.

@jsoneaday
Created October 3, 2020 17:30
Show Gist options
  • Save jsoneaday/f840603f57652ff146f59247d13a2c70 to your computer and use it in GitHub Desktop.
Save jsoneaday/f840603f57652ff146f59247d13a2c70 to your computer and use it in GitHub Desktop.
Resolvers
import { IResolvers } from "apollo-server-express";
import { addCar, getAllVehicles, getCars } from "../dataService";
import { Boat, Car, Truck } from "../types/Entities";
import { GqlContext } from "./GqlContext";
const resolvers: IResolvers = {
SearchResult: {
__resolveType(obj: any, ctx: GqlContext, info: any) {
if (obj.wheelCount && obj.passengerCount) return "Car";
if (obj.wheelCount && obj.maxPayload) return "Truck";
if (obj.passengerCount) return "Boat";
return "Car";
},
},
IVehicle: {
__resolveType(obj: any, ctx: GqlContext, info: any) {
if (obj.wheelCount && obj.passengerCount) return "Car";
if (obj.wheelCount && obj.maxPayload) return "Truck";
if (obj.passengerCount) return "Boat";
return "Car";
},
},
IPassengerVehicle: {
__resolveType(obj: any, ctx: GqlContext, info: any) {
if (obj.wheelCount && obj.passengerCount) {
return "Car";
}
return "Boat";
},
},
IWorkVehicle: {
__resolveType(obj: any, ctx: GqlContext, info: any) {
return "Truck";
},
},
ILandVehicle: {
__resolveType(obj: any, ctx: GqlContext, info: any) {
if (obj.wheelCount && obj.passengerCount) {
return "Car";
}
return "Truck";
},
},
Query: {
getAllVehicles: async (
obj: any,
args: null,
ctx: GqlContext,
info: any
): Promise<Array<Car | Truck | Boat>> => {
return getAllVehicles();
},
},
};
export default resolvers;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment