java使用graphql_graphql-java使用手册: part1 入门

入门

graphql-java 需要运行于 Java 8 或更高版本.

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

首先,保证 mavenCentral 在你的 repos 库列表中:

repositories {

mavenCentral()

}

依赖:

dependencies {

compile 'com.graphql-java:graphql-java:6.0'

}

如果在 Maven 中使用最新正式版本

依赖:

com.graphql-java

graphql-java

6.0

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 上获取.

可以从 最新版本 中看到最新的版本号.

如果在 Gradle 中使用最新的开发中版本

增加 repositories:

repositories {

mavenCentral()

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

}

依赖:

dependencies {

compile 'com.graphql-java:graphql-java:INSERT_LATEST_VERSION_HERE'

}

如果在 Maven 中使用最新的开发中版本

增加 repository:

false

bintray-andimarek-graphql-java

bintray

http://dl.bintray.com/andimarek/graphql-java

依赖:

com.graphql-java

graphql-java

INSERT_LATEST_VERSION_HERE

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
graphql-java 是 GraphQLJava 实现。这个库的目标是用于真实的生产环境。graphql-java 解析和执行查询 GraphQL 。它并不真正获取任何数据的:数据来源于执行回调或提供静态数据。graphql-java 的 "hello world":import graphql.schema.GraphQLObjectType; import graphql.schema.GraphQLSchema; import static graphql.Scalars.GraphQLString; import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition; import static graphql.schema.GraphQLObjectType.newObject; public class HelloWorld {     public static void main(String[] args) {         GraphQLObjectType queryType = newObject()                         .name("helloWorldQuery")                         .field(newFieldDefinition()                                 .type(GraphQLString)                                 .name("hello")                                 .staticValue("world")                                 .build())                         .build();         GraphQLSchema schema = GraphQLSchema.newSchema()                         .query(queryType)                         .build();         Map result = new GraphQL(schema).execute("{hello}").getData();         System.out.println(result);         // Prints: {hello=world}     } } 标签:graphql
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值