Search in sources :

Example 1 with ObjectResponse

use of com.graphql_java_generator.client.request.ObjectResponse in project graphql-maven-plugin-project by graphql-java-generator.

the class SubscriptionTypeExecutor method newCharacterWithBindValues.

/**
 * This method registers a subscription, by executing a direct partial request against the GraphQL server. This
 * subscription is one of the fields defined in the GraphQL subscription object. The queryResponseDef contains the
 * part of the subscription that <B><U>is after</U></B> the subscription name (see the sample below), for instance
 * "{id name}" if you want these two fields to be sent in the notifications you'll receive for this
 * subscription.<BR/>
 * You must also provide a callback instance of the {@link SubscriptionCallback}, and the parameter for the
 * subscription as parameter for this method. For instance, if the subscription subscribeToNewPost has one parameter
 * <I>boardName</I> (as defined in the GraphQL schema):
 *
 * <PRE>
 * SubscriptionClient client;
 *
 * void setup() {
 * 	subscriptionType = new SubscriptionType("http://localhost:8180/graphql/subscription");
 * }
 *
 * void exec() {
 * 	Map<String, Object> params = new HashMap<>();
 * 	params.put("anOptionalParam", "a param value");
 * 	// PostSubscriptionCallback implement SubscriptionCallback<Post>, as Post is the returned type for the
 * 	// subscribeToNewPost subscription. Its onMessage(T) method will be called for each notification of this
 * 	// subscription.
 * 	client = subscriptionType.subscribeToNewPost(
 * 			"{id date author publiclyAvailable title(param: ?anOptionalParam) content}",
 * 			new PostSubscriptionCallback(), "Board name 1", // The parameter(s) of the subscription if any, are
 * 															// directly sent as parameter for this method
 * 			params // The bind variable you defined in your query are in this map.
 * 	);
 * }
 *
 * void freeResources() {
 * 	client.unsubscribe();
 * }
 * </PRE>
 *
 * @param queryResponseDef
 *            The response definition of the subscription, in the native GraphQL format (see here above)
 * @param subscriptionCallback
 *            An instance of SubscriptionCallback<Character>. Its {@link SubscriptionCallback#onMessage(Object)}
 *            will be called for each notification received from this subscription.
 * @param parameters
 *            The list of values, for the bind variables defined in the subscription. If there is no bind variable
 *            in the defined subscription, this argument may be null or an empty {@link Map}
 * @throws GraphQLRequestPreparationException
 *             When an error occurs during the request preparation, typically when building the
 *             {@link ObjectResponse}
 * @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 parsed
 */
public SubscriptionClient newCharacterWithBindValues(String queryResponseDef, SubscriptionCallback<Character> subscriptionCallback, Map<String, Object> parameters) throws GraphQLRequestExecutionException, GraphQLRequestPreparationException {
    logger.debug("Executing subscription 'newCharacter': {} ", queryResponseDef);
    ObjectResponse objectResponse = getNewCharacterResponseBuilder().withQueryResponseDef(queryResponseDef).build();
    return newCharacter(objectResponse, subscriptionCallback, parameters);
}
Also used : ObjectResponse(com.graphql_java_generator.client.request.ObjectResponse)

Example 2 with ObjectResponse

use of com.graphql_java_generator.client.request.ObjectResponse in project graphql-maven-plugin-project by graphql-java-generator.

the class QueryExecutorImpl_StarWars_Test method test_buildRequest_Episode_idNameAppearsInFriendsName.

/**
 * Build a request with one parameter (ID), and a {@link Character} as the response.
 *
 * @throws GraphQLRequestPreparationException
 * @throws GraphQLRequestExecutionException
 */
@Test
void test_buildRequest_Episode_idNameAppearsInFriendsName() throws GraphQLRequestPreparationException, GraphQLRequestExecutionException {
    // Preparation
    Map<String, Object> parameters = new HashMap<>();
    parameters.put("queryTypeHeroEpisode", Episode.NEWHOPE);
    // The response should contain id and name
    ObjectResponse objectResponse = queryType.getHeroResponseBuilder().withQueryResponseDef("{ id\nname\rappearsIn\tfriends{name}}").build();
    // Go, go, go
    String request = objectResponse.buildRequestAsString(parameters);
    // Verification
    assertEquals("{\"query\":\"query{hero(episode:NEWHOPE){id name appearsIn friends{name __typename} __typename}}\"}", request);
}
Also used : HashMap(java.util.HashMap) ObjectResponse(com.graphql_java_generator.client.request.ObjectResponse) Test(org.junit.jupiter.api.Test)

