use of graphql.schema.DataFetchingEnvironment in project graphql-java by graphql-java.
the class FunWithStringsSchemaFactory method createBatched.
public static FunWithStringsSchemaFactory createBatched(final Map<CallType, AtomicInteger> callCounts) {
FunWithStringsSchemaFactory factory = new FunWithStringsSchemaFactory();
factory.setStringObjectValueFetcher(new DataFetcher() {
@Override
@Batched
@SuppressWarnings("unchecked")
public Object get(DataFetchingEnvironment environment) {
increment(callCounts, CallType.VALUE);
List<String> retVal = new ArrayList<>();
for (String s : (List<String>) environment.getSource()) {
retVal.add("null".equals(s) ? null : s);
}
return retVal;
}
});
factory.setThrowExceptionFetcher(new DataFetcher() {
@Override
@Batched
public Object get(DataFetchingEnvironment environment) {
throw new RuntimeException("TestException");
}
});
factory.setReturnBadList(new DataFetcher() {
@Override
@Batched
@SuppressWarnings("unchecked")
public Object get(DataFetchingEnvironment environment) {
List<String> retVal = new ArrayList<>();
for (String s : (List<String>) environment.getSource()) {
retVal.add("null".equals(s) ? null : s);
}
retVal.add("badValue");
return retVal;
}
});
factory.setAnyIterable(new DataFetcher() {
@Override
@Batched
@SuppressWarnings("unchecked")
public Object get(DataFetchingEnvironment environment) {
List<Iterable<String>> retVal = new ArrayList<>();
for (String s : (List<String>) environment.getSource()) {
retVal.add(new LinkedHashSet<>(Arrays.asList(s, "end")));
}
return retVal;
}
});
factory.setAppendFetcher(new DataFetcher() {
@Override
@Batched
@SuppressWarnings("unchecked")
public Object get(DataFetchingEnvironment environment) {
increment(callCounts, CallType.APPEND);
List<String> retVal = new ArrayList<>();
for (String s : (List<String>) environment.getSource()) {
retVal.add(s + environment.getArgument("text"));
}
// make it an Iterable thing not just List
return new ArrayDeque<>(retVal);
}
});
factory.setWordsAndLettersFetcher(new DataFetcher() {
@Batched
@Override
@SuppressWarnings("unchecked")
public Object get(DataFetchingEnvironment environment) {
increment(callCounts, CallType.WORDS_AND_LETTERS);
List<String> sources = environment.getSource();
List<List<List<String>>> retVal = new ArrayList<>();
for (String source : sources) {
List<List<String>> sentence = new ArrayList<>();
splitSentence(source, sentence);
retVal.add(sentence);
}
return retVal.toArray();
}
});
factory.setSplitFetcher(new DataFetcher() {
@Batched
@Override
@SuppressWarnings("unchecked")
public Object get(DataFetchingEnvironment environment) {
increment(callCounts, CallType.SPLIT);
String regex = environment.getArgument("regex");
List<String> sources = environment.getSource();
List<List<String>> retVal = new ArrayList<>();
if (regex == null) {
for (String ignored : sources) {
retVal.add(null);
}
return retVal;
}
for (String source : sources) {
List<String> retItem = new ArrayList<>();
for (String str : source.split(regex)) {
if (str.isEmpty()) {
retItem.add(null);
} else {
retItem.add(str);
}
}
retVal.add(retItem);
}
return retVal;
}
});
factory.setShatterFetcher(new DataFetcher() {
@Batched
@Override
@SuppressWarnings("unchecked")
public Object get(DataFetchingEnvironment environment) {
increment(callCounts, CallType.SHATTER);
List<String> sources = environment.getSource();
List<List<String>> retVal = new ArrayList<>();
for (String source : sources) {
List<String> retItem = new ArrayList<>();
for (char c : source.toCharArray()) {
retItem.add(Character.toString(c));
}
retVal.add(retItem);
}
return retVal;
}
});
return factory;
}
use of graphql.schema.DataFetchingEnvironment in project graphql-java by graphql-java.
the class UnbatchedDataFetcher method get.
@Override
public CompletableFuture<List<Object>> get(DataFetchingEnvironment environment) {
List<Object> sources = environment.getSource();
List<CompletableFuture<Object>> results = new ArrayList<>();
for (Object source : sources) {
DataFetchingEnvironment singleEnv = newDataFetchingEnvironment(environment).source(source).build();
CompletableFuture<Object> cf = Async.toCompletableFuture(delegate.get(singleEnv));
results.add(cf);
}
return Async.each(results);
}
use of graphql.schema.DataFetchingEnvironment in project timbuctoo by HuygensING.
the class ViewConfigFetcher method get.
@Override
public Object get(DataFetchingEnvironment env) {
SubjectReference source = env.getSource();
final DataSet dataSet = source.getDataSet();
final QuadStore qs = dataSet.getQuadStore();
final Map<String, Type> schema = dataSet.getSchemaStore().getStableTypes();
final TypeNameStore typeNameStore = dataSet.getTypeNameStore();
try (Stream<CursorQuad> quads = qs.getQuads(source.getSubjectUri(), HAS_VIEW_CONFIG, Direction.OUT, "")) {
return quads.findFirst().flatMap(q -> {
try {
return Optional.ofNullable(objectMapper.readValue(q.getObject(), List.class));
} catch (IOException e) {
LOG.error("view config is not a valid JSON object", e);
return Optional.empty();
}
}).orElseGet(() -> makeDefaultViewConfig(source.getSubjectUri(), schema, typeNameStore));
}
}
use of graphql.schema.DataFetchingEnvironment in project nextprot-api by calipho-sib.
the class EntryDataFetcherImpl method get.
@Override
public Entry get(DataFetchingEnvironment environment) {
// TODO THIS IS A POC (Proof of Concept)
// If we decide to go ahead with GraphQL please try to adapt this terrible function using EntryQueryResolver that implements GraphQLQueryResolver
// graphql java tools allows to do newSchemaParser().file(schemaFile).resolvers(...).getSchemaObjects.getGraphqlSchema
String accession = environment.getArgument("accession");
Integer publicationLimit = -1;
Integer annotationLimit = -1;
String category = "";
// Searching for publication limit
Optional<Selection> publicationField = environment.getFields().get(0).getSelectionSet().getSelections().stream().filter(f -> ((Field) f).getName().equals("publications")).findAny();
if (publicationField.isPresent()) {
Optional<Argument> publicationLimitArg = ((Field) publicationField.get()).getArguments().stream().filter(f -> f.getName().equals("limit")).findAny();
if (publicationLimitArg.isPresent()) {
publicationLimit = Integer.valueOf(publicationLimitArg.get().getValue().toString().replace("IntValue{value=", "").replace("}", ""));
}
}
// Searching for annotation field
Optional<Selection> annotationField = environment.getFields().get(0).getSelectionSet().getSelections().stream().filter(f -> ((Field) f).getName().equals("annotations")).findAny();
if (annotationField.isPresent()) {
Optional<Argument> publicationLimitArg = ((Field) annotationField.get()).getArguments().stream().filter(f -> f.getName().equals("category")).findAny();
if (publicationLimitArg.isPresent()) {
category = publicationLimitArg.get().getValue().toString().replace("StringValue{value='", "").replace("'}", "");
}
Optional<Argument> annotationLimitArg = ((Field) annotationField.get()).getArguments().stream().filter(f -> f.getName().equals("limit")).findAny();
if (annotationLimitArg.isPresent()) {
annotationLimit = Integer.valueOf(annotationLimitArg.get().getValue().toString().replace("IntValue{value=", "").replace("}", ""));
}
}
Entry entry = entryBuilderService.build(EntryConfig.newConfig(accession).with(category).withPublications().withOverview().withTargetIsoforms());
if (publicationLimit != -1) {
List<Publication> publications = entry.getPublications();
List<Publication> publicationSubset = publications.subList(0, publicationLimit);
entry.setPublications(publicationSubset);
}
if (annotationLimit != -1) {
String cat = entry.getAnnotationsByCategory().keySet().iterator().next();
entry.setAnnotations(entry.getAnnotationsByCategory(AnnotationCategory.getDecamelizedAnnotationTypeName(cat)).subList(0, annotationLimit));
}
return entry;
}
use of graphql.schema.DataFetchingEnvironment in project ontrack by nemerosa.
the class GitChangeLogBranchGraphQLFieldContributor method gitChangeLogFetcher.
private DataFetcher gitChangeLogFetcher() {
return fetcher(Branch.class, (DataFetchingEnvironment environment, Branch branch) -> {
String from = GraphqlUtils.getStringArgument(environment, "from").orElseThrow(() -> new IllegalStateException("From argument is required."));
String to = GraphqlUtils.getStringArgument(environment, "to").orElseThrow(() -> new IllegalStateException("To argument is required."));
Build fromBuild = structureService.findBuildByName(branch.getProject().getName(), branch.getName(), from).orElseThrow(() -> new BuildNotFoundException(branch.getProject().getName(), branch.getName(), from));
Build toBuild = structureService.findBuildByName(branch.getProject().getName(), branch.getName(), to).orElseThrow(() -> new BuildNotFoundException(branch.getProject().getName(), branch.getName(), to));
return gitService.changeLog(new BuildDiffRequest(fromBuild.getId(), toBuild.getId()));
});
}
Aggregations