Page tree
Skip to end of metadata
Go to start of metadata

If you need to retrieve two spaces, for example, then it won't work for both to be returned with the name "space". This is where GraphQL allows you to define an alilas that the object should be returned as. This causes two challenges, firstly for creating the query, secondly for parsing the response.

The Alilased ObjectDataSenderBuilder

There are only two differences in the ObjectDataSenderBuilders for building what fields or child objects the query should retrieve.

  1. For a normal query, the setObjectName() method uses an enum, e.g. Space.ONE_SPACE_QUERY_OBJECT_NAME which maps to "space". For aliased queries, the setObjectName() method takes the alias name you wish to use for the return object, e.g. "space1".
  2. The API needs some way to know what class to cast the return object to. This is done with a setReturnType() method of the ObjectDataSenderBuilder. To use this, there is a new enum - WWQueryResponseObjectTypes - which allows the API to know what to cast the alias to in the query and what Java class to convert the return object to.

The Multiple Query

Normal queries based on the BaseGraphQLQuery object can only take a single ObjectDataSenderBuilder, although that ObjectDataSenderBuilder may take other ObjectDataSenderBuilders as children. Aliased queries need to take more than one ObjectDataSenderBuilder at the top level.This is done with the BaseGraphQLMultiQuery. Instead of passing a single ObjectDataSenderBuilder into the constructor, you can pass either a single ObjectDataSenderBuilder to the constructor or a List of ObjectDataSenderBuilders. There are also methods for adding and removing ObjectDataSenderBuilders.

Parsing the Response

When aliases are used, the DataContainer cannot auto-convert using Gson. Instead DataContainer.getAliasedChildren() returns a Map of any aliases. The returnType property o the ObjectDataSenderBuilder is used to know what class to convert the response to.

Examples

getTwoSpaces
ObjectDataSenderBuilder query = new ObjectDataSenderBuilder();
query.setReturnType(WWQueryResponseObjectTypes.SPACE);
query.setObjectName("space1");
query.addAttribute(SpaceFields.ID, spaceId);
query.addField(SpaceFields.TITLE);
query.addField(SpaceFields.DESCRIPTION);
query.addField(SpaceFields.ID);
query.addField(SpaceFields.CREATED);
query.addField(SpaceFields.UPDATED);
BaseGraphQLMultiQuery twoSpaceQuery = new BaseGraphQLMultiQuery("getTwoSpaces", query);
ObjectDataSenderBuilder query2 = new ObjectDataSenderBuilder();
query2.setObjectName("space2");
query2.setReturnType(WWQueryResponseObjectTypes.SPACE);
query2.addAttribute(SpaceFields.ID, space2Id);
query2.addField(SpaceFields.TITLE);
query2.addField(SpaceFields.DESCRIPTION);
query2.addField(SpaceFields.ID);
query2.addField(SpaceFields.CREATED);
query2.addField(SpaceFields.UPDATED);
twoSpaceQuery.addQueryObject(query2);
ep.setRequest(new GraphQLRequest(twoSpaceQuery));
ep.executeRequest();
        
DataContainer data = ep.getResultContainer().getData();
assert (null != data);
SpaceWrapper space1 = (SpaceWrapper) data.getAliasedChildren().get("space1");
assert(null != space1);
assert (spaceName.equals(space1.getTitle()));
SpaceWrapper space2 = (SpaceWrapper) data.getAliasedChildren().get("space2");
assert(null != space2);
assert (space2Name.equals(space2.getTitle()));
getTwoSpacesFromClient
ObjectDataSenderBuilder query = new ObjectDataSenderBuilder();
query.setReturnType(WWQueryResponseObjectTypes.SPACE);
query.setObjectName("space1");
query.addAttribute(SpaceFields.ID, spaceId);
query.addField(SpaceFields.TITLE);
query.addField(SpaceFields.DESCRIPTION);
query.addField(SpaceFields.ID);
query.addField(SpaceFields.CREATED);
query.addField(SpaceFields.UPDATED);
BaseGraphQLMultiQuery twoSpaceQuery = new BaseGraphQLMultiQuery("getTwoSpaces", query);
ObjectDataSenderBuilder query2 = new ObjectDataSenderBuilder();
query2.setObjectName("space2");
query2.setReturnType(WWQueryResponseObjectTypes.SPACE);
query2.addAttribute(SpaceFields.ID, space2Id);
query2.addField(SpaceFields.TITLE);
query2.addField(SpaceFields.DESCRIPTION);
query2.addField(SpaceFields.ID);
query2.addField(SpaceFields.CREATED);
query2.addField(SpaceFields.UPDATED);
twoSpaceQuery.addQueryObject(query2);
GraphResultContainer resultContainer = client.getCustomQuery(twoSpaceQuery);
        
DataContainer data = resultContainer.getData();
assert (null != data);
SpaceWrapper space1 = (SpaceWrapper) data.getAliasedChildren().get("space1");
assert(null != space1);
assert (spaceName.equals(space1.getTitle()));
SpaceWrapper space2 = (SpaceWrapper) data.getAliasedChildren().get("space2");
assert(null != space2);
assert (space2Name.equals(space2.getTitle()));
Write a comment…