Example 3 with ObjectResponse

use of com.graphql_java_generator.client.request.ObjectResponse in project graphql-maven-plugin-project by graphql-java-generator.

the class QueryExecutorImpl_StarWars_Test method test_buildRequest_Episode_idNameAppearsInFriendsName_nullMap.

/**
 * Build a request with one parameter (ID), and a {@link Character} as the response.
 *
 * @throws GraphQLRequestPreparationException
 * @throws GraphQLRequestExecutionException
 */
@Test
void test_buildRequest_Episode_idNameAppearsInFriendsName_nullMap() throws GraphQLRequestPreparationException, GraphQLRequestExecutionException {
    // The response should contain id and name
    ObjectResponse objectResponse = queryType.getHeroResponseBuilder().withQueryResponseDef("\t\n\r {\t\n\r id\t\n\r name\t\n\r appearsIn\t\n\r friends\t\n\r {\t\n\r name\t\n\r }\t\n\r }\t\n\r ").build();
    // Go, go, go
    // No map given (null instead)
    String request = objectResponse.buildRequestAsString(null);
    // Verification
    assertEquals("{\"query\":\"query{hero{id name appearsIn friends{name __typename} __typename}}\"}", request);
}
Also used : ObjectResponse(com.graphql_java_generator.client.request.ObjectResponse) Test(org.junit.jupiter.api.Test)

Example 4 with ObjectResponse

use of com.graphql_java_generator.client.request.ObjectResponse in project graphql-maven-plugin-project by graphql-java-generator.

the class QueryExecutorImpl_StarWars_Test method test_parseResponse_OK_noFriends.

@Test
void test_parseResponse_OK_noFriends() throws GraphQLResponseParseException, IOException, GraphQLRequestPreparationException {
    // Preparation
    List<InputParameter> parameters = new ArrayList<>();
    parameters.add(InputParameter.newHardCodedParameter("", "episode", Episode.NEWHOPE, "Episode", false, 0, false));
    // The response should contain id and name
    ObjectResponse objectResponse = queryType.getHeroResponseBuilder().withQueryResponseDef(" { id appearsIn friends { name } } ").build();
    String rawResponse = "{\"data\":{\"hero\":{\"id\":\"An id\",\"name\":\"A hero's name\",\"appearsIn\":[\"NEWHOPE\",\"JEDI\"],\"friends\":null, \"__typename\": \"Human\"}}}";
    // Go, go, go
    Object response = parseResponseForStarWarsSchema(rawResponse, objectResponse, QueryType.class);
    // Verification
    assertTrue(response instanceof QueryType, "response instanceof QueryTypeHero");
    Character character = ((QueryType) response).getHero();
    assertTrue(character instanceof Human, "character instanceof QueryTypeHero");
    assertEquals(Human.class.getName(), character.getClass().getName());
    assertEquals("An id", character.getId(), "id");
    assertEquals("A hero's name", character.getName(), "name");
    List<Episode> episodes = character.getAppearsIn();
    assertEquals(2, episodes.size(), "2 episodes");
    assertEquals(Episode.NEWHOPE, episodes.get(0), "First episode");
    assertEquals(Episode.JEDI, episodes.get(1), "Second episode");
    assertNull(character.getFriends(), "He has no friends!  :(  ");
}
Also used : Human(com.graphql_java_generator.domain.client.starwars.Human) Episode(com.graphql_java_generator.domain.client.starwars.Episode) Character(com.graphql_java_generator.domain.client.starwars.Character) ArrayList(java.util.ArrayList) ObjectResponse(com.graphql_java_generator.client.request.ObjectResponse) QueryType(com.graphql_java_generator.domain.client.starwars.QueryType) InputParameter(com.graphql_java_generator.client.request.InputParameter) Test(org.junit.jupiter.api.Test)

