Quick Start - Java
This guide gets you started with SymphonyQL with a simple working example on Java.
Assuming we want to develop an application for the GraphQL Schema below:
schema {
query: Queries
}
enum Origin {
EARTH
MARS
BELT
}
type CharacterOutput {
name: String!
origin: Origin!
}
type Queries {
characters(origin: Origin): [CharacterOutput!]
}
SymphonyQL uses APT (Annotation Processing Tool) to automatically generate schema during compilation. Therefore, you only need to write record class to define the schema:
Defining Resolver
Resolver Object defined using @ObjectSchema
:
@ObjectSchema
record Queries(Function<FilterArgs, Source<CharacterOutput, NotUsed>> characters) {
}
FilterArgs
is the input andSource<CharacterOutput, NotUsed>>
is the output.Source
indicates that it is a Query/Subscription that returns pekko streams. For more types, please refer to the Schema Specification.
Defining Object
Object defined using @ObjectSchema
:
@ObjectSchema
record CharacterOutput(String name, Origin origin) {
}
Defining Input
Input defined using @InputSchema
and @ArgExtractor
:
@InputSchema
@ArgExtractor
record FilterArgs(Optional<Origin> origin, Optional<NestedArg> nestedArg) {
}
Defining Enum
Enum defined using @EnumSchema
, if the enumeration is input, you also need @ArgExtractor
:
@EnumSchema
@ArgExtractor
enum Origin {
EARTH,
MARS,
BELT
}
After writing the record class, we don't need to write graphql schemas to start developing the application.
Let's start developing the application now:
var graphql = SymphonyQL
.newSymphonyQL()
.addQuery(
new Queries(
args1 -> Source.single(new CharacterOutput("hello-" + args1.origin().map(Enum::toString).get(), args1.origin().get()))
),
QueriesSchema.schema
)
.build();
var characters = """
{
characters(origin: "BELT") {
name
origin
}
}""";
final var actorSystem = ActorSystem.create("symphonyActorSystem");
var getRes = graphql.run(
SymphonyQLRequest.newRequest().query(Optional.of(characters)).build(),
actorSystem
);
QueriesSchema.schema
is a static method automatically generated by APT.