Graphql FieldVisibility

Graphql 除了通过instrument修改返回外,也可以通过FieldVisibility来进行控制,官方示意代码是通过emptyList不返回,关键代码如下:

@Override
        public List<GraphQLFieldDefinition> getFieldDefinitions(GraphQLFieldsContainer fieldsContainer) {
     
            if ("AdminType".equals(fieldsContainer.getName())) {
     
                if (!userAccessService.isAdminUser()) {
     
                    return Collections.emptyList();
                }
            }
            return fieldsContainer.getFieldDefinitions();
        }

        @Override
        public GraphQLFieldDefinition getFieldDefinition(GraphQLFieldsContainer fieldsContainer, String fieldName) {
     
            if ("AdminType".equals(fieldsContainer.getName())) {
     
                if (!userAccessService.isAdminUser()) {
     
                    return null;
                }
            }
            return fieldsContainer.getFieldDefinition(fieldName);
        }

以上代码来自https://www.graphql-java.com/documentation/v15/fieldvisibility/

实际上也可以参考instrument,返回新的fieldDefinition来实现mask等任意操作。

你可能感兴趣的:(graphql,架构,java,graphql,fieldVisibility,java)