Java Code Examples for graphql.schema.DataFetchingEnvironment#getSource()

The following examples show how to use graphql.schema.DataFetchingEnvironment#getSource() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EntityTitleFetcher.java    From timbuctoo with GNU General Public License v3.0 6 votes vote down vote up
@Override
public TypedValue get(DataFetchingEnvironment env) {
  if (env.getSource() instanceof SubjectReference) {
    SubjectReference source = env.getSource();
    DataSet dataSet = source.getDataSet();

    if (env.getParentType() instanceof GraphQLObjectType) {
      String type = getDirectiveArgument((GraphQLObjectType) env.getParentType(), "rdfType", "uri").orElse(null);

      Optional<TypedValue> summaryProperty = summaryPropDataRetriever.createSummaryProperty(source, dataSet, type);
      if (summaryProperty.isPresent()) {
        return summaryProperty.get();
      }
    }

    // fallback to the uri if no summary props can be found for the title
    return TypedValue.create(source.getSubjectUri(), STRING, dataSet);

  }
  return null;
}
 
Example 2
Source File: ViewConfigFetcher.java    From timbuctoo with GNU General Public License v3.0 6 votes vote down vote up
@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));
  }
}
 
Example 3
Source File: ResolutionEnvironment.java    From graphql-spqr with Apache License 2.0 6 votes vote down vote up
public ResolutionEnvironment(Resolver resolver, DataFetchingEnvironment env, ValueMapper valueMapper, GlobalEnvironment globalEnvironment,
                             ConverterRegistry converters, DerivedTypeRegistry derivedTypes) {

    this.context = env.getSource();
    this.rootContext = env.getContext();
    this.resolver = resolver;
    this.valueMapper = valueMapper;
    this.globalEnvironment = globalEnvironment;
    this.converters = converters;
    this.fieldType = env.getFieldType();
    this.parentType = (GraphQLNamedType) env.getParentType();
    this.graphQLSchema = env.getGraphQLSchema();
    this.dataFetchingEnvironment = env;
    this.derivedTypes = derivedTypes;
    this.arguments = new HashMap<>();
}
 
Example 4
Source File: ResolverDataFetcher.java    From graphql-apigen with Apache License 2.0 6 votes vote down vote up
@Batched
@Override
public Object get(DataFetchingEnvironment env) {
    List<Object> unresolved = new ArrayList<>();
    Object result;
    int depth = listDepth;
    if ( env.getSource() instanceof List ) { // batched.
        result = getBatched(env);
        if ( null != resolver ) addUnresolved(unresolved, result, ++depth);
    } else {
        result = getUnbatched(env);
        if ( null != resolver ) addUnresolved(unresolved, result, depth);
    }
    if ( null == resolver ) return result;
    return replaceResolved(result, resolver.resolve(unresolved).iterator(), depth);
}
 
Example 5
Source File: EntityDescriptionFetcher.java    From timbuctoo with GNU General Public License v3.0 6 votes vote down vote up
@Override
public TypedValue get(DataFetchingEnvironment env) {
  if (env.getSource() instanceof SubjectReference) {
    SubjectReference source = env.getSource();
    DataSet dataSet = source.getDataSet();

    if (env.getParentType() instanceof GraphQLObjectType) {
      String type = getDirectiveArgument((GraphQLObjectType) env.getParentType(), "rdfType", "uri").orElse(null);

      Optional<TypedValue> summaryProperty = summaryPropDataRetriever.createSummaryProperty(source, dataSet, type);
      if (summaryProperty.isPresent()) {
        return summaryProperty.get();
      }
    }
  }
  return null;
}
 
Example 6
Source File: MethodDataFetcher.java    From graphql-apigen with Apache License 2.0 5 votes vote down vote up
@Override
public Object get(DataFetchingEnvironment env) {
    Object source = ( null != impl ) ? impl : env.getSource();
    if (source == null) return null;
    if (source instanceof ResolveDataFetchingEnvironment) {
        source = ((ResolveDataFetchingEnvironment)source).resolve(env);
    }
    return getMethodViaGetter(source, env.getFieldType(), getFieldType(env.getParentType()), env.getArguments());
}
 
