GraphQL SPQR (GraphQL Schema Publisher & Query Resolver, pronounced like speaker) is a simple-to-use library for rapid development of GraphQL APIs in Java.
GraphQL SPQR aims to make it dead simple to add a GraphQL API to any Java project. It works by dynamically generating a GraphQL schema from Java code.
When developing GraphQL-enabled applications it is common to define the schema first and hook up the business logic later. This is known as the schema-first style. While it has its advantages, in strongly and statically typed languages, like Java, it leads to a lot of duplication.
For example, a schema definition of a simple GraphQL type could like this:
type Link {
id: ID!
url: String!
description: String
}
and, commonly, a corresponding Java type would exist in the system, similar to the following:
public class Link {
private final String id;
private final String url;
private final String description;
//constructors, getters and setters
//...
}
Both of these blocks contain the exact same information. Worse yet, changing one requires an immediate change to the other. This makes refactoring risky and cumbersome, and the compiler can not help. On the other hand, if you’re trying to introduce a GraphQL API into an existing project, writing the schema practically means re-describing the entire existing model. This is both expensive and error-prone, and still suffers from duplication and lack of tooling.
Instead, GraphQL SPQR takes the code-first approach, by generating the schema from the existing model. This keeps the schema and the model in sync, easing refactoring. It also works well in projects where GraphQL is introduced on top of an existing codebase.
Note that developing in the code-first style is still effectively schema-first, the difference is that you develop your schema not in yet another language, but in Java, with your IDE, the compiler and all your tools helping you. Breaking changes to the schema mean the compilation will fail. No need for linters or other fragile hacks.
GraphQL SPQR is deployed to Maven Central.
Maven
<dependency>
<groupId>io.leangen.graphql</groupId>
<artifactId>spqr</artifactId>
<version>0.11.2</version>
</dependency>
Gradle
compile 'io.leangen.graphql:spqr:0.11.2'
The example will use annotations provided by GraphQL SPQR itself, but these are optional and the mapping is completely configurable, enabling existing services to be exposed through GraphQL without modification.
Service class:
class UserService {
@GraphQLQuery(name = "user")
public User getById(@GraphQLArgument(name = "id") Integer id) {
...
}
}
If you want to skip adding @GraphQLArgument
, compile with the -parameters
option or the names will be lost.
Domain class:
public class User {
private String name;
private Integer id;
private Date registrationDate;
@GraphQLQuery(name = "name", description = "A person's name")
public String getName() {
return name;
}
@GraphQLQuery
public Integer getId() {
return id;
}
@GraphQLQuery(name = "regDate", description = "Date of registration")
public Date getRegistrationDate() {
return registrationDate;
}
}
To attach additional fields to the User
GraphQL type, without modifyning the User
class, you simply add a query that has User
as the context. The simplest way is using the @GraphQLContext
annotation:
class UserService {
... //regular queries, as above
// Attach a new field called twitterProfile to the User GraphQL type
@GraphQLQuery
public TwitterProfile twitterProfile(@GraphQLContext User user) {
...
}
}
Exposing the service with graphql-spqr:
UserService userService = new UserService(); //instantiate the service (or inject by Spring or another framework)
GraphQLSchema schema = new GraphQLSchemaGenerator()
.withBasePackages("io.leangen") //not mandatory but strongly recommended to set your "root" packages
.withOperationsFromSingleton(userService) //register the service
.generate(); //done ;)
GraphQL graphQL = new GraphQL.Builder(schema)
.build();
//keep the reference to GraphQL instance and execute queries against it.
//this operation selects a user by ID and requests name, regDate and twitterProfile fields only
ExecutionResult result = graphQL.execute(
"{ user (id: 123) {
name,
regDate,
twitterProfile {
handle
numberOfTweets
}
}}");
We're working on a SPQR-powered Spring Boot starter. The project is still very young, but already functional.
See more complete examples using Spring Boot at https://github.com/leangen/graphql-spqr-samples
Coming soon
For best compatibility, Kotlin 1.3.70 or later is needed with the compiler argument -Xemit-jvm-type-annotations
.This instructs the Kotlin compiler to produce type-use annotations (introduced in JDK8) correctly.See KT-35843 and KT-13228 for details.
There's a bug in OpenJDK's annotation parser before version 16 b17 that causes annotations on generic type parameters to be duplicated. You may experience this in a form of a mysterious
AnnotationFormatError: Duplicate annotation for class: interface io.leangen.graphql.annotations.GraphQLNonNull
being thrown when using @GraphQLNonNull
both on a type and on its generic parameters e.g. @GraphQLNonNull List<@GraphQLNonNull Item>
.
Fortunately, very few users seem to experience this problem, even on affected JDKs. Do note it is only relevant which Java compiles the sources, not which Java runs the code. Also note that IntelliJ IDEA comes bundled with a JDK of its own, so building the project in IDEA may lead to this error. You should configure your IDE to use the system Java if it is different.
GraphQL:整合Spring Boot GraphQL在Java中的应用要解决的第一个问题便是如何根据Java Bean自动生成GraphQL Schema,定义多套基本上是不可接受的,在现有加一些注解还是能忍的。 1. SQL解决方案 关系型数据库的解决方案已经做得比较好了,目前Yahoo/Elide应该是做得最好的,结合elide-spring-boot-starter可以轻松完成。虽然文
GraphQL 是一种用于 API 的查询语言也是一个满足你数据查询的运行时。 GraphQL 对你的 API 中的数据提供了一套易于理解的完整描述,使得客户端能够准确地获得它需要的数据,而且没有任何冗余. GraphQL服务搭建 首先需要运行一个Express GraphQL服务器,express-graphql模块提供了一个创建Express服务的简单方法来运行GraphQL API,grap
一.引言 GraphQL与RESTful都是基于HTTP进行数据的请求与接收。 核心差异:资源的描述信息与其获取方式相分离。 例如根据图书id获取图书信息: 1.RESTful获取: GET /books/1 { "title": "Black Hole Blues", "author": { "firstName"
如何在Vue中使用Apollo ? 准备工作: 配置安装 npm install --save vue-apollo graphql apollo-boost 创建apollo.js 文件 import Vue from 'vue' import VueApollo from 'vue-apollo' import ApolloClient from 'apollo-boost' Vue.use(
首先下载express,express-graphql和graphql var express = require('express'); var graphqlHTTP = require('express-graphql'); var { buildSchema } = require('graphql'); var schema = buildSchema(` type Accou
graphql GraphQL is a query language for API’s. It shows what are the different types of data provided by the server and then the client can pick exactly what it wants. GraphQL是API的查询语言。 它显示了服务器提供的不同类型
快速开始 GraphQL 是一种用于 API 的查询语言。这是 GraphQL 和 REST 之间一个很好的比较 (译者注: GraphQL 替代 REST 是必然趋势)。在这组文章中, 我们不会解释什幺是 GraphQL, 而是演示如何使用 @nestjs/GraphQL 模块。 GraphQLModule 只不过是 Apollo 服务器的包装器。我们没有造轮子, 而是提供一个现成的模块, 这让
GraphQL 既是一种用于 API 的查询语言也是一个满足你数据查询的运行时。 GraphQL 对你的 API 中的数据提供了一套易于理解的完整描述,使得客户端能够准确地获得它需要的数据,而且没有任何冗余,也让 API 更容易地随着时间推移而演进,还能用于构建强大的开发者工具。 向你的 API 发出一个 GraphQL 请求就能准确获得你想要的数据,不多不少。 GraphQL 查询总是返回可预测
Graphql editor 是一款 Graphql 的可视化编辑器和 IDE,帮助用户更容易理解 GraphQL 模式,通过使用可视化块系统创建模式。GraphQL Editor 将把它们转化为代码。通过 GraphQL Editor,用户可以在不写任何代码的情况下创建可视化的图表,或者以一种很好的方式呈现其模式。 GraphQL View Code Editor View Hierarchy View
GraphQL CLI Help us to improve new GraphQL CLI. Check out the new structure and commands below!Feel free to contact us in Discord channel. We would love to hear your feedback. Features Helpful command
Fullstack GraphQL Simple Demo Application API built with Node + Express + GraphQL + Sequelize (supports MySQL, Postgres, Sqlite and MSSQL). WebApp built with React + Redux. Written in ES6 using Babel
Hasura GraphQL Engine Hasura is an open source product that accelerates API development by 10x by giving you GraphQL or REST APIs with built in authorization on your data, instantly. Read more at hasu
这是利用Koa + GraphQL + Apollo-Server实现的,学生信息增、删、改、查的栗子 本栗子将搭配Koa实现一个GraphQL查询,逐步从简单Kao服务、到Mongodb的数据插入查询、再到GraphQL的使用,让大家快速看到: 搭建Koa搭建一个后台项目 后台路由简单处理方式 利用Mongoose简单操作Mongodb的增、删、改、查 掌握Apollo-Server简单操作数据
GraphQL Ruby 是 GraphQL 的一个 Ruby 实现。 Website API Documentation Newsletter 安装: # Gemfilegem 'graphql' $ bundle install 旨在: 实现 GraphQL 规范并支持一个 Relay 前端 在可能的情况下,提供与参考实现相似的习惯性的、简单的 Ruby API 支持 Ruby on Rails 和 Relay