graphql-java使用手冊: part1 入門

入門

graphql-java 須要運行於 Java 8 或更高版本.java

如何在 Gradle 中使用最新正式版本

首先,保證 mavenCentral 在你的 repos 庫列表中:maven

repositories {
    mavenCentral()
}

依賴:ui

dependencies {
  compile 'com.graphql-java:graphql-java:6.0'
}

若是在 Maven 中使用最新正式版本

依賴:url

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

Hello World【譯註:這個用不翻譯了吧 :) 】

下面就用 graphql-java 來實現經典的 「hello world」 :翻譯

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

如何使用最新的開發中版本

最近的開發中版本,能夠在 Bintray 上獲取.code

能夠從 最新版本 中看到最新的版本號.ci

若是在 Gradle 中使用最新的開發中版本

增長 repositories:開發

repositories {
    mavenCentral()
    maven { url  "http://dl.bintray.com/andimarek/graphql-java" }
}

依賴:get

dependencies {
  compile 'com.graphql-java:graphql-java:INSERT_LATEST_VERSION_HERE'
}

若是在 Maven 中使用最新的開發中版本

增長 repository:graphql

<repository>
    <snapshots>
        <enabled>false</enabled>
    </snapshots>
    <id>bintray-andimarek-graphql-java</id>
    <name>bintray</name>
    <url>http://dl.bintray.com/andimarek/graphql-java</url>
</repository>

依賴:

<dependency>
    <groupId>com.graphql-java</groupId>
    <artifactId>graphql-java</artifactId>
    <version>INSERT_LATEST_VERSION_HERE</version>
</dependency>
相關文章
相關標籤/搜索