2021-09-28 graphql

官网:http://spec.graphql.org/


中文网:http://graphql.cn/



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



https://github.com/graphql-java-kickstart/graphql-spring-boot/tree/v11.1.0


<!-- to embed GraphQL Playground tool -->

<dependency>

    <groupId>com.graphql-java-kickstart</groupId>

    <artifactId>playground-spring-boot-starter</artifactId>

    <version>11.0.0</version>

    <scope>runtime</scope>

</dependency>

添加后启动项目


graphql 文件中,type Query 或者 type Mutation 定义的方法名,必须与我们后端实现 GraphQLQueryResolver 或 GraphQLMutationResolver 之后写的接口名称一致,如出现此问题请检查是否一致

http://localhost:8999/playground



在代码中的graphql文件中添加注释:


重启之后访问playground:


备注:相对于restful的好处:能够指定返回的字段不用返回整个对象从而减少网络开销

递归场景的查询:


其中这个递归查询可以无限往下(可能导致恶意之人攻击服务器,所以需要配置最大递归数)


配置之后重启


playgroud配置项学习


graphql.playground:

    mapping: /playground

    endpoint: /graphql

    subscriptionEndpoint: /subscriptions

    staticPath.base: my-playground-resources-folder

    enabled: true

    pageTitle: Playground

    cdn:

        enabled: false

        version: latest

    settings:

        editor.cursorShape: line

        editor.fontFamily: "'Source Code Pro', 'Consolas', 'Inconsolata', 'Droid Sans Mono', 'Monaco', monospace"

        editor.fontSize: 14

        editor.reuseHeaders: true

        editor.theme: dark

        general.betaUpdates: false

        prettier.printWidth: 80

        prettier.tabWidth: 2

        prettier.useTabs: false

        request.credentials: omit

        schema.polling.enable: true

        schema.polling.endpointFilter: "*localhost*"

        schema.polling.interval: 2000

        schema.disableComments: true

        tracing.hideTracingResponse: true

    headers:

        headerFor: AllTabs

    tabs:

        - name: Example Tab

          query: classpath:exampleQuery.graphql

          headers:

            SomeHeader: Some value

          variables: classpath:variables.json

          responses:

            - classpath:exampleResponse1.json

            - classpath:exampleResponse2.json


配置示例值(打开界面就能查看)


配置方法:

application.yml:





还加上了统一配置的header:



一般只在开发和测试环境会使用到playgroud的配置:正式环境需要关闭可设置开关

这样playground就被禁用了


可视化学习:

https://github.com/APIs-guru/graphql-voyager


点进去:(schema中的类型和他们之间的引用关系呈现出来)

https://apis.guru/graphql-voyager/


当然graphql-java-kickstart/graphql-spring-boot也有Voyager


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


添加依赖之后:访问http://localhost:8999/voyager


其中界面卡在了这个地方是因为maxQueryDepth的最大值设置为5了,需要修改成大一点voyager才能正常访问



生产环境可关闭:


感叹号去掉就允许字段为空

异常处理:

如果使用:


会默认覆盖掉:


如果想使用自定义异常需要开启开关:



如果不想要冗余的内容(定制化处理)




©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容