GraphQL Java入门

GraphQL can be implemented in any language. Today let’s take a look at Java server implementation for GraphQL. Let’s use Maven and assume that we already have set up the project structure. What's next?

Dependencies

To run GraphQL in your Java project you only need one library which is GraphQL Java implementation. However, there are a couple of libs you might find useful when using GraphQL Java:

  • Spring Boot - GraphQL Java Spring and Spring Boot integration that will handle any HTTP related tasks, expose the GraphQL service on the /graphql endpoint of your app and accept POST requests containing your GraphQL payload.
  • GraphQL Java Tools - this library allows you to use the GraphQL SDL to build your graphql-java schema.
  • GraphQL Java Servlet - implementation of GraphQL Java Servlet including support for libraries like Relay.js, Apollo etc. and wraps the Java implementation of GraphQL provided by GraphQL Java.

So add dependencies you need to Project Object Model file (pom.xml):
Add GraphQL Java dependency

Define the schema

GraphQL Schema is the centerpiece of any GraphQL implementation so we definitely need one before going any further. To make it easier to understand the operation that a server can perform GraphQL defined a universal schema syntax know as SDL (Schema Definition Language).

SDL定义项目的元素,例如:

  • type (the most basic GraphQL schema components): GraphQL Type
  • query (asking the server for the data) GraphQL Query
  • mutation (manipulating the data): GraphQL Mutation The fastest way to define your schema is to use GraphQL Editor. It allows you to define your schema traditionally (code) or shape it from visual nodes. Low-code GraphQL Editor

Server

There plenty of servlet containers to choose from, both open source and commercial, so just pick the one you feel comfortable with. Let's use Jetty for our example and implement it via a Maven Plugins:
Jetty

您已经准备就绪,接下来的步骤是:

  • 添加一些更基本的服务器配置,例如正确的Java版本或Servlet规范创造GraphQL端点公开您的API的类定义解析器

If you want to read more details about GraphQL Java implementation to take a look at Bojan's tutorial regarding setting up GraphQL Java server on https://www.howtographql.com/

from: https://dev.to//graphqleditor/getting-started-with-graphql-java-13j1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值