This project is a generator for SmallRye GraphQL Clients used in Quarkus. It provides an annotation processor that generates an interface for a typesafe client including all model objects.
Additionally, it creates constants for names of all query, mutation and subscription operations and all model fields. This allows you to create use the dynamic client with those constants.
Add the following repository and dependency to your Maven project:
<project>
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<dependency>
<groupId>com.github.dan-osterrath.quarkus-smallrye-graphql-client-generator</groupId>
<artifactId>quarkus-smallrye-graphql-client-generator</artifactId>
<version>quarkus-smallrye-graphql-client-generator-parent-[VERSION]</version>
</dependency>
</project>
Put the GraphQL schema into your classpath, i.e. into src/main/resources/
. Alternatively, you can also fetch the
schema from a remote server, but this is not recommended as this may result in failing compilations when there are
connectivity issues.
Create an interface annotated with @GraphQLSchema
and provide a URI to the GraphQL schema:
@GraphQLSchema("resource:schema.graphql")
interface MyGraphQLService {
}
Optionally, you may add some dynamic GraphQL queries, that are validated at compile time:
@GraphQLSchema("resource:schema.graphql")
@GraphQLQuery(
identifier = "allUserNames",
value = "{ allUsers { name } }"
)
interface MyGraphQLService {
}
During compilation the annotation processor will parse this schema at compile time and creates a Java interface for the API and all models in the same package as the annotated interface.
@GraphQLClientApi(configKey = "MyGraphQLServiceApi")
@ApplicationScoped
@Generated(
value = "net.packsam.quarkus.graphql.client.generator.Generator",
date = "2022-12-16T11:11:27.192760200+01:00[Europe/Berlin]"
)
public interface MyGraphQLServiceApi {
String QUERY_ALLUSERS = "allUsers";
String ARGUMENT_ALLUSERS_FILTER = "filter";
List<User> allUsers(Filter filter);
class DynamicQueries {
public final static String ALL_USER_NAMES = "{ allUsers { name } }";
}
}
@Generated(
value = "net.packsam.quarkus.graphql.client.generator.Generator",
date = "2022-12-16T11:11:27.192760200+01:00[Europe/Berlin]"
)
public class User {
public static final String FIELD_ID = "id";
public static final String FIELD_NAME = "name";
private String id;
private String name;
// getters and setters...
}
@Generated(
value = "net.packsam.quarkus.graphql.client.generator.Generator",
date = "2022-12-16T11:11:27.192760200+01:00[Europe/Berlin]"
)
public class Filter {
public static final String FIELD_QUERY = "query";
public static final String FIELD_MAXRESULTS = "maxResults";
private String query;
private Integer maxResults;
// getters and setters...
}
Inject the generated interface into your application and use it as a typesafe client:
public class MyService {
@Inject
MyGraphQLServiceApi serviceApi;
public List<User> getAllUsers() {
return serviceApi.allUsers(null);
}
}
Configure the client in application.properties
:
quarkus.smallrye-graphql-client.MyGraphQLServiceApi.url=http://my-service/graphql
See the Quarkus documentation for more details.
Inject a DynamicGraphQLClient
into your application and annotate it with @GraphQLClientApi
:
public class MyService {
@Inject
@GraphQLClient("DynamicMyGraphQLServiceApi")
DynamicGraphQLClient client;
public List<User> getAllUsers() {
Response = client.executeSync(doucment(
operation(OperationType.Query,
field(MyGraphQLServiceApi.QUERY_ALLUSERS,
field(User.FIELD_ID),
field(User.FIELD_NAME)
)
)
));
return response.getList(User.class, MyGraphQLServiceApi.QUERY_ALLUSERS);
}
public List<String> getAllUserNames() {
Response = client.executeSync(MyGraphQLServiceApi.DynamicQueries.ALL_USER_NAMES);
return response.getList(User.class, MyGraphQLServiceApi.QUERY_ALLUSERS)
.stream()
.map(User::getName)
.collect(Collectors.toList());
}
}
Configure the client in application.properties
:
quarkus.smallrye-graphql-client.DynamicMyGraphQLServiceApi.url=http://my-service/graphql
See the Quarkus documentation for more details.
This project is inspired by the experimental SmallRye GraphQL client generator .