Skip to content

Commit

Permalink
feat: handle graphql errors as thrown exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohammad-Dwairi committed Sep 4, 2024
1 parent ce2033b commit 076a8e7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package com.expediagroup.sdk.lodgingconnectivity.graphql

import com.apollographql.apollo3.ApolloClient
import com.apollographql.apollo3.annotations.ApolloExperimental
import com.apollographql.apollo3.api.ApolloResponse
import com.apollographql.apollo3.api.Mutation
import com.apollographql.apollo3.api.Query
import com.apollographql.apollo3.api.Subscription
Expand Down Expand Up @@ -58,21 +57,33 @@ class BaseGraphQLClient(config: ExpediaGroupClientConfiguration, private val nam
)
}

override fun <T : Query.Data> execute(query: Query<T>): ApolloResponse<T> {
override fun <T : Query.Data> execute(query: Query<T>): T {
return runBlocking {
return@runBlocking apolloClient.query(query).execute()
apolloClient.query(query).execute().apply {
if (hasErrors()) {
throw ExpediaGroupServiceException(errors.toString())
}
}.dataAssertNoErrors
}
}

override fun <T : Mutation.Data> execute(mutation: Mutation<T>): ApolloResponse<T> {
override fun <T : Mutation.Data> execute(mutation: Mutation<T>): T {
return runBlocking {
return@runBlocking apolloClient.mutation(mutation).execute()
apolloClient.mutation(mutation).execute().apply {
if (hasErrors()) {
throw ExpediaGroupServiceException(errors.toString())
}
}.dataAssertNoErrors
}
}

override fun <T : Subscription.Data> execute(subscription: Subscription<T>): ApolloResponse<T> {
override fun <T : Subscription.Data> execute(subscription: Subscription<T>): T {
return runBlocking {
return@runBlocking apolloClient.subscription(subscription).execute()
apolloClient.subscription(subscription).execute().apply {
if (hasErrors()) {
throw ExpediaGroupServiceException(errors.toString())
}
}.dataAssertNoErrors
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@

package com.expediagroup.sdk.lodgingconnectivity.graphql

import com.apollographql.apollo3.api.ApolloResponse
import com.apollographql.apollo3.api.Mutation
import com.apollographql.apollo3.api.Query
import com.apollographql.apollo3.api.Subscription

interface GraphQLExecutor {
fun <T : Query.Data> execute(query: Query<T>): ApolloResponse<T>
fun <T : Mutation.Data> execute(mutation: Mutation<T>): ApolloResponse<T>
fun <T : Subscription.Data> execute(subscription: Subscription<T>): ApolloResponse<T>
fun <T : Query.Data> execute(query: Query<T>): T
fun <T : Mutation.Data> execute(mutation: Mutation<T>): T
fun <T : Subscription.Data> execute(subscription: Subscription<T>): T
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,40 +58,38 @@ public static void main(String[] args) {
var createPropertyInput = CreatePropertyInput.builder().name(PROPERTY_NAME).build();
var createPropertyResponse = client.execute(new SandboxCreatePropertyMutation(createPropertyInput));

assert createPropertyResponse.data != null;

var propertyId = createPropertyResponse.data.createProperty.property.id;
var propertyId = createPropertyResponse.createProperty.property.id;

System.out.println("Property Created: " + propertyId);
System.out.println(createPropertyResponse.data);
System.out.println(createPropertyResponse);


// ******* Update Property Name *******
var updatePropertyInput = UpdatePropertyInput.builder().id(propertyId).name(UPDATED_PROPERTY_NAME).build();
var updatePropertyResponse = client.execute(new SandboxUpdatePropertyMutation(updatePropertyInput));

System.out.println("Property Updated: " + propertyId);
System.out.println(updatePropertyResponse.data);
System.out.println(updatePropertyResponse);


// ******* Create Reservation *******
var createReservationInput = CreateReservationInput.builder().propertyId(propertyId).childCount(4).adultCount(2).build();
var createReservationResponse = client.execute(new SandboxCreateReservationMutation(createReservationInput));

assert createReservationResponse.data != null;

var reservationId = createReservationResponse.data.createReservation.reservation.sandboxReservationFragment.id;
var reservationId = createReservationResponse.createReservation.reservation.sandboxReservationFragment.id;

System.out.println("Reservation Created: " + reservationId);
System.out.println(createReservationResponse.data);
System.out.println(createReservationResponse);


// ******* Update Reservation *******
var updateReservationInput = UpdateReservationInput.builder().id(reservationId).childAges(List.of(3, 5, 7)).build();
var updateReservationResponse = client.execute(new SandboxUpdateReservationMutation(updateReservationInput));

System.out.println("Reservation Updated: " + reservationId);
System.out.println(updateReservationResponse.data);
System.out.println(updateReservationResponse);


// ******* Update Reservation Stay Dates *******
Expand All @@ -105,40 +103,38 @@ public static void main(String[] args) {
var changeStayDatesResponse = client.execute(new SandboxChangeReservationStayDatesMutation(changeStayDatesInput));

System.out.println("Reservation Stay Dates Updated: " + reservationId);
System.out.println(changeStayDatesResponse.data);
System.out.println(changeStayDatesResponse);


// ******* Cancel Reservation *******
var cancelReservationInput = CancelReservationInput.builder().id(reservationId).sendNotification(false).build();
var cancelReservationResponse = client.execute(new SandboxCancelReservationMutation(cancelReservationInput));

System.out.println("Reservation Was Canceled: " + reservationId);
System.out.println(cancelReservationResponse.data);
System.out.println(cancelReservationResponse);


// ******* Delete Reservation *******
var deleteReservationInput = DeleteReservationInput.builder().id(reservationId).build();
var deleteReservationResponse = client.execute(new SandboxDeleteReservationMutation(deleteReservationInput));

System.out.println("Reservation Was Deleted: " + reservationId);
System.out.println(deleteReservationResponse.data);
System.out.println(deleteReservationResponse);


// ******* Delete Property *******
var deletePropertyInput = DeletePropertyInput.builder().id(propertyId).build();
var deletePropertyResponse = client.execute(new SandboxDeletePropertyMutation(deletePropertyInput));

System.out.println("Property Was Deleted: " + propertyId);
System.out.println(deletePropertyResponse.data);
System.out.println(deletePropertyResponse);
}

private static void deletePropertyIfExists() {
var propertiesQuery = SandboxPropertiesQuery.builder().skipReservations(true).build();
var propertiesResponse = client.execute(propertiesQuery);

assert propertiesResponse.data != null;

propertiesResponse.data.properties.elements.forEach(property -> {
propertiesResponse.properties.elements.forEach(property -> {
if (property.name.equals(PROPERTY_NAME) || property.name.equals(UPDATED_PROPERTY_NAME)) {
System.out.println("Deleting existing property: ID: " + property.id + ", Name: " + property.name);
var deletePropertyInput = DeletePropertyInput.builder().id(property.id).build();
Expand Down

0 comments on commit 076a8e7

Please sign in to comment.