graphql.language.Argument Java Examples

The following examples show how to use graphql.language.Argument. 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: Generator.java    From smallrye-graphql with Apache License 2.0 5 votes vote down vote up
private Type<?> type(Argument argument) {
    Value<?> value = argument.getValue();
    if (value instanceof VariableReference)
        return resolve(((VariableReference) value).getName());
    throw new GraphQlGeneratorException(
            "unsupported type " + value + " for argument '" + argument.getName() + "'");
}
 
Example #2
Source File: ValidationDecorator.java    From smallrye-graphql with Apache License 2.0 5 votes vote down vote up
private Argument requestedArgument(Node node) {
    String graphQLArgumentName = dfe.getFieldDefinition().getArguments().get(parameterIndex(node.getName()))
            .getName();

    Field requestedField = dfe.getMergedField().getSingleField();

    return requestedField.getArguments().stream()
            .filter(argument -> argument.getName().equals(graphQLArgumentName))
            .findFirst()
            .orElseThrow(() -> new AssertionError(
                    "expected field " + graphQLArgumentName + " in " + requestedField.getArguments()));
}
 
Example #3
Source File: TransformException.java    From smallrye-graphql with Apache License 2.0 5 votes vote down vote up
private SourceLocation getSourceLocation(DataFetchingEnvironment dfe,
        DataFetcherExceptionHandlerParameters handlerParameters) {
    List<Argument> arguments = dfe.getField().getArguments();
    for (Argument a : arguments) {
        if (a.getName().equals(this.field.getName())) {
            return a.getSourceLocation();
        }
    }
    // Else fallback to more general
    return handlerParameters.getSourceLocation();
}
 
Example #4
Source File: ExecutionTreeNode.java    From hypergraphql with Apache License 2.0 5 votes vote down vote up
private JsonNode getFieldJson(Field field, String parentId, String nodeId, String parentType) {

        ObjectMapper mapper = new ObjectMapper();
        ObjectNode query = mapper.createObjectNode();

        query.put("name", field.getName());
        query.put("alias", field.getAlias());
        query.put("parentId", parentId);
        query.put("nodeId", nodeId);
        List<Argument> args = field.getArguments();

        String contextLdKey = (field.getAlias()==null) ? field.getName() : field.getAlias();
        String contextLdValue = getContextLdValue(contextLdKey);

        this.ldContext.put(contextLdKey, contextLdValue);

        if (args.isEmpty()) {
            query.set("args", null);
        } else {
            query.set("args", getArgsJson(args));
        }

        FieldOfTypeConfig fieldConfig = hgqlSchema.getTypes().get(parentType).getField(field.getName());
        String targetName = fieldConfig.getTargetName();

        query.put("targetName", targetName);
        query.set("fields", this.traverse(field, nodeId, parentType));

        return query;
    }
 
Example #5
Source File: ExecutionTreeNode.java    From hypergraphql with Apache License 2.0 5 votes vote down vote up
private JsonNode getFieldJson(Field field, String parentId, String nodeId, String parentType) {

        ObjectMapper mapper = new ObjectMapper();
        ObjectNode query = mapper.createObjectNode();

        query.put("name", field.getName());
        query.put("alias", field.getAlias());
        query.put("parentId", parentId);
        query.put("nodeId", nodeId);
        List<Argument> args = field.getArguments();

        String contextLdKey = (field.getAlias()==null) ? field.getName() : field.getAlias();
        String contextLdValue = getContextLdValue(contextLdKey);

        this.ldContext.put(contextLdKey, contextLdValue);

        if (args.isEmpty()) {
            query.set("args", null);
        } else {
            query.set("args", getArgsJson(args));
        }

        FieldOfTypeConfig fieldConfig = hgqlSchema.getTypes().get(parentType).getField(field.getName());
        String targetName = fieldConfig.getTargetName();

        query.put("targetName", targetName);
        query.set("fields", this.traverse(field, nodeId, parentType));

        return query;
    }
 
Example #6
Source File: TypeEntry.java    From graphql-apigen with Apache License 2.0 5 votes vote down vote up
private static String getPackageName(List<Directive> directives, String defaultPackageName) {
    String packageName = null;
    for ( Directive directive : directives ) {
        if ( ! "java".equals(directive.getName()) ) continue;
        for ( Argument arg : directive.getArguments() ) {
            if ( ! "package".equals(arg.getName()) ) continue;
            packageName = (String)Scalars.GraphQLString.getCoercing().parseLiteral(arg.getValue());
            break;
        }
        break;
    }
    return ( null == packageName ) ? defaultPackageName : packageName;
}
 
Example #7
Source File: GraphQLIntrospectionResultToSchema.java    From js-graphql-intellij-plugin with MIT License 5 votes vote down vote up
private List<Directive> createDeprecatedDirective(Map<String, Object> field) {
    if ((Boolean) field.get("isDeprecated")) {
        String reason = (String) field.get("deprecationReason");
        if (reason == null) {
            reason = "No longer supported"; // default according to spec
        }
        Argument reasonArg = new Argument("reason", new StringValue(reason));
        return Collections.singletonList(new Directive("deprecated", Collections.singletonList(reasonArg)));
    }
    return Collections.emptyList();
}
 
