Search in sources :

Example 6 with Argument

use of graphql.language.Argument in project graphql-java by graphql-java.

the class SchemaDiff method checkDirectives.

void checkDirectives(DiffCtx ctx, TypeDefinition old, List<Directive> oldDirectives, List<Directive> newDirectives) {
    if (!options.enforceDirectives) {
        return;
    }
    Map<String, Directive> oldDirectivesMap = sortedMap(oldDirectives, Directive::getName);
    Map<String, Directive> newDirectivesMap = sortedMap(newDirectives, Directive::getName);
    for (String directiveName : oldDirectivesMap.keySet()) {
        Directive oldDirective = oldDirectivesMap.get(directiveName);
        Optional<Directive> newDirective = Optional.ofNullable(newDirectivesMap.get(directiveName));
        if (!newDirective.isPresent()) {
            ctx.report(DiffEvent.apiBreakage().category(DiffCategory.MISSING).typeName(old.getName()).typeKind(getTypeKind(old)).components(directiveName).reasonMsg("The new API does not have a directive named '%s' on type '%s'", directiveName, old.getName()).build());
            continue;
        }
        Map<String, Argument> oldArgumentsByName = new TreeMap<>(oldDirective.getArgumentsByName());
        Map<String, Argument> newArgumentsByName = new TreeMap<>(newDirective.get().getArgumentsByName());
        if (oldArgumentsByName.size() > newArgumentsByName.size()) {
            ctx.report(DiffEvent.apiBreakage().category(DiffCategory.MISSING).typeName(old.getName()).typeKind(getTypeKind(old)).components(directiveName).reasonMsg("The new API has less arguments on directive '%s' on type '%s' than the old API", directiveName, old.getName()).build());
            return;
        }
        for (String argName : oldArgumentsByName.keySet()) {
            Argument oldArgument = oldArgumentsByName.get(argName);
            Optional<Argument> newArgument = Optional.ofNullable(newArgumentsByName.get(argName));
            if (!newArgument.isPresent()) {
                ctx.report(DiffEvent.apiBreakage().category(DiffCategory.MISSING).typeName(old.getName()).typeKind(getTypeKind(old)).components(directiveName, argName).reasonMsg("The new API does not have an argument named '%s' on directive '%s' on type '%s'", argName, directiveName, old.getName()).build());
            } else {
                Value oldValue = oldArgument.getValue();
                Value newValue = newArgument.get().getValue();
                if (oldValue != null && newValue != null) {
                    if (!oldValue.getClass().equals(newValue.getClass())) {
                        ctx.report(DiffEvent.apiBreakage().category(DiffCategory.INVALID).typeName(old.getName()).typeKind(getTypeKind(old)).components(directiveName, argName).reasonMsg("The new API has changed value types on argument named '%s' on directive '%s' on type '%s'", argName, directiveName, old.getName()).build());
                    }
                }
            }
        }
    }
}
Also used : Argument(graphql.language.Argument) Value(graphql.language.Value) TreeMap(java.util.TreeMap) Directive(graphql.language.Directive)

Example 7 with Argument

use of graphql.language.Argument in project graphql-java by graphql-java.

the class ValuesResolver method getArgumentValues.

public Map<String, Object> getArgumentValues(GraphqlFieldVisibility fieldVisibility, List<GraphQLArgument> argumentTypes, List<Argument> arguments, Map<String, Object> variables) {
    if (argumentTypes.isEmpty()) {
        return Collections.emptyMap();
    }
    Map<String, Object> result = new LinkedHashMap<>();
    Map<String, Argument> argumentMap = argumentMap(arguments);
    for (GraphQLArgument fieldArgument : argumentTypes) {
        String argName = fieldArgument.getName();
        Argument argument = argumentMap.get(argName);
        Object value;
        if (argument != null) {
            value = coerceValueAst(fieldVisibility, fieldArgument.getType(), argument.getValue(), variables);
        } else {
            value = fieldArgument.getDefaultValue();
        }
        // the default value ended up being something non null
        if (argumentMap.containsKey(argName) || value != null) {
            result.put(argName, value);
        }
    }
    return result;
}
Also used : GraphQLArgument(graphql.schema.GraphQLArgument) Argument(graphql.language.Argument) GraphQLArgument(graphql.schema.GraphQLArgument) LinkedHashMap(java.util.LinkedHashMap)

Example 8 with Argument

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

the class QueryConfig method handleFieldArguments.

