Search in sources :

Example 1 with Selection

use of graphql.language.Selection in project structr by structr.

the class GraphQLQuery method init.

// ----- private methods -----
private void init(final SecurityContext securityContext, final Class type, final Field field, final String path) throws FrameworkException {
    final QueryConfig config = getConfig(path);
    config.handleTypeArguments(securityContext, type, field.getArguments());
    final SelectionSet selectionSet = field.getSelectionSet();
    if (selectionSet != null) {
        for (final Selection selection : selectionSet.getSelections()) {
            if (selection instanceof Field) {
                final Field childField = (Field) selection;
                final SelectionSet childSet = childField.getSelectionSet();
                final PropertyKey key = StructrApp.getConfiguration().getPropertyKeyForJSONName(type, childField.getName());
                final Class relatedType = key.relatedType() != null ? key.relatedType() : type;
                // add field to property set
                config.addPropertyKey(key);
                config.handleFieldArguments(securityContext, relatedType, field, childField);
                // recurse
                if (childSet != null) {
                    init(securityContext, relatedType, childField, path + "/" + childField.getName());
                }
            }
        }
    }
}
Also used : Field(graphql.language.Field) SelectionSet(graphql.language.SelectionSet) Selection(graphql.language.Selection) PropertyKey(org.structr.core.property.PropertyKey)

Example 2 with Selection

use of graphql.language.Selection 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;
}
Also used : DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) Entry(org.nextprot.api.core.domain.Entry) Autowired(org.springframework.beans.factory.annotation.Autowired) Field(graphql.language.Field) EntryBuilderService(org.nextprot.api.core.service.EntryBuilderService) Argument(graphql.language.Argument) Selection(graphql.language.Selection) Publication(org.nextprot.api.core.domain.Publication) Component(org.springframework.stereotype.Component) List(java.util.List) AnnotationCategory(org.nextprot.api.commons.constants.AnnotationCategory) EntryConfig(org.nextprot.api.core.service.fluent.EntryConfig) DataFetcher(graphql.schema.DataFetcher) Optional(java.util.Optional) Entry(org.nextprot.api.core.domain.Entry) Argument(graphql.language.Argument) Selection(graphql.language.Selection) Publication(org.nextprot.api.core.domain.Publication)

Example 3 with Selection

use of graphql.language.Selection in project structr by structr.

the class GraphQLRequest method initialize.

// ----- private methods -----
private void initialize(final SecurityContext securityContext, Document document) throws FrameworkException {
    for (final Node child : document.getChildren()) {
        if (child instanceof OperationDefinition) {
            final OperationDefinition operationDefinition = (OperationDefinition) child;
            final SelectionSet selectionSet = operationDefinition.getSelectionSet();
            for (final Selection selection : selectionSet.getSelections()) {
                if (selection instanceof Field) {
                    queries.add(new GraphQLQuery(securityContext, (Field) selection));
                } else {
                    logger.warn("Unknown selection set element {} in GraphQL query, ignoring.", selection.getClass());
                }
            }
        } else {
            logger.warn("Unknown document element {} in GraphQL query, ignoring.", child.getClass());
        }
    }
}
Also used : Field(graphql.language.Field) SelectionSet(graphql.language.SelectionSet) Selection(graphql.language.Selection) Node(graphql.language.Node) OperationDefinition(graphql.language.OperationDefinition)

Aggregations

Field (graphql.language.Field)3 Selection (graphql.language.Selection)3 SelectionSet (graphql.language.SelectionSet)2 Argument (graphql.language.Argument)1 Node (graphql.language.Node)1 OperationDefinition (graphql.language.OperationDefinition)1 DataFetcher (graphql.schema.DataFetcher)1 DataFetchingEnvironment (graphql.schema.DataFetchingEnvironment)1 List (java.util.List)1 Optional (java.util.Optional)1 AnnotationCategory (org.nextprot.api.commons.constants.AnnotationCategory)1 Entry (org.nextprot.api.core.domain.Entry)1 Publication (org.nextprot.api.core.domain.Publication)1 EntryBuilderService (org.nextprot.api.core.service.EntryBuilderService)1 EntryConfig (org.nextprot.api.core.service.fluent.EntryConfig)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1 Component (org.springframework.stereotype.Component)1 PropertyKey (org.structr.core.property.PropertyKey)1