Created
April 5, 2020 19:11
-
-
Save john-nash-rs/71928e9931752248dc34036b685340ac to your computer and use it in GitHub Desktop.
This file contains 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
package com.nulpointerexception.cabdetailsgraphQLdemo; | |
import com.google.common.collect.ImmutableMap; | |
import graphql.schema.DataFetcher; | |
import org.springframework.stereotype.Component; | |
import java.util.Arrays; | |
import java.util.List; | |
import java.util.Map; | |
@Component | |
public class GraphQLDataFetchers { | |
private static List<Map<String, String>> cars = Arrays.asList( | |
ImmutableMap.of("id", "car1", | |
"carNumber", "KA01H", | |
"driverId", "12"), | |
ImmutableMap.of("id", "car2", | |
"carNumber", "BR01H", | |
"driverId", "13") | |
); | |
private static List<Map<String, String>> drivers = Arrays.asList( | |
ImmutableMap.of("id", "12", | |
"firstName", "Harsh", | |
"lastName", "Vardhan"), | |
ImmutableMap.of("id", "13", | |
"firstName", "Rohit", | |
"lastName", "Singh") | |
); | |
public DataFetcher getCabByIdDateFetcher(){ | |
return dataFetchingEnvironment -> { | |
String carId = dataFetchingEnvironment.getArgument("id"); | |
Map<String, String> carInStore = cars.stream() | |
.filter(car -> car.get("id").equals(carId)) | |
.findFirst() | |
.orElse(null); | |
return carInStore; | |
}; | |
} | |
public DataFetcher getDriverDataFetcher(){ | |
return dataFetchingEnvironment -> { | |
Map<String, String> carInStore = dataFetchingEnvironment.getSource(); | |
String driverId = carInStore.get("driverId"); | |
return drivers.stream() | |
.filter(driver -> driver.get("id").equals(driverId)) | |
.findFirst() | |
.orElse(null); | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment