什么是GraphQL
GraphQL一个解决「前端做还是后端做」问题的查询语言。
GraphQL是Facebook于2012年在内部开发的数据查询语言,在2015年开源,旨在提供RESTful架构体系替代方案。
Helloworld
- 通过maven引入最新版本
<dependency><groupId>com.graphql-java</groupId><artifactId>graphql-java</artifactId><version>9.0</version></dependency>
- 编写Helloworld文档
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}}}
运行结果

大致理念

