graphQL的使用

    最近一直使用graphQL开发接口,这个真的是很方便,再也不用给前端提供API文档啦。基础知识可以直接上官网https://graphql.cn/上学习一下即可。下面我记录一下,自己使用过程中觉得需要注意的细节。

1、每一个函数必须有输出,可以是对象、数组、字符、布尔值

2、输入参数中,若为必输参数,在参数后加!

3、GraphQL 支持的指令有两个:

         @include(if: Boolean) 仅在参数为true时,包含此字段。

         @skip(if: Boolean) 如果参数为true,跳过此字段。

但是可以自定义指令,例:

directive @auth(requires: String) on OBJECT | FIELD_DEFINITION
@Bean
public SchemaDirective authorisationDirective() {
    return new SchemaDirective("auth", new AuthorisationDirective());
}

4、自定义类型

scalar Date



@Bean
public GraphQLScalarType dateScalar() {
    return GraphQLScalarType.newScalar().name("Date").description(" Date 转换类").coercing(new Coercing<Date, String>() {
        @Override
        public String serialize(Object input) throws CoercingSerializeException {
            if (input instanceof Date) {
                return DateUtil.format((Date) input, "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
            } else if (input instanceof String) {
                return (String) input;
            }
            throw new CoercingSerializeException(
                "Expected a 'String' or 'java.util.Date' but was '" + typeName(input) + "'."
            );
        }

        @Override
        public Date parseValue(Object input) throws CoercingParseValueException {
            Date date = null;
            if (input instanceof Date) {
                date = (Date) input;
            } else if (input instanceof String) {
                ReflectionUtils.convert(input, Date.class);
            } else {
                throw new CoercingParseValueException(
                    "Expected a 'String' or 'java.time.temporal.TemporalAccessor' but was '" + typeName(input) + "'."
                );
            }
            return date;
        }

        @Override
        public Date parseLiteral(Object input) throws CoercingParseLiteralException {
            if (!(input instanceof StringValue)) {
                throw new CoercingParseLiteralException(
                    "Expected AST type 'StringValue' but was '" + typeName(input) + "'."
                );
            }
            return ReflectionUtils.convert(input, Date.class);
        }

    }).build();
}

5、若函数返回值为数字、字符、布尔值时,测试函数接口时,不需要写返回值。

6、输入接口的参数可以与实体bean不一样,根据业务需求进行修改。

7、输出对象中的属性可以是方法,方法也可以传参数,只需要实现该方法即可。例:

#任务主表
type Issue {
    # 任务ID
    id: ID
    # 项目id(预留)
    project: IssueProject
    # 任务对应的当前操作
    operations(user: ID): [IssueOperation]

}

实现该方法

public List<IssueOperation> operations(Issue issue,Long user) {
    List<IssueWorkflowStepTransition> transitions =     this.issueWorkflowService.getWorkflowStepTransitions(issue,user);

    return transitions.stream().map(item -> IssueOperation.builder()
            .name(item.getName()).id(item.getId()).screen(item.getView()).grants(item.getGrants())
            .build()).collect(Collectors.toList());
}

 8、两个实体可以使用同一个输出对象(该对象包含两个bean的全部属性),输出时需要重写输出对象的属性。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值