Example #8
Source File: ExtendedJpaDataFetcher.java    From graphql-jpa with MIT License 5 votes vote down vote up
private PageInformation extractPageInformation(DataFetchingEnvironment environment, Field field) {
    Optional<Argument> paginationRequest = field.getArguments().stream().filter(it -> GraphQLSchemaBuilder.PAGINATION_REQUEST_PARAM_NAME.equals(it.getName())).findFirst();
    if (paginationRequest.isPresent()) {
        field.getArguments().remove(paginationRequest.get());

        ObjectValue paginationValues = (ObjectValue) paginationRequest.get().getValue();
        IntValue page = (IntValue) paginationValues.getObjectFields().stream().filter(it -> "page".equals(it.getName())).findFirst().get().getValue();
        IntValue size = (IntValue) paginationValues.getObjectFields().stream().filter(it -> "size".equals(it.getName())).findFirst().get().getValue();

        return new PageInformation(page.getValue().intValue(), size.getValue().intValue());
    }

    return new PageInformation(1, Integer.MAX_VALUE);
}
 
Example #9
Source File: FieldComplexityCalculatorImpl.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
@Override
public int calculate(FieldComplexityEnvironment fieldComplexityEnvironment, int childComplexity) {
    String fieldName = fieldComplexityEnvironment.getField().getName();
    String parentType = fieldComplexityEnvironment.getParentType().getName();
    List<Argument> ArgumentList = fieldComplexityEnvironment.getField().getArguments();

    int argumentsValue = getArgumentsValue(ArgumentList);
    int customFieldComplexity = getCustomComplexity(fieldName, parentType, policyDefinition);
    return (argumentsValue * (customFieldComplexity + childComplexity));
}
 
Example #10
Source File: FieldComplexityCalculatorImpl.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
private int getArgumentsValue(List<Argument> argumentList) {
    int argumentValue = 0;
    if (argumentList.size() > 0) {
        for (Argument object : argumentList) {
            BigInteger value = ((IntValue) object.getValue()).getValue();
            int val = value.intValue();
            argumentValue = argumentValue + val;
        }
    } else {
        argumentValue = 1;
    }
    return argumentValue;
}
 
Example #11
Source File: QueryComplexityNode.java    From glitr with MIT License 4 votes vote down vote up
public List<Argument> getArguments() {
    return arguments;
}
 
Example #12
Source File: QueryComplexityNode.java    From glitr with MIT License 4 votes vote down vote up
public void setArguments(List<Argument> arguments) {
    this.arguments = arguments;
}
 
Example #13
Source File: QueryComplexityNode.java    From glitr with MIT License 4 votes vote down vote up
public void addArgument(Argument argument) {
    if (arguments == null) {
        arguments = new ArrayList<>();
    }
    arguments.add(argument);
}
 
Example #14
Source File: ContentTypeBasedDataFetcher.java    From engine with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Adds the required filters to the ES query for the given field
 */
protected void processSelection(String path, Selection currentSelection, BoolQueryBuilder query,
                                List<String> queryFieldIncludes, DataFetchingEnvironment env)  {
    if (currentSelection instanceof Field) {
        // If the current selection is a field
        Field currentField = (Field) currentSelection;

        // Get the original field name
        String propertyName = getOriginalName(currentField.getName());
        // Build the ES-friendly path
        String fullPath = StringUtils.isEmpty(path)? propertyName : path + "." + propertyName;

        // If the field has sub selection
        if (Objects.nonNull(currentField.getSelectionSet())) {
            // If the field is a flattened component
            if (fullPath.matches(COMPONENT_INCLUDE_REGEX)) {
                // Include the 'content-type' field to make sure the type can be resolved during runtime
                String contentTypeFieldPath = fullPath + "." + QUERY_FIELD_NAME_CONTENT_TYPE;
                if (!queryFieldIncludes.contains(contentTypeFieldPath)) {
                    queryFieldIncludes.add(contentTypeFieldPath);
                }
            }

            // Process recursively and finish
            currentField.getSelectionSet().getSelections()
                .forEach(selection -> processSelection(fullPath, selection, query, queryFieldIncludes, env));
            return;
        }

        // Add the field to the list
        logger.debug("Adding selected field '{}' to query", fullPath);
        queryFieldIncludes.add(fullPath);

        // Check the filters to build the ES query
        Optional<Argument> arg =
            currentField.getArguments().stream().filter(a -> a.getName().equals(FILTER_NAME)).findFirst();
        if (arg.isPresent()) {
            logger.debug("Adding filters for field {}", fullPath);
            Value<?> argValue = arg.get().getValue();
            if (argValue instanceof ObjectValue) {
                List<ObjectField> filters = ((ObjectValue) argValue).getObjectFields();
                filters.forEach((filter) -> addFieldFilterFromObjectField(fullPath, filter, query, env));
            } else if (argValue instanceof VariableReference &&
                    env.getVariables().containsKey(((VariableReference) argValue).getName())) {
                Map<String, Object> map =
                        (Map<String, Object>) env.getVariables().get(((VariableReference) argValue).getName());
                map.entrySet().forEach(filter -> addFieldFilterFromMapEntry(fullPath, filter, query, env));
            }
        }
    } else if (currentSelection instanceof InlineFragment) {
        // If the current selection is an inline fragment, process recursively
        InlineFragment fragment = (InlineFragment) currentSelection;
        fragment.getSelectionSet().getSelections()
            .forEach(selection -> processSelection(path, selection, query, queryFieldIncludes, env));
    } else if (currentSelection instanceof FragmentSpread) {
        // If the current selection is a fragment spread, find the fragment and process recursively
        FragmentSpread fragmentSpread = (FragmentSpread) currentSelection;
        FragmentDefinition fragmentDefinition = env.getFragmentsByName().get(fragmentSpread.getName());
        fragmentDefinition.getSelectionSet().getSelections()
            .forEach(selection -> processSelection(path, selection, query, queryFieldIncludes, env));
    }
}