Class GraphqlClientUtils
java.lang.Object
com.graphql_java_generator.client.GraphqlClientUtils
- Author:
- etienne-sf
-
Field Summary
Modifier and TypeFieldDescriptionstatic GraphqlClientUtils
This singleton is usable in default method, within interfaces -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionClass<?>
checkFieldOfGraphQLType
(String name, Boolean shouldBeScalar, Class<?> owningClass) Check if the given field is owned by the class of thisObjectResponse
.Class<?>
checkIsScalar
(Field field, Boolean shouldBeScalar) This method checks whether the given field (as an attribute) of the given class is a GraphQL scalar, or not, depending on shouldBeScalar.Class<?>
checkIsScalar
(String fieldName, Method method, Boolean shouldBeScalar) This method checks whether the given field (as a method: getter, query...) of the given class is a GraphQL scalar, or not, depending on shouldBeScalar.void
Checks that the given GraphQL name is valid.generatesBindVariableValuesMap
(Object[] paramsAndValues) This method retrieves the couple of name and values given in these parameters, stores them in a map where the key is the param name, and the value is the value of theMap
.Class<?>
Retrieves a class for a given classname.getDeclaredField
(Class<?> owningClass, String fieldName, boolean mustFindField) Returns aField
from the given class.Class<?>
getFieldType
(Class<?> owningClass, String fieldName, boolean returnIsMandatory) Retrieves the class of the fieldName field of the owningClass class.graphql.schema.GraphQLScalarType
getGraphQLCustomScalarType
(AccessibleObject fieldOrMethod, String schema) This method retrieves theGraphQLScalarType
for a custom scalar field or method.graphql.schema.GraphQLScalarType
getGraphQLScalarTypeFromName
(String typeName, String schema) Returns the GraphQL scalar type for the given Standard or Custom Scalar name, as defined in the GraphQL schema.Class<?>
getGraphQLType
(AccessibleObject fieldOrMethod) Returns the Class indicated as the value for the graphqlType attribute of the GraphQLScalar or GraphQLNonScalar annotationgetGraphQLTypeNameFromClass
(Class<?> clazz) Retrieves the GraphQL type name (as defined in the GraphQL schema), from the GraphQL annotation added in the generated code by the plugin.boolean
isScalar
(AccessibleObject fieldOrMethod) Indicates whether the given class is a scalar or notparseValueForInputParameter
(Object parameterValue, String parameterType, Class<?> parameterClass, String schema) Parse a value, depending on the parameter type.
-
Field Details
-
graphqlClientUtils
This singleton is usable in default method, within interfaces
-
-
Constructor Details
-
GraphqlClientUtils
public GraphqlClientUtils()
-
-
Method Details
-
checkName
Checks that the given GraphQL name is valid.- Parameters:
graphqlIdentifier
-- Throws:
NullPointerException
- If name is nullGraphQLRequestPreparationException
- If the given graphqlIdentifier is not a valid identifier
-
checkIsScalar
public Class<?> checkIsScalar(Field field, Boolean shouldBeScalar) throws GraphQLRequestPreparationException This method checks whether the given field (as an attribute) of the given class is a GraphQL scalar, or not, depending on shouldBeScalar.- Parameters:
field
- The field whose type should be (or not) a scalarshouldBeScalar
- if true, this method checks that field's type is a scalar (if false, checks that it is not a scalar)- Returns:
- Returns the Class indicated as the value for the graphqlType attribute of the GraphQLScalar or GraphQLNonScalar annotation
- Throws:
GraphQLRequestPreparationException
-
checkIsScalar
public Class<?> checkIsScalar(String fieldName, Method method, Boolean shouldBeScalar) throws GraphQLRequestPreparationException This method checks whether the given field (as a method: getter, query...) of the given class is a GraphQL scalar, or not, depending on shouldBeScalar.- Parameters:
fieldName
- the name of the field represented by the given method.method
- The method whose return should be (or not) a scalar. This method can be a setter, a getter (in which case its name is different from the fieldName), or a query/mutation/subscription (in which case its name is the fieldName)shouldBeScalar
- if true, this method checks that method return type is a scalar (if false, checks that it is not a scalar)- Throws:
GraphQLRequestPreparationException
-
isScalar
Indicates whether the given class is a scalar or not- Parameters:
fieldOrMethod
-- Returns:
- true if clazz is a scalar type
- Throws:
GraphQLRequestPreparationException
-
getClass
Retrieves a class for a given classname. For standard GraphQL types (Int, Boolean...) the good package is used (java.lang, java.lang, java.util...). For others, the class is retrieved from the generated GraphQLTypeMapping.- Parameters:
packageName
- The name of the package, where the code has been generated.graphQLTypeName
- The name of the classschema
- value of the springBeanSuffix plugin parameter for the searched schema. When there is only one schema, this plugin parameter is usually not set. In this case, its default value ("") is used.- Returns:
-
getDeclaredField
public Field getDeclaredField(Class<?> owningClass, String fieldName, boolean mustFindField) throws GraphQLRequestPreparationException Returns aField
from the given class.- Parameters:
owningClass
- The class that should contain this field. If the class's name finishes by Response, as an empty XxxResponse class is created for each Query/Mutation/Subscription (to be compatible with previsous version), then this method also looks in the owningClass's superclass.fieldName
- The name of the searched fieldmustFindField
- If true and the field is not found, aGraphQLRequestPreparationException
is thrown.
If false an the field is not found, the method returns null- Returns:
- Throws:
GraphQLRequestPreparationException
-
getFieldType
public Class<?> getFieldType(Class<?> owningClass, String fieldName, boolean returnIsMandatory) throws GraphQLRequestPreparationException Retrieves the class of the fieldName field of the owningClass class.- Parameters:
owningClass
-fieldName
-returnIsMandatory
- If true, aGraphQLRequestPreparationException
is thrown if the field is not found.- Returns:
- The class of the field. Or null of the field doesn't exist, and returnIdMandatory is false
- Throws:
GraphQLRequestPreparationException
-
getGraphQLType
public Class<?> getGraphQLType(AccessibleObject fieldOrMethod) throws GraphQLRequestPreparationException Returns the Class indicated as the value for the graphqlType attribute of the GraphQLScalar or GraphQLNonScalar annotation- Parameters:
fieldOrMethod
-- Returns:
- Throws:
GraphQLRequestPreparationException
-
checkFieldOfGraphQLType
public Class<?> checkFieldOfGraphQLType(String name, Boolean shouldBeScalar, Class<?> owningClass) throws GraphQLRequestPreparationException Check if the given field is owned by the class of thisObjectResponse
. This method returns the class for this field.- Parameters:
name
- The name of the field we want to checkshouldBeScalar
- if true: also checks that the field is a scalar (throws a GraphQLRequestPreparationException if not). If false: also checks that the field is not a scalar (throws a GraphQLRequestPreparationException if not). If null: no check whether the field is a scalar or notowningClass
- The class in which will search for name as a GraphQL field- Returns:
- the class of this field
- Throws:
NullPointerException
- if name is nullGraphQLRequestPreparationException
- if the check is KO
-
generatesBindVariableValuesMap
public Map<String,Object> generatesBindVariableValuesMap(Object[] paramsAndValues) throws GraphQLRequestExecutionException, ClassCastException This method retrieves the couple of name and values given in these parameters, stores them in a map where the key is the param name, and the value is the value of theMap
.- Parameters:
paramsAndValues
- A series of name and values : (paramName1, paramValue1, paramName2, paramValue2...). So there must be an even number of items in this array. Empty arrays are allowed (that is no parameter name and value).
This series is sent by the developer's code, when it calls the request methods.- Returns:
- The map with paramName1, paramName2 (...) are the keys, and paramValue1, paramValue2 (...) are the associated content.
- Throws:
GraphQLRequestExecutionException
- When a non-even number of parameters is sent to this methodClassCastException
- When a parameter name is not a String
-
parseValueForInputParameter
public Object parseValueForInputParameter(Object parameterValue, String parameterType, Class<?> parameterClass, String schema) Parse a value, depending on the parameter type.- Parameters:
parameterValue
-parameterType
-packageName
-schema
- value of the springBeanSuffix plugin parameter for the searched schema. When there is only one schema, this plugin parameter is usually not set. In this case, its default value ("") is used.- Returns:
- Throws:
RuntimeException
- When the value could be parsed
-
getGraphQLTypeNameFromClass
Retrieves the GraphQL type name (as defined in the GraphQL schema), from the GraphQL annotation added in the generated code by the plugin.- Parameters:
clazz
-- Returns:
-
getGraphQLCustomScalarType
public graphql.schema.GraphQLScalarType getGraphQLCustomScalarType(AccessibleObject fieldOrMethod, String schema) This method retrieves theGraphQLScalarType
for a custom scalar field or method.GraphQLScalar
is used in generated InputType and response type POJOs andGraphQLCustomScalar
is used in some legacy generated response type POJOs.- Parameters:
fieldOrMethod
- The field or method of the generated POJO classschema
- value of the springBeanSuffix plugin parameter for the searched schema. When there is only one schema, this plugin parameter is usually not set. In this case, its default value ("") is used.- Returns:
- the
GraphQLScalarType
-
getGraphQLScalarTypeFromName
public graphql.schema.GraphQLScalarType getGraphQLScalarTypeFromName(String typeName, String schema) Returns the GraphQL scalar type for the given Standard or Custom Scalar name, as defined in the GraphQL schema. TheGraphQLScalarType
contains the method that allows to parse a String value, parse an AST value, or serialize the value (for instance to write it into a JSON string)- Parameters:
typeName
-schema
- value of the springBeanSuffix plugin parameter for the searched schema. When there is only one schema, this plugin parameter is usually not set. In this case, its default value ("") is used.- Returns:
- The GraphQL type. Or null if not found (enum, object, input type, interface, union)
-