Class AbstractGraphQLRequest
java.lang.Object
com.graphql_java_generator.client.request.AbstractGraphQLRequest
- Direct Known Subclasses:
ObjectResponse
This class contains the description for a GraphQL request that will be sent to the server. It's an abstract class,
and can not be use directly: a concrete class is generated by the plugin, when in client mode. This concrete class
provides all the necessary context to this abstract class for it to work properly.
This class stores:
This class stores:
- The query part, if any
- The mutation part, if any
- The subscription part, if any
- The fragments, if any
- Author:
- etienne-sf
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
This class contains the payload that will be sent to the server. -
Field Summary
Modifier and TypeFieldDescriptionprotected final String
The package name, where the GraphQL generated classes are. -
Constructor Summary
ConstructorDescriptionAbstractGraphQLRequest
(org.springframework.graphql.client.GraphQlClient graphQlClient, String schema, String graphQLRequest) Creates the GraphQL request, for a full request.AbstractGraphQLRequest
(org.springframework.graphql.client.GraphQlClient graphQlClient, String schema, String graphQLRequest, RequestType requestType, String fieldName, InputParameter... inputParams) Create the instance for the given GraphQL request, for a partial request or a full request.
Important note: this constructor SHOULD be used only by the code generated by the plugin, not by external applications. -
Method Summary
Modifier and TypeMethodDescription<R extends GraphQLRequestObject>
RThis method executes the current GraphQL as a query or mutation GraphQL request, and return its response mapped in the relevant POJO.<R extends GraphQLRequestObject,
T>
SubscriptionClientexec
(Map<String, Object> params, SubscriptionCallback<T> subscriptionCallback, Class<R> subscriptionType, Class<T> messageType) Execution of the given subscription GraphQL request, and return its response mapped in the relevant POJO.<R extends GraphQLRequestObject>
reactor.core.publisher.Mono<R>execReactive
(Class<R> r, Map<String, Object> params) This method executes the current GraphQL as a query or mutation GraphQL request, and return a reactiveMono
that will contain the response mapped in the relevant POJO.<R extends GraphQLRequestObject>
reactor.core.publisher.Flux<R>execReactive
(Map<String, Object> params, Class<R> subscriptionType) Execution of the given subscription GraphQL request, in reactive mode.protected abstract String
This method returns the package name, where the GraphQL generated classes are.This method creates and configures a JacksonObjectMapper
that is ready to parse the response for this GraphQL.Retrieve the request String that has been provided to create this instanceprotected abstract QueryField
Retrieved theQueryField
for the mutation (that is the mutation type coming from the GraphQL schema) from the concrete class.getPayload
(Map<String, Object> params) Returns the payload for this request.getQuery()
protected abstract QueryField
Retrieved theQueryField
for the query (that is the query type coming from the GraphQL schema) from the concrete class.protected abstract Class<? extends GraphQLRequestObject>
Returns the subscription class for this schemaprotected abstract QueryField
Retrieved theQueryField
for the subscription (that is the subscription type coming from the GraphQL schema) from the concrete class.protected static void
logExecution
(RequestType requestType, String queryMutationTypeName, Map<String, Object> parameters)
-
Field Details
-
packageName
The package name, where the GraphQL generated classes are. It's used to load the class definition, and get the GraphQL metadata coming from the GraphQL schema.
-
-
Constructor Details
-
AbstractGraphQLRequest
public AbstractGraphQLRequest(org.springframework.graphql.client.GraphQlClient graphQlClient, String schema, String graphQLRequest, RequestType requestType, String fieldName, InputParameter... inputParams) throws GraphQLRequestPreparationException Create the instance for the given GraphQL request, for a partial request or a full request.
Important note: this constructor SHOULD be used only by the code generated by the plugin, not by external applications. Its signature may change in the future. To prepare Partial Requests, application code SHOULD call the getXxxxGraphQLRequests methods, that are generated in the query/mutation/subscription java classes.- Parameters:
graphQlClient
- TheGraphQlClient
that is responsible for the actual execution of the requestschema
- 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.graphQLRequest
- The partial GraphQL request, in text format. Writing partial request allows use to execute a query/mutation/subscription, and only define what's expected as a response for this query/mutation/subscription. You can send the parameters for this query/mutation/subscription as parameter of the java method, without dealing with bind variable in the GraphQL query. Please read the client doc page for more information, including hints and limitations.requestType
- The information whether this queryName is actually a query, a mutation or a subscriptionfieldName
- The name of the query, mutation or subscription, for instance "createHuman", in the GraphQL request "mutation {createHuman (...) { ...}}".inputParams
- The list of input parameters for this query/mutation/subscription- Throws:
GraphQLRequestPreparationException
-
AbstractGraphQLRequest
public AbstractGraphQLRequest(org.springframework.graphql.client.GraphQlClient graphQlClient, String schema, String graphQLRequest) throws GraphQLRequestPreparationException Creates the GraphQL request, for a full request. It will:- Read the query and/or the mutation
- Read all fragment definitions
- For all non scalar field, subfields (and so on recursively), if they are empty (that is the query doesn't define the requested fields of a non scalar field, then all its scalar fields are added)
- Add the introspection __typename field to all scalar field list, if it doesnt't already exist. This is necessary to allow proper deserialization of interfaces and unions.
- Parameters:
graphQlClient
- TheGraphQlClient
that is responsible for the actual execution of the requestschema
- 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.graphQLRequest
- The GraphQL request, in text format, as defined in the GraphQL specifications, and as it can be used in GraphiQL. Please read the client doc page for more information, including hints and limitations.- Throws:
GraphQLRequestPreparationException
-
-
Method Details
-
execReactive
public <R extends GraphQLRequestObject> reactor.core.publisher.Mono<R> execReactive(Class<R> r, Map<String, Object> params) throws GraphQLRequestExecutionExceptionThis method executes the current GraphQL as a query or mutation GraphQL request, and return a reactiveMono
that will contain the response mapped in the relevant POJO. This method executes a partial GraphQL query, or a full GraphQL request.
It should only be called from- Type Parameters:
R
-- Parameters:
r
- The type of the POJO which should be returned. As we're executing a query or a mutation, here, it must be a query or mutation class, generated from the GraphQL subscription definition in the provided schema, by the pluginparams
-- Returns:
- A Mono the returns the result of the GraphQL request, maps on the relevant POJO
- Throws:
GraphQLRequestExecutionException
-
exec
public <R extends GraphQLRequestObject> R exec(Class<R> r, Map<String, Object> params) throws GraphQLRequestExecutionExceptionThis method executes the current GraphQL as a query or mutation GraphQL request, and return its response mapped in the relevant POJO. This method executes a partial GraphQL query, or a full GraphQL request.- Type Parameters:
R
-- Parameters:
r
- The type of the POJO which should be returned. It must be the query or the mutation class, generated by the pluginparams
-- Returns:
- Throws:
GraphQLRequestExecutionException
-
execReactive
public <R extends GraphQLRequestObject> reactor.core.publisher.Flux<R> execReactive(Map<String, Object> params, Class<R> subscriptionType) throws GraphQLRequestExecutionExceptionExecution of the given subscription GraphQL request, in reactive mode. This method returns aFlux
of the relevant POJO. This method executes a partial GraphQL query, or a full GraphQL request.- Type Parameters:
T
- The type that must be returned by the subscription in the GraphQL schema, which is actually the type that will be sent in theFlux
returned by this subscription.- Parameters:
params
- the input parameters for this query. If the query has no parameters, it may be null or an empty list.messageType
- The T class- Returns:
- The
Flux
of the relevant POJO (T class), depending on the requested subscription.
Please note thatFlux
may not propagate null values. So the returned POJO is embedded into anOptional
, so that null values can be returned by the subscriptions. The POJO is always embedded in anOptional
. This make your code homogeneous between subscriptions. And your code won't have to be changed if the GraphQL subscription definition switch from mandatory to not mandatory. - Throws:
GraphQLRequestExecutionException
- The Flux will throw this exception when an error occurs during the request execution, typically if the request is invalid, if there is a network error, an error from the GraphQL server or if the server response can't be parsed.IOException
-
exec
public <R extends GraphQLRequestObject,T> SubscriptionClient exec(Map<String, Object> params, SubscriptionCallback<T> subscriptionCallback, Class<R> subscriptionType, Class<T> messageType) throws GraphQLRequestExecutionExceptionExecution of the given subscription GraphQL request, and return its response mapped in the relevant POJO. This method executes a partial GraphQL query, or a full GraphQL request.
Note: Don't forget to free the server's resources by calling theWebSocketClient#stop()
method of the returned object.- Type Parameters:
R
- The class that is generated from the subscription definition in the GraphQL schema. It contains one attribute, for each available subscription. The data tag of the GraphQL server response will be mapped into an instance of this class.T
- The type that must is returned by the subscription in the GraphQL schema, which is actually the type that will be sent in each notification received from this subscription.- Parameters:
t
- The type of the POJO which should be returned. It must be the query or the mutation class, generated by the pluginparams
- the input parameters for this query. If the query has no parameters, it may be null or an empty list.subscriptionCallback
- The object that will be called each time a message is received, or an error on the subscription occurs. This object is provided by the application.subscriptionName
- The name of the subscription that should be subscribed by this method call. It will be used to check that the correct GraphQLRequest has been provided by the caller.subscriptionType
- The R classmessageType
- The T class- Returns:
- The Subscription client. It allows to stop the subscription, by executing its
SubscriptionClient.unsubscribe()
method. This will stop the incoming notification flow, and will free resources on both the client and the server. - Throws:
GraphQLRequestExecutionException
- When an error occurs during the request execution, typically a network error, an error from the GraphQL server or if the server response can't be parsedIOException
-
getPayload
public AbstractGraphQLRequest.Payload getPayload(Map<String, Object> params) throws GraphQLRequestExecutionExceptionReturns the payload for this request. This payload can then be serialized, according to the transport protocol used.- Parameters:
params
-- Returns:
- Throws:
GraphQLRequestExecutionException
-
getGraphQLObjectMapper
This method creates and configures a JacksonObjectMapper
that is ready to parse the response for this GraphQL. The main configuration is the management for the GraphQL aliases.- Returns:
-
getGraphQLRequest
Retrieve the request String that has been provided to create this instance- Returns:
-
getGraphQLClassesPackageName
This method returns the package name, where the GraphQL generated classes are. It's used to load the class definition, and get the GraphQL metadata coming from the GraphQL schema.- Returns:
-
getQueryContext
Retrieved theQueryField
for the query (that is the query type coming from the GraphQL schema) from the concrete class.- Returns:
- Throws:
GraphQLRequestPreparationException
-
getMutationContext
Retrieved theQueryField
for the mutation (that is the mutation type coming from the GraphQL schema) from the concrete class.- Returns:
- Throws:
GraphQLRequestPreparationException
-
getSubscriptionClass
Returns the subscription class for this schema -
getSubscriptionContext
Retrieved theQueryField
for the subscription (that is the subscription type coming from the GraphQL schema) from the concrete class.- Returns:
- Throws:
GraphQLRequestPreparationException
-
getQuery
-
getMutation
-
getSubscription
-
getFragments
-
getRequestType
-
getRequestName
-
logExecution
protected static void logExecution(RequestType requestType, String queryMutationTypeName, Map<String, Object> parameters) - Parameters:
executionOf
- A stringparameters
-
-