sidebar_position | title | date | description |
---|---|---|---|
1 |
Getting started |
2018-09-09 12:52:46 +1000 |
GraphQL basics. Start here if you're new to GraphQL :-) |
import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem';
graphql-java
requires at least Java 11.
Make sure mavenCentral
is among your repos:
repositories {
mavenCentral()
}
dependencies {
implementation 'com.graphql-java:graphql-java:22.3'
}
repositories {
mavenCentral()
}
dependencies {
implementation("com.graphql-java:graphql-java:22.3")
}
Dependency:
<dependency>
<groupId>com.graphql-java</groupId>
<artifactId>graphql-java</artifactId>
<version>22.3</version>
</dependency>
This is the famous "hello world" in graphql-java
:
import graphql.ExecutionResult;
import graphql.GraphQL;
import graphql.schema.GraphQLSchema;
import graphql.schema.StaticDataFetcher;
import graphql.schema.idl.RuntimeWiring;
import graphql.schema.idl.SchemaGenerator;
import graphql.schema.idl.SchemaParser;
import graphql.schema.idl.TypeDefinitionRegistry;
import static graphql.schema.idl.RuntimeWiring.newRuntimeWiring;
public class HelloWorld {
public static void main(String[] args) {
String schema = "type Query{hello: String}";
SchemaParser schemaParser = new SchemaParser();
TypeDefinitionRegistry typeDefinitionRegistry = schemaParser.parse(schema);
RuntimeWiring runtimeWiring = newRuntimeWiring()
.type("Query", builder -> builder.dataFetcher("hello", new StaticDataFetcher("world")))
.build();
SchemaGenerator schemaGenerator = new SchemaGenerator();
GraphQLSchema graphQLSchema = schemaGenerator.makeExecutableSchema(typeDefinitionRegistry, runtimeWiring);
GraphQL build = GraphQL.newGraphQL(graphQLSchema).build();
ExecutionResult executionResult = build.execute("{hello}");
System.out.println(executionResult.getData().toString());
// Prints: {hello=world}
}
}
The latest development build is available on Maven Central.
Please look at the latest build for the latest version value.
Add the repository and dependency:
repositories {
mavenCentral()
}
dependencies {
implementation 'com.graphql-java:graphql-java:INSERT_LATEST_VERSION_HERE'
}
repositories {
mavenCentral()
}
dependencies {
implementation("com.graphql-java:graphql-java:INSERT_LATEST_VERSION_HERE")
}
Dependency:
<dependency>
<groupId>com.graphql-java</groupId>
<artifactId>graphql-java</artifactId>
<version>INSERT_LATEST_VERSION_HERE</version>
</dependency>