功能介绍

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

NGS Mutations

类型定义

type Mutation {
    addActor(actor: SubmitActor!): Actor
}

type Subscription {
    actorAdded: Actor
}

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

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

image-20221028214119439

类型转换器

@Mapper(componentModel = SPRING)
public interface ActorAssembler {

	Actor convert(SubmitActor submit);
}

定义解析器

	@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-20221028213607156

最后修改:2022 年 10 月 28 日
如果觉得我的文章对你有用,请随意赞赏