This is just the core for GraphQL clients by brix. You will need a client for an actual product to be able to use it.
A general explanation how the GraphQL clients based on this one are working.
In the base package you can find the class Client (take this one and not its parent) which can be instantiated. Constructor arguments:
- String endpoint: the endpoint for the graphQL requests
- (optional) the OkHttpClient
Authorization is done by passing an OkHttpClient which handles it. E.g.
okHttpClient = new OkHttpClient.Builder()
.addInterceptor(chain -> chain.proceed(chain.request().newBuilder()
.addHeader("Authorization", "Bearer " + token).build()))
.build();In the base package are two classes Query and Mutation with static methods to create calls (actually callBuilders, but they don't require a build() when finished).
- Use one of the static methods on the
QueryorMutationclass to get a call builder. - The call builder has the following methods:
args(ArgBuilder argBuilder)if there are any arguments, there will always only be one matching ArgBuilder of that type, so there should be no confusion. An ArgBuilder has a method to set each argument, the method expects a specific scalar or an input object for which there are builders.onType(OnTypeBuilder onTypeBuilder)for each possible return type which has fields (so no unions, only their implementations, but interfaces can be used to request common fields of types, so they don't have to be listed for every type). As before the method expects a typed onTypeBuilder of which there is only one. "on" is to select the fields which should be returned, but the fields can be calls (with arguments) themselves which return objects and so on (this is how graphQL do). This means the onTypeBuilder has methods for every field on this type and (unless the field is not scalar or enum and has no arguments) it requires another call builder of which there will be only one. E.g.
Basically the same thing like when we would write the query manually, except that everything is done almost automatically because of the IDE suggestions and the javadoc including default values and deprecation notes helps too.Query.workspaceProject() .args(new RootQuery_workspaceProject_Args() .id(IdScalar.of("123"))) .onWorkspace(new On_Workspace() .id() .assets(new Workspace_assets() .args(new Workspace_assets_Args()) // just using default arg values .onAssetItems(new On_AssetItems() .total() .items(new AssetItems_items() .onAsset(new On_Asset() .id() .title() .description()) .onImage(new On_Image() .focalPoint())))));
- The callBuilder created in 2. can be executed with the client, it knows the exact return type through generics and returns an object of that type, e.g.
Workspace workspace = client.execute(queryFromAbove); List<Asset> assets = workspace.getAssets().getItems(); assets.forEach(asset -> { if (asset instanceof Image) System.out.println(((Image) asset).getFocalPoint().getValue()); ... })
- 1.4: added ThrottledClient