package com.kfit.test.resolver;import org.springframework.stereotype.Component;import com.coxautodev.graphql.tools.GraphQLMutationResolver;import com.kfit.test.bean.Author;@Componentpublic class AuthorMutation implements GraphQLMutationResolver{ public Author saveAuthor(Author author) { //调用service方法进行保存Author,这里模拟一下设置一个id,不进行操作了。 author.setId(3); return author; } }
1.3 编写.graphqls文件
我们在author.graphqls文件添加如下信息:
#这里的接收的参数的对象input AuthorInput{ #作者的id id: Int #作者名称 name: String #作者照片 photo: String}#增删改方法extend type Mutation{ #保存作者信息 saveAuthor(author:AuthorInput!):Author}
这里的saveAuthor是否的参数是否可以使用Author,也就是写成如下的代码:
saveAuthor(author:Author!):Author
很遗憾,启动就会报错了:
Expected type 'Author' to be a GraphQLInputType, but it wasn't! Was a type only permitted for object types incorrectly used as an input type, or vice-versa?
意思期望接收的参数是Author应该是一个GraphQLInputType的类型,但是实际上不是。也就是我们在配置文件定义的input AuthorInput对应的是GraphQLInputType类型。
另外很重要的一个配置就是在root.graphqls文件中进行配置type Mutation,不配置是可以正常启动的,但是extend type Mutation是没有生效的:
FieldResolverError: No method found with any of the following signatures (with or without one of [interface graphql.schema.DataFetchingEnvironment] as the last argument)