Example 7
Source File: DataSetImportStatusFetcher.java    From timbuctoo with GNU General Public License v3.0 5 votes vote down vote up
@Override
public List<LogEntryImportStatus> get(DataFetchingEnvironment env) {
  User currentUser = env.<ContextData>getContext().getUser()
                                                  .orElseThrow(() -> new RuntimeException("You are not logged in"));

  DataSetMetaData dataSetMetaData = env.getSource();

  MutationHelpers.checkPermission(env, dataSetMetaData, Permission.READ_IMPORT_STATUS);

  DataSetMetaData input = env.getSource();
  Optional<DataSet> dataSetOpt = dataSetRepository.getDataSet(
    currentUser,
    input.getOwnerId(),
    input.getDataSetId()
  );

  return dataSetOpt.map(dataSet -> dataSet.getImportManager().getLogList())
                   .map(logList -> {
                     final int[] num = new int[]{0};
                     return logList.getEntries().stream().map(logEntry -> {
                       Iterable<LogEntry> unprocessedEntries = logList::getUnprocessed;
                       boolean unprocessed = stream(unprocessedEntries.spliterator(), false)
                         .anyMatch(unprocessedEntry -> unprocessedEntry.equals(logEntry));
                       return new LogEntryImportStatus(logEntry, num[0]++, unprocessed);
                     }).collect(toList());
                   }).orElse(Lists.newArrayList());
}
 
Example 8
Source File: SummaryPropertiesDataFetcher.java    From timbuctoo with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Map<String, SummaryProp> get(DataFetchingEnvironment env) {
  HashMap<String, SummaryProp> summaryPropsMap = new HashMap<>();
  if (env.getSource() instanceof SubjectReference) {
    SubjectReference source = env.getSource();
    DataSet dataSet = source.getDataSet();
    QuadStore quadStore = dataSet.getQuadStore();
    summaryPropsMap.put("title", getData(source, quadStore, RdfConstants.TIM_SUMMARYTITLEPREDICATE));
    summaryPropsMap.put("description", getData(source, quadStore, RdfConstants.TIM_SUMMARYDESCRIPTIONPREDICATE));
    summaryPropsMap.put("image", getData(source, quadStore, RdfConstants.TIM_SUMMARYIMAGEPREDICATE));

  }
  return summaryPropsMap;
}
 
Example 9
Source File: LookUpSubjectByUriFetcherWrapper.java    From timbuctoo with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Object get(DataFetchingEnvironment environment) {
  final DatabaseResult source = environment.getSource();
  ParsedURI baseUri = new ParsedURI(source.getDataSet().getMetadata().getBaseUri());
  String uri = environment.getArgument(uriArgument);
  return lookUpSubjectByUriFetcher.getItem(
    baseUri.resolve(uri).toString(),
    source.getDataSet()
  );
}
 
Example 10
Source File: UrlTransformDataFetcher.java    From engine with GNU General Public License v3.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public Object doGet(final DataFetchingEnvironment environment) {
    String fieldName = environment.getField().getName();
    Map<String, Object> source = environment.getSource();
    if (source.containsKey(fieldName) && nonNull(source.get(fieldName))) {
        String transformerName = environment.getArgument(ARG_NAME_TRANSFORM);
        if (isNotEmpty(transformerName)) {
            return urlTransformationService.transform(transformerName, source.get(fieldName).toString());
        }
    }
    return source.get(fieldName);
}
 
Example 11
Source File: DynamicRelationDataFetcher.java    From timbuctoo with GNU General Public License v3.0 5 votes vote down vote up
@Override
public PaginatedDynamicList get(DataFetchingEnvironment environment) {
  if (environment.getSource() instanceof SubjectReference) {
    SubjectReference source = environment.getSource();
    PaginationArguments arguments = argumentsHelper.getPaginationArguments(environment);
    DataSet dataSet = ((DatabaseResult) environment.getSource()).getDataSet();
    String cursor = arguments.getCursor();
    String predicate = environment.getArgument("uri");
    Direction direction = environment.getArgument("outgoing") ? OUT : IN;

    try (Stream<CursorQuad> q = dataSet.getQuadStore()
      .getQuads(source.getSubjectUri(), predicate, direction, cursor)) {
      final PaginatedList<DatabaseResult> paginatedList = getPaginatedList(
        q,
        qd -> this.makeItem(qd, dataSet),
        arguments,
        Optional.empty()
      );
      return PaginatedDynamicList.create(
        paginatedList.getPrevCursor(),
        paginatedList.getNextCursor(),
        paginatedList.getItems()
      );
    }
  } else {
    return PaginatedDynamicList.create(
      Optional.empty(),
      Optional.empty(),
      Lists.newArrayList()
    );
  }
}
 
