我有一个使用Spring Boot运行的项目,并已使用com.graphql-java.graphql-java:12.0实现了GraphQL API。
[我现在想为某些Mutator以及某些字段设置字段可见性,但不幸的是,我没有找到有关如何做到这一点的有效说明的教程,文档或示例。
为了说明,我在模式中有以下示例条目:
type Query {
login(username: String!, password: String!): String
organization(id: Int!): ApiOrganization
}
type Mutation {
updateProfile(profileData: ProfileInputDto): ID
updateAdminStuff(adminData: AdminStuffDto): ID
}
现在,使用api的所有用户的查询条目在方案中将可见,并且对updateProfile的更改也将可见。但是只有当用户以管理员角色身份登录时,突变updateAdminStuff才可见,这样普通用户甚至都不知道该突变存在。此外,可能会发生某些模式类型的某些字段仅对某些角色可见的情况。
我发现可以通过GraphqlFieldVisibility(https://www.graphql-java.com/documentation/v12/fieldvisibility/)进行类似设置。我发现的第一个版本说过要在GraphQLSchema中进行设置,但似乎已弃用,我应该使用GraphQLCodeRegistry来设置可见性。对于GraphQLCodeRegistry我在https://www.graphql-java.com/documentation/v12/execution/中找到了
GraphQLCodeRegistry codeRegistry = newCodeRegistry()
.dataFetcher(
coordinates("CreateReviewForEpisodeMutation", "createReview"),
mutationDataFetcher()
)
.build();
GraphQLSchema schema = GraphQLSchema.newSchema()
.query(queryType)
.mutation(createReviewForEpisodeMutation)
.codeRegistry(codeRegistry)
.build();
但是不幸的是,我找不到为我使用的模式生成设置此方法的方法。
有人可以给我一个提示(示例,教程,文档),在哪里可以找到解决方案的提示? (如果在GraphQL中完全可行)
[有关此项目的其他一些信息:我有一个schmea定义另存为schema.graphqls。我有一个GraphQLProvider,它通过以下方式准备Scehma和GraphQL:
private GraphQL graphQL;
@Bean
public GraphQL graphQL() {
return graphQL;
}
@PostConstruct
public void init() throws IOException {
URL url = Resources.getResource("graphql/schema.graphqls");
String sdl = Resources.toString(url, Charsets.UTF_8);
GraphQLSchema graphQLSchema = buildSchema(sdl);
this.graphQL = GraphQL.newGraphQL(graphQLSchema).build();
}
private GraphQLSchema buildSchema(String sdl) {
TypeDefinitionRegistry typeRegistry = new SchemaParser().parse(sdl);
RuntimeWiring runtimeWiring = buildWiring();
SchemaGenerator schemaGenerator = new SchemaGenerator();
return schemaGenerator.makeExecutableSchema(typeRegistry, runtimeWiring);
}
在我的控制器中,我通过]获取数据>
ExecutionInput executionInput = ExecutionInput.newExecutionInput().context(request).query(body.getQuery()) .build(); ExecutionResult executionResult = graphQL.execute(executionInput);
其中body是一个GraphQLQuery,而graphQL是之前的代码的bean。
感谢您的帮助和最诚挚的问候。
我有一个使用Spring Boot运行的项目,并已使用com.graphql-java.graphql-java:12.0实现了GraphQL API。我想为某些Mutators设置字段可见性,也许现在为某些字段设置...
OK在GraphQL-Java聊天中得到了答案。