Skip to content

Latest commit

 

History

History
147 lines (109 loc) · 3.29 KB

getting-started.mdx

File metadata and controls

147 lines (109 loc) · 3.29 KB
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';

Getting started

graphql-java requires at least Java 11.

How to use the latest release with Gradle

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")
}

How to use the latest release with Maven

Dependency:

<dependency>
    <groupId>com.graphql-java</groupId>
    <artifactId>graphql-java</artifactId>
    <version>22.3</version>
</dependency>

Hello World

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}
    }
}

Using the latest development build

The latest development build is available on Maven Central.

Please look at the latest build for the latest version value.

How to use the latest build with Gradle

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")
}

How to use the latest build with Maven

Dependency:

<dependency>
    <groupId>com.graphql-java</groupId>
    <artifactId>graphql-java</artifactId>
    <version>INSERT_LATEST_VERSION_HERE</version>
</dependency>