Example 12
Source File: FetchParams.java    From hypergraphql with Apache License 2.0 5 votes vote down vote up
public FetchParams(DataFetchingEnvironment environment, HGQLSchema hgqlschema) {

        subjectResource = environment.getSource();
        String predicate = ((Field) environment.getFields().toArray()[0]).getName();
        predicateURI = hgqlschema.getFields().get(predicate).getId();
        client = environment.getContext();
        if (!environment.getParentType().getName().equals("Query")) {
            String targetName = hgqlschema.getTypes().get(environment.getParentType().getName()).getField(predicate).getTargetName();
            if (hgqlschema.getTypes().containsKey(targetName) && hgqlschema.getTypes().get(targetName).getId()!=null) {
                targetURI=hgqlschema.getTypes().get(targetName).getId();
            }
        }
    }
 
Example 13
Source File: OrderItemProductFetcher.java    From research-graphql with MIT License 5 votes vote down vote up
@Override
public ProductDto get(DataFetchingEnvironment environment) {
    log.info("getSource {}", (Object)environment.getSource());
    OrderItemDto orderItemDto = environment.getSource();
    ProductDto product = productAdaptor.getProduct(orderItemDto.getProductId());
    return product;
}
 
Example 14
Source File: OrderUserFetcher.java    From research-graphql with MIT License 5 votes vote down vote up
@Override
public UserDto get(DataFetchingEnvironment environment) {
    log.info("getSource {}", (Object) environment.getSource());
    OrderDto order = environment.getSource();
    UserDto user = userAdaptor.getUser(order.getUserId());
    return user;
}
 
Example 15
Source File: ProtoDataFetcher.java    From rejoiner with Apache License 2.0 5 votes vote down vote up
@Override
public Object get(DataFetchingEnvironment environment) throws Exception {

  final Object source = environment.getSource();
  if (source == null) {
    return null;
  }

  if (source instanceof Message) {
    GraphQLType type = environment.getFieldType();
    if (type instanceof GraphQLEnumType) {
      return ((Message) source).getField(fieldDescriptor).toString();
    }
    return ((Message) source).getField(fieldDescriptor);
  }
  if (environment.getSource() instanceof Map) {
    return ((Map<?, ?>) source).get(convertedFieldName);
  }

  if (method == null) {
    // no synchronization necessary because this line is idempotent
    final String methodNameSuffix =
        fieldDescriptor.isMapField() ? "Map" : fieldDescriptor.isRepeated() ? "List" : "";
    final String methodName =
        "get" + LOWER_CAMEL_TO_UPPER.convert(convertedFieldName) + methodNameSuffix;
    method = source.getClass().getMethod(methodName);
  }
  return method.invoke(source);
}
 
Example 16
Source File: FetchParams.java    From hypergraphql with Apache License 2.0 5 votes vote down vote up
public FetchParams(DataFetchingEnvironment environment, HGQLSchema hgqlSchema)
        throws HGQLConfigurationException {

    final String predicate = extractPredicate(environment);
    predicateURI = extractPredicateUri(hgqlSchema, predicate);
    targetURI = extractTargetURI(environment, hgqlSchema, predicate);
    subjectResource = environment.getSource();
    client = environment.getContext();
}
 
Example 17
Source File: DefaultTypes.java    From graphql-java-type-generator with MIT License 4 votes vote down vote up
@Override
public Object get(DataFetchingEnvironment environment) {
    if (environment.getSource() == null) return null;
    return environment.getSource().toString();
}
 
Example 18
Source File: VertxMappedBatchLoaderTest.java    From vertx-web with Apache License 2.0 4 votes vote down vote up
private Object getLinkPostedBy(DataFetchingEnvironment env) {
  Link link = env.getSource();
  DataLoader<String, User> user = env.getDataLoader("user");
  return user.load(link.getUserId());
}
 
Example 19
Source File: VertxBatchLoaderTest.java    From vertx-web with Apache License 2.0 4 votes vote down vote up
private Object getLinkPostedBy(DataFetchingEnvironment env) {
  Link link = env.getSource();
  DataLoader<String, User> user = env.getDataLoader("user");
  return user.load(link.getUserId());
}
 
Example 20
Source File: GraphQLJavaToolsTest.java    From research-graphql with MIT License 4 votes vote down vote up
public Author author(DataFetchingEnvironment dfe) {
    Book book = dfe.getSource();
    return mapAuthors.get(book.getAuthorId());
}