前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >GraphQL hello world

GraphQL hello world

原创
作者头像
vanguard
修改2020-03-16 11:15:22
4240
修改2020-03-16 11:15:22
举报
文章被收录于专栏:vanguard

A query language for your API

GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data. GraphQL provides a complete and understandable description of the data in your API, gives clients the power to ask for exactly what they need and nothing more, makes it easier to evolve APIs over time, and enables powerful developer tools.

https://graphql.org

python

代码语言:python
代码运行次数:0
复制
# pip install graphene
import graphene

class Query(graphene.ObjectType):
  hello = graphene.String(name=graphene.String(default_value="World"))

  def resolve_hello(self, info, name):
    return 'Hello ' + name

schema = graphene.Schema(query=Query)
result = schema.execute('{ hello }')
print(result.data['hello']) # "Hello World"

https://github.com/graphql-python/graphene

java

代码语言:text
复制
# Maven
<dependency>  
 <groupId>com.graphql-java</groupId>  
 <artifactId>graphql-spring-boot-starter</artifactId>  
 <version>4.0.0</version>  
</dependency>

# Spring Bean
@Bean  
GraphQLSchema schema() {  
 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("Xiangbin")))  
 .build();  
 SchemaGenerator schemaGenerator = new SchemaGenerator();  
 return schemaGenerator.makeExecutableSchema(typeDefinitionRegistry, runtimeWiring);  

# URI - application.properties
graphql.servlet.mapping=/graphql  
graphql.servlet.enabled=true  
graphql.servlet.corsEnabled=true

# Start
 java -jar my_project.jar
http://localhost:8080/graphql/
Request body {"query":"{hello}"}
Response {"data":{"hello": "Xiangbin"}}

https://github.com/graphql-java/graphql-spring-boot

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • A query language for your API
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档