public void handleFieldArguments(final SecurityContext securityContext, final Class type, final Field parentField, final Field field) throws FrameworkException {
    final ConfigurationProvider config = StructrApp.getConfiguration();
    final PropertyKey parentKey = config.getPropertyKeyForJSONName(type, parentField.getName());
    final PropertyKey key = config.getPropertyKeyForJSONName(type, field.getName(), false);
    final List<SearchTuple> searchTuples = new LinkedList<>();
    Occurrence occurrence = Occurrence.REQUIRED;
    // parse arguments
    for (final Argument argument : field.getArguments()) {
        final String name = argument.getName();
        final Value value = argument.getValue();
        switch(name) {
            case "_equals":
                searchTuples.add(new SearchTuple(castValue(securityContext, key.relatedType(), key, value), true));
                break;
            case "_contains":
                searchTuples.add(new SearchTuple(castValue(securityContext, key.relatedType(), key, value), false));
                break;
            case "_conj":
                occurrence = getOccurrence(value);
                break;
            default:
                break;
        }
    }
    // only add field if a value was set
    for (final SearchTuple tuple : searchTuples) {
        addAttribute(parentKey, key.getSearchAttribute(securityContext, occurrence, tuple.value, tuple.exact, null), occurrence);
    }
}
Also used : Argument(graphql.language.Argument) ConfigurationProvider(org.structr.schema.ConfigurationProvider) ObjectValue(graphql.language.ObjectValue) Value(graphql.language.Value) StringValue(graphql.language.StringValue) IntValue(graphql.language.IntValue) BooleanValue(graphql.language.BooleanValue) Occurrence(org.structr.api.search.Occurrence) PropertyKey(org.structr.core.property.PropertyKey) LinkedList(java.util.LinkedList)

Example 9 with Argument

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

the class QueryConfig method handleTypeArguments.

public void handleTypeArguments(final SecurityContext securityContext, final Class type, final List<Argument> arguments) throws FrameworkException {
    // parse arguments
    for (final Argument argument : arguments) {
        final String name = argument.getName();
        final Value value = argument.getValue();
        switch(name) {
            case "_page":
                this.page = getIntegerValue(value, 1);
                break;
            case "_pageSize":
            case "_first":
                this.pageSize = getIntegerValue(value, Integer.MAX_VALUE);
                break;
            case "_sort":
                this.sortKeySource = getStringValue(value, "name");
                this.sortKey = StructrApp.getConfiguration().getPropertyKeyForJSONName(type, sortKeySource, false);
                break;
            case "_desc":
                this.sortDescending = getBooleanValue(value, false);
                break;
            default:
                // handle simple selections like an _equals on the field
                final PropertyKey key = StructrApp.getConfiguration().getPropertyKeyForJSONName(type, name, false);
                if (key != null) {
                    addAttribute(key, key.getSearchAttribute(securityContext, Occurrence.REQUIRED, castValue(securityContext, type, key, value), true, null), Occurrence.REQUIRED);
                }
                break;
        }
    }
}
Also used : Argument(graphql.language.Argument) ObjectValue(graphql.language.ObjectValue) Value(graphql.language.Value) StringValue(graphql.language.StringValue) IntValue(graphql.language.IntValue) BooleanValue(graphql.language.BooleanValue) PropertyKey(org.structr.core.property.PropertyKey)

Example 10 with Argument

use of graphql.language.Argument 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)

Aggregations

Argument (graphql.language.Argument)19 Directive (graphql.language.Directive)9 StringValue (graphql.language.StringValue)9 List (java.util.List)9 Optional (java.util.Optional)9 Map (java.util.Map)8 Collectors (java.util.stream.Collectors)8 GraphQLError (graphql.GraphQLError)7 Internal (graphql.Internal)7 AstPrinter (graphql.language.AstPrinter)7 EnumTypeDefinition (graphql.language.EnumTypeDefinition)7 EnumValueDefinition (graphql.language.EnumValueDefinition)7 FieldDefinition (graphql.language.FieldDefinition)7 InputObjectTypeDefinition (graphql.language.InputObjectTypeDefinition)7 InputValueDefinition (graphql.language.InputValueDefinition)7 InterfaceTypeDefinition (graphql.language.InterfaceTypeDefinition)7 ObjectTypeDefinition (graphql.language.ObjectTypeDefinition)7 Type (graphql.language.Type)7 TypeDefinition (graphql.language.TypeDefinition)7 TypeName (graphql.language.TypeName)7