Example 5 with ObjectResponse

use of com.graphql_java_generator.client.request.ObjectResponse in project graphql-maven-plugin-project by graphql-java-generator.

the class QueryExecutorImpl_allGraphqlCases_Test method buildRequest_withEnum_withDirectives_prepared.

@Test
void buildRequest_withEnum_withDirectives_prepared() throws GraphQLRequestExecutionException, GraphQLRequestPreparationException, JsonProcessingException {
    // Preparation
    String query = "{\n" + "    id     @anotherTestDirective    @testDirective  ( value: \"id1 value\", anotherValue:\" something else for id1 \")\n" + // 
    "    name\n" + "    appearsIn @testDirective(value: \"a value2\", anotherValue:\"something else2\")\n" + // 
    "    friends {\n" + "      id @anotherTestDirective\n" + "      name @testDirective(value: \"a value3\", anotherValue:\"something_else3\") @anotherTestDirective\n" + // 
    "    }\n" + "}\n";
    Map<String, Object> parameters = new HashMap<>();
    parameters.put("myQueryTypeWithEnumEpisode", Episode.JEDI);
    ObjectResponse objectResponse = myQueryType.getWithEnumResponseBuilder().withQueryResponseDef(query).build();
    // Go, go, go
    String request = objectResponse.buildRequestAsString(parameters);
    // Verification
    assertEquals(// 
    "{\"query\":\"query{withEnum(episode:JEDI)" + "{id @anotherTestDirective @testDirective(value:\\\"id1 value\\\",anotherValue:\\\" something else for id1 \\\") " + // 
    "name " + "appearsIn @testDirective(value:\\\"a value2\\\",anotherValue:\\\"something else2\\\") " + // 
    "friends{id @anotherTestDirective name @testDirective(value:\\\"a value3\\\",anotherValue:\\\"something_else3\\\") @anotherTestDirective " + // 
    "__typename} __typename}}\"" + "}", request);
    // Go, go, go
    Map<String, Object> map = objectResponse.buildRequestAsMap(parameters);
    // Verification
    checkRequestMap(map, // 
    "query{withEnum(episode:JEDI)" + "{id @anotherTestDirective @testDirective(value:\"id1 value\",anotherValue:\" something else for id1 \") " + // 
    "name " + "appearsIn @testDirective(value:\"a value2\",anotherValue:\"something else2\") " + // 
    "friends{id @anotherTestDirective name @testDirective(value:\"a value3\",anotherValue:\"something_else3\") @anotherTestDirective " + "__typename} __typename}}", null, null);
}
Also used : HashMap(java.util.HashMap) ObjectResponse(com.graphql_java_generator.client.request.ObjectResponse) Test(org.junit.jupiter.api.Test)

Aggregations

ObjectResponse (com.graphql_java_generator.client.request.ObjectResponse)146 GraphQLNonScalar (com.graphql_java_generator.annotation.GraphQLNonScalar)74 GraphQLScalar (com.graphql_java_generator.annotation.GraphQLScalar)26 Test (org.junit.jupiter.api.Test)17 HashMap (java.util.HashMap)9 Character (com.graphql_java_generator.domain.client.starwars.Character)4 Builder (com.graphql_java_generator.client.request.Builder)3 InputParameter (com.graphql_java_generator.client.request.InputParameter)3 QueryType (com.graphql_java_generator.domain.client.starwars.QueryType)3 ArrayList (java.util.ArrayList)3 Droid (com.graphql_java_generator.domain.client.starwars.Droid)2 Human (com.graphql_java_generator.domain.client.starwars.Human)2 GraphQLRequestExecutionException (com.graphql_java_generator.exception.GraphQLRequestExecutionException)2 Disabled (org.junit.jupiter.api.Disabled)2 JsonParseException (com.fasterxml.jackson.core.JsonParseException)1 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 InvalidFormatException (com.fasterxml.jackson.databind.exc.InvalidFormatException)1 UnrecognizedPropertyException (com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException)1 Board (com.graphql_java_generator.domain.client.forum.Board)1 Post (com.graphql_java_generator.domain.client.forum.Post)1