GraphQL实践9——Netflix Dgs Graphql数据更新

2022-11-01 11:20:55 浏览数 (2)

功能介绍

graphql-java提供了Mutations类型支持数据更新

NGS Mutations

类型定义

代码语言:yaml复制
type Mutation {
    addActor(actor: SubmitActor!): Actor
}

type Subscription {
    actorAdded: Actor
}

input SubmitActor {
    firstName: String!
    lastName: String!
}

SubmitActor类型可以交给代码生成器自动生成

image-20221028214119439image-20221028214119439

类型转换器

代码语言:java复制
@Mapper(componentModel = SPRING)
public interface ActorAssembler {

	Actor convert(SubmitActor submit);
}

定义解析器

代码语言:java复制
	@DgsMutation
	public Actor addActor(@InputArgument SubmitActor actor) {
		Actor actorEntity = actorAssembler.convert(actor);
		actorEntity.setActorId(10);
		actorEntity.setLastUpdate(new Date());
		return actorEntity;
	}

测试

访问http://localhost:8080/graphiql即可看到在线查询页面

image-20221028213607156image-20221028213607156

0 人点赞