From 299984dea4cb3c678940d0bb02bba219893ca1d8 Mon Sep 17 00:00:00 2001 From: Michael Westergaard Date: Mon, 10 Nov 2025 14:35:13 +0100 Subject: [PATCH] Better generation for parameters with null-values Handle default parameters --- .../request/AbstractGraphQLRequest.java | 14 ++++++- .../client/request/InputParameter.java | 40 ++++++++++--------- 2 files changed, 34 insertions(+), 20 deletions(-) diff --git a/graphql-java-client-runtime/src/main/java/com/graphql_java_generator/client/request/AbstractGraphQLRequest.java b/graphql-java-client-runtime/src/main/java/com/graphql_java_generator/client/request/AbstractGraphQLRequest.java index 2c53f6ff93..867cf6a9de 100644 --- a/graphql-java-client-runtime/src/main/java/com/graphql_java_generator/client/request/AbstractGraphQLRequest.java +++ b/graphql-java-client-runtime/src/main/java/com/graphql_java_generator/client/request/AbstractGraphQLRequest.java @@ -484,8 +484,15 @@ private void readRequestParameters(QueryTokenizer qt, List input } else { mandatory = false; } + + Object defaultValue = null; + if (qt.checkNextToken("=")) { + token = qt.nextToken(); + token = qt.nextToken(); + defaultValue = token; + } - inputParameters.add(InputParameter.newGraphQLVariableParameter(schema, name, graphQLTypeName, mandatory, + inputParameters.add(InputParameter.newGraphQLVariableParameter(schema, name, defaultValue, graphQLTypeName, mandatory, listDepth, itemMandatory)); // The next token should be either the end of parameters (with a ')') or a name step = Step.NAME; @@ -882,6 +889,11 @@ public Payload getPayload(Map params) throws GraphQLRequestExecu ////////////////////////////////////////////////////////////////////// // And the variable value list (for the json variables field) payload.variables.put(param.getBindParameterName(), param.getValueForGraphqlQuery(params)); + + if (param.getValue() != null) { + sbGraphQLVariables.append("="); + sbGraphQLVariables.append(param.getValue()); + } separator = ","; //$NON-NLS-1$ } diff --git a/graphql-java-client-runtime/src/main/java/com/graphql_java_generator/client/request/InputParameter.java b/graphql-java-client-runtime/src/main/java/com/graphql_java_generator/client/request/InputParameter.java index 633faceffe..1a8be0d575 100644 --- a/graphql-java-client-runtime/src/main/java/com/graphql_java_generator/client/request/InputParameter.java +++ b/graphql-java-client-runtime/src/main/java/com/graphql_java_generator/client/request/InputParameter.java @@ -165,6 +165,8 @@ public static InputParameter newBindParameter(String schema, String name, String * schema, this plugin parameter is usually not set. In this case, its default value ("") is used. * @param name * The parameter name, as defined in the GraphQL schema + * @param value + * Optional default value for parameter * @param graphQLTypeName * The GraphQL type name of this parameter. For instance: "Human", for the type "[[Human]]" * @param mandatory @@ -177,9 +179,9 @@ public static InputParameter newBindParameter(String schema, String name, String * otherwise * @return The newly created {@link InputParameter}, according to these parameters */ - public static InputParameter newGraphQLVariableParameter(String schema, String name, String graphQLTypeName, + public static InputParameter newGraphQLVariableParameter(String schema, String name, Object value, String graphQLTypeName, boolean mandatory, int listDepth, boolean itemMandatory) { - return new InputParameter(schema, name, name, null, InputParameterType.GRAPHQL_VARIABLE, graphQLTypeName, + return new InputParameter(schema, name, name, value, InputParameterType.GRAPHQL_VARIABLE, graphQLTypeName, mandatory, listDepth, itemMandatory); } @@ -243,7 +245,7 @@ public static InputParameter newHardCodedParameter(String schema, String name, O * Used only if this parameter is a list. In this case: true if the item of the list are mandatory, false * otherwise */ - private InputParameter(String schema, String name, String bindParameterName, Object value, InputParameterType type, + protected InputParameter(String schema, String name, String bindParameterName, Object value, InputParameterType type, String graphQLTypeName, boolean mandatory, int listDepth, boolean itemMandatory) { if (name == null) { throw new NullPointerException("The input parameter's name is mandatory"); @@ -291,7 +293,7 @@ private InputParameter(String schema, String name, String bindParameterName, Obj * The field name * @throws GraphQLRequestPreparationException */ - private InputParameter(String schema, String name, String parameterName, Object value, InputParameterType type, + protected InputParameter(String schema, String name, String parameterName, Object value, InputParameterType type, Directive directive, Class owningClass, String fieldName) throws GraphQLRequestPreparationException { String localGraphQLCustomScalarType = null; boolean localMandatory = false; @@ -744,7 +746,7 @@ public String getStringContentForGraphqlQuery(boolean writingGraphQLVariables, M // If the InputParameter is mandatory, which must have its value in the map of BindVariables. if ((type.equals(InputParameterType.MANDATORY) || type.equals(InputParameterType.GRAPHQL_VARIABLE)) - && (bindVariables == null || !bindVariables.keySet().contains(bindParameterName))) { + && ((bindVariables == null || !bindVariables.keySet().contains(bindParameterName)) && value == null)) { throw new GraphQLRequestExecutionException( "The Bind Parameter for '" + bindParameterName + "' must be provided in the BindVariables map"); } @@ -779,15 +781,15 @@ public String getStringContentForGraphqlQuery(boolean writingGraphQLVariables, M * @return * @throws GraphQLRequestExecutionException */ - String getStringContentForGraphqlQuery(boolean writingGraphQLVariables, Object val, int listDepth, + protected String getStringContentForGraphqlQuery(boolean writingGraphQLVariables, Object val, int listDepth, String graphQLTypeNameParam, GraphQLScalarType graphQLScalarTypeParam, boolean graphQLVariable) throws GraphQLRequestExecutionException { - if (val == null) { - return null; - } else if (graphQLVariable && !writingGraphQLVariables) { - // When writing a GraphQL variable in the query itself, then we write the variable name. The value is - // written only in the GraphQL variable field - return "$" + bindParameterName; + if (graphQLVariable && !writingGraphQLVariables) { + // When writing a GraphQL variable in the query itself, then we write the variable name. The value is + // written only in the GraphQL variable field + return "$" + bindParameterName; + } else if (val == null) { + return null; } else if (listDepth > 0) { // We expect val to be a list return getStringContentForAListValue(writingGraphQLVariables, val, listDepth, graphQLTypeNameParam, @@ -842,7 +844,7 @@ String getStringContentForGraphqlQuery(boolean writingGraphQLVariables, Object v * @return * @throws GraphQLRequestExecutionException */ - private void appendStringContentForGraphqlQueryFromObjectNode(StringBuilder sb, JsonParser jsonParser, + protected void appendStringContentForGraphqlQueryFromObjectNode(StringBuilder sb, JsonParser jsonParser, ObjectNode node) throws GraphQLRequestExecutionException { try { @@ -963,7 +965,7 @@ private void appendStringContentForGraphqlQueryFromObjectNode(StringBuilder sb, * @return * @throws GraphQLRequestExecutionException */ - private void appendStringContentForGraphqlQueryFromMap(StringBuilder sb, Map map) { + protected void appendStringContentForGraphqlQueryFromMap(StringBuilder sb, Map map) { boolean appendComma = false;// false for the first item, then true for others sb.append('{'); for (Object key : map.keySet()) { @@ -994,7 +996,7 @@ private void appendStringContentForGraphqlQueryFromMap(StringBuilder sb, Map list) { + protected void appendStringContentForGraphqlQueryFromMapForAListItem(StringBuilder sb, List list) { boolean appendComma = false;// false for the first item, then true for others sb.append('['); for (Object val : list) { @@ -1022,7 +1024,7 @@ private void appendStringContentForGraphqlQueryFromMapForAListItem(StringBuilder * @param value * @throws GraphQLRequestExecutionException */ - private void appendStringContentForGraphqlQueryFromValueItem(StringBuilder sb, Object value) { + protected void appendStringContentForGraphqlQueryFromValueItem(StringBuilder sb, Object value) { if (value instanceof Map) { appendStringContentForGraphqlQueryFromMap(sb, (Map) value); } else if (value instanceof List) { @@ -1046,7 +1048,7 @@ private void appendStringContentForGraphqlQueryFromValueItem(StringBuilder sb, O * @see json.org section on strings * @return escaped string */ - private String getStringValue(String str) { + protected String getStringValue(String str) { String r = "\"" + StringEscapeUtils.escapeJson(str) + "\""; return r; } @@ -1071,7 +1073,7 @@ private String getStringValue(String str) { * @throws NullPointerException * If list is null */ - private String getStringContentForAListValue(boolean writingGraphQLVariables, Object list, int listDepth, + protected String getStringContentForAListValue(boolean writingGraphQLVariables, Object list, int listDepth, String graphQLTypeNameParam, GraphQLScalarType graphQLScalarTypeParam, boolean graphQLVariable) throws GraphQLRequestExecutionException { StringBuilder result = new StringBuilder("["); @@ -1114,7 +1116,7 @@ private String getStringContentForAListValue(boolean writingGraphQLVariables, Ob * @return The String that represents this object, according to GraphQL standard representation, as expected in the * query to be sent to the server */ - private String getStringContentForAnInputTypeValue(boolean writingGraphQLVariables, Object object, int listDepth, + protected String getStringContentForAnInputTypeValue(boolean writingGraphQLVariables, Object object, int listDepth, boolean graphQLVariable) throws GraphQLRequestExecutionException { StringBuilder result = new StringBuilder("{"); String separator = "";