什么是GraphQL

  • GraphQL一个解决「前端做还是后端做」问题的查询语言。

  • GraphQL是Facebook于2012年在内部开发的数据查询语言,在2015年开源,旨在提供RESTful架构体系替代方案。

Helloworld

  • 通过maven引入最新版本
  1. <dependency>
  2. <groupId>com.graphql-java</groupId>
  3. <artifactId>graphql-java</artifactId>
  4. <version>9.0</version>
  5. </dependency>
  • 编写Helloworld文档
  1. public class HelloWorld {
  2. public static void main(String[] args) {
  3. String schema = "type Query{hello: String}";
  4. SchemaParser schemaParser = new SchemaParser();
  5. TypeDefinitionRegistry typeDefinitionRegistry = schemaParser.parse(schema);
  6. RuntimeWiring runtimeWiring = newRuntimeWiring()
  7. .type("Query", builder -> builder.dataFetcher("hello", new StaticDataFetcher("world")))
  8. .build();
  9. SchemaGenerator schemaGenerator = new SchemaGenerator();
  10. GraphQLSchema graphQLSchema = schemaGenerator.makeExecutableSchema(typeDefinitionRegistry, runtimeWiring);
  11. GraphQL build = GraphQL.newGraphQL(graphQLSchema).build();
  12. ExecutionResult executionResult = build.execute("{hello}");
  13. System.out.println(executionResult.getData().toString());
  14. // Prints: {hello=world}
  15. }
  16. }

运行结果

【20180926】SpringBoot   GraphQL - 图1

大致理念

【20180926】SpringBoot   GraphQL - 图2

参考资料