New utility method from the new annotations
@@ -17,8 +17,12 @@ import org.springframework.stereotype.Component;
import com.graphql_java_generator.GraphqlUtils;
import com.graphql_java_generator.annotation.GraphQLCustomScalar;
+import com.graphql_java_generator.annotation.GraphQLEnumType;
+import com.graphql_java_generator.annotation.GraphQLInterfaceType;
import com.graphql_java_generator.annotation.GraphQLNonScalar;
+import com.graphql_java_generator.annotation.GraphQLObjectType;
import com.graphql_java_generator.annotation.GraphQLScalar;
+import com.graphql_java_generator.annotation.GraphQLUnionType;
import com.graphql_java_generator.client.request.ObjectResponse;
import com.graphql_java_generator.customscalars.CustomScalarRegistryImpl;
import com.graphql_java_generator.exception.GraphQLRequestExecutionException;
@@ -32,6 +36,9 @@ import graphql.schema.GraphQLScalarType;
@Component
public class GraphqlClientUtils {
+ /** A singleton without Spring */
+ public static GraphqlClientUtils graphqlClientUtils = new GraphqlClientUtils();
+
Pattern graphqlNamePattern = Pattern.compile("^[_A-Za-z][_0-9A-Za-z]*$");
GraphqlUtils graphqlUtils = new GraphqlUtils();
@@ -314,11 +321,47 @@ public class GraphqlClientUtils {
}
/**
- * This method retrieves the {@link GraphQLScalarType} for a custom scalar field or method.
- * {@link GraphQLScalar} is used in generated InputType and response type POJOs and
- * {@link GraphQLCustomScalar} is used in some legacy generated response type POJOs.
+ * Retrieves the GraphQL type name (as defined in the GraphQL schema), from the GraphQL annotation added in the
+ * generated code by the plugin.
*
- * @param fieldOrMethod The field or method of the generated POJO class
+ * @param clazz
+ * @return
+ */
+ public String getGraphQLTypeNameFromClass(Class<?> clazz) {
+ // Object
+ GraphQLObjectType graphQLObjectType = clazz.getAnnotation(GraphQLObjectType.class);
+ if (graphQLObjectType != null) {
+ return graphQLObjectType.value();
+ }
+
+ // Interface
+ GraphQLInterfaceType graphQLInterfaceType = clazz.getAnnotation(GraphQLInterfaceType.class);
+ if (graphQLInterfaceType != null) {
+ return graphQLInterfaceType.value();
+ }
+
+ // Union
+ GraphQLUnionType graphQLUnionType = clazz.getAnnotation(GraphQLUnionType.class);
+ if (graphQLUnionType != null) {
+ return graphQLUnionType.value();
+ }
+
+ // Enum
+ GraphQLEnumType graphQLEnumType = clazz.getAnnotation(GraphQLEnumType.class);
+ if (graphQLEnumType != null) {
+ return graphQLEnumType.value();
+ }
+
+ throw new RuntimeException("Could not find the GraphQL type for the class " + clazz.getName());
+ }
+
+ /**
+ * This method retrieves the {@link GraphQLScalarType} for a custom scalar field or method. {@link GraphQLScalar} is
+ * used in generated InputType and response type POJOs and {@link GraphQLCustomScalar} is used in some legacy
+ * generated response type POJOs.
+ *
+ * @param fieldOrMethod
+ * The field or method of the generated POJO class
* @return the {@link GraphQLScalarType}
*/
public GraphQLScalarType getGraphQLCustomScalarType(AccessibleObject fieldOrMethod) {
@@ -337,5 +380,4 @@ public class GraphqlClientUtils {
}
}
-
}
\ No newline at end of file
