diff --git a/docs/dev-guide/graphql/graphql-java.md b/docs/dev-guide/graphql/graphql-java.md index 7a89b9424..7d1f1f180 100644 --- a/docs/dev-guide/graphql/graphql-java.md +++ b/docs/dev-guide/graphql/graphql-java.md @@ -255,6 +255,25 @@ filter对应于后台的TreeBean类型的对象,这是一个通用的Tree结 | startsWith | 字符串的前缀为指定值 | | endsWith | 字符串的后缀为指定值 | +### BizArgsNormalizer参数规范格式转换 +CrudBizModel的`findPage/findList`等函数接收QueryBean格式的query参数,但是前台构造QueryBean结构比较复杂,所以也支持直接传递`filter_{propName}`格式的过滤条件,例如`filter_status=1`。 +后台的实现是利用`@BizArgsNormalizer`注解引入的IGraphQLArgsNormalizer接口对象来对前台传递的参数进行规范化,将前台传递的`filter_xx`条件转换为QueryBean对象。这是一种通用机制,不仅仅用在QueryBean结构转换上。 + +```javascript + @BizQuery + @BizArgsNormalizer(BizConstants.BEAN_nopQueryBeanArgsNormalizer) + @GraphQLReturn(bizObjName = BIZ_OBJ_NAME_THIS_OBJ) + public PageBean deleted_findPage(@Optional @Name("query") @Description("@i18n:biz.query|查询条件") QueryBean query, + FieldSelectionBean selection, IServiceContext context) { + ... + } +``` + +`@BizArgsNormalizer`的参数是在NopIoC中注册的bean的名字,nopQueryBeanArgsNormalizer对应于`biz-defaults.beans.xml`中注册的QueryBeanArgsNormalizer对象。 + +`IGraphQLEngine.newRpcContext`的时候会调用argsNormalizer,因此前台通过`/p/{bizObjName}_{bizAction}`或者`/r/{bizObjName}_{bizAction}`执行后台服务时都会调用到这一逻辑。 +单元测试参见`TestNopAuthUserBizModel.testQueryBeanNormalizer`。 + ## 3.3 this指针:知识的相对化 GraphQL中定义的操作名是全局名称,例如 `query{ getUser(id:3){ id, userName}}` diff --git a/nop-auth/nop-auth-service/src/test/resources/_vfs/_delta/default/nop/auth/model/NopAuthUser/NopAuthUser.xbiz b/nop-auth/nop-auth-service/src/test/resources/_vfs/_delta/default/nop/auth/model/NopAuthUser/NopAuthUser.xbiz index ad277d2ce..54805c2d4 100644 --- a/nop-auth/nop-auth-service/src/test/resources/_vfs/_delta/default/nop/auth/model/NopAuthUser/NopAuthUser.xbiz +++ b/nop-auth/nop-auth-service/src/test/resources/_vfs/_delta/default/nop/auth/model/NopAuthUser/NopAuthUser.xbiz @@ -7,6 +7,9 @@ + + + diff --git a/nop-biz/src/main/java/io/nop/biz/impl/BizModelToGraphQLDefinition.java b/nop-biz/src/main/java/io/nop/biz/impl/BizModelToGraphQLDefinition.java index b6f5701d8..e05e54e08 100644 --- a/nop-biz/src/main/java/io/nop/biz/impl/BizModelToGraphQLDefinition.java +++ b/nop-biz/src/main/java/io/nop/biz/impl/BizModelToGraphQLDefinition.java @@ -15,6 +15,7 @@ import io.nop.biz.model.BizActionModel; import io.nop.biz.model.BizLoaderModel; import io.nop.biz.model.BizReturnModel; +import io.nop.commons.util.StringHelper; import io.nop.core.context.action.IServiceAction; import io.nop.core.lang.eval.IEvalAction; import io.nop.core.lang.eval.IEvalScope; @@ -34,8 +35,8 @@ import io.nop.graphql.core.fetcher.BeanMethodBatchFetcher; import io.nop.graphql.core.fetcher.ServiceActionFetcher; import io.nop.graphql.core.reflection.ArgBuilders; -import io.nop.graphql.core.reflection.EvalGraphQLArgsNormalizer; import io.nop.graphql.core.reflection.IServiceActionArgBuilder; +import io.nop.graphql.core.reflection.LazyGraphQLArgsNormalizer; import io.nop.graphql.core.schema.GraphQLScalarType; import io.nop.graphql.core.schema.TypeRegistry; import io.nop.graphql.core.schema.meta.ObjMetaToGraphQLDefinition; @@ -97,7 +98,7 @@ public GraphQLFieldDefinition toOperationDefinition(String bizObjName, BizAction } if (actionModel.getArgsNormalizer() != null) { - field.setArgsNormalizer(new EvalGraphQLArgsNormalizer(actionModel.getArgsNormalizer())); + field.setArgsNormalizer(new LazyGraphQLArgsNormalizer(actionModel.getArgsNormalizer())); } return field; } @@ -115,6 +116,10 @@ public GraphQLFieldDefinition toBuilder(String thisObjName, BizLoaderModel loade IDataFetcher fetcher = buildFetcher(thisObjName, loaderModel, typeRegistry); field.setFetcher(fetcher); field.setAutoCreate(loaderModel.isAutoCreateField()); + + if (!StringHelper.isEmpty(loaderModel.getArgsNormalizer())) + field.setArgsNormalizer(new LazyGraphQLArgsNormalizer(loaderModel.getArgsNormalizer())); + return field; } diff --git a/nop-biz/src/main/java/io/nop/biz/impl/BizObjectBuildHelper.java b/nop-biz/src/main/java/io/nop/biz/impl/BizObjectBuildHelper.java index 6e54cc0d8..429710a91 100644 --- a/nop-biz/src/main/java/io/nop/biz/impl/BizObjectBuildHelper.java +++ b/nop-biz/src/main/java/io/nop/biz/impl/BizObjectBuildHelper.java @@ -63,6 +63,15 @@ private static void mergeBizModel(IGraphQLBizObject bizObj, String name, GraphQL if (op.getArguments() == null) { op.setArguments(action.cloneArguments()); } + if( op.getArgsNormalizer() == null){ + op.setArgsNormalizer(action.getArgsNormalizer()); + } + if(op.getAuth() == null) + op.setAuth(action.getAuth()); + if(op.getMakerCheckerMeta() == null) + op.setMakerCheckerMeta(action.getMakerCheckerMeta()); + if(op.getLazy() == null) + op.setLazy(action.getLazy()); } } } diff --git a/nop-biz/src/main/java/io/nop/biz/model/_gen/_BizActionModel.java b/nop-biz/src/main/java/io/nop/biz/model/_gen/_BizActionModel.java index 137b3f682..379d0db85 100644 --- a/nop-biz/src/main/java/io/nop/biz/model/_gen/_BizActionModel.java +++ b/nop-biz/src/main/java/io/nop/biz/model/_gen/_BizActionModel.java @@ -25,10 +25,10 @@ public abstract class _BizActionModel extends io.nop.core.resource.component.Abs /** * - * xml name: args-normalizer + * xml name: argsNormalizer * */ - private io.nop.core.lang.eval.IEvalFunction _argsNormalizer ; + private java.lang.String _argsNormalizer ; /** * @@ -203,16 +203,16 @@ public boolean hasArgs(){ /** * - * xml name: args-normalizer + * xml name: argsNormalizer * */ - public io.nop.core.lang.eval.IEvalFunction getArgsNormalizer(){ + public java.lang.String getArgsNormalizer(){ return _argsNormalizer; } - public void setArgsNormalizer(io.nop.core.lang.eval.IEvalFunction value){ + public void setArgsNormalizer(java.lang.String value){ checkAllowChange(); this._argsNormalizer = value; diff --git a/nop-biz/src/main/java/io/nop/biz/model/_gen/_BizLoaderModel.java b/nop-biz/src/main/java/io/nop/biz/model/_gen/_BizLoaderModel.java index 74714c011..698381454 100644 --- a/nop-biz/src/main/java/io/nop/biz/model/_gen/_BizLoaderModel.java +++ b/nop-biz/src/main/java/io/nop/biz/model/_gen/_BizLoaderModel.java @@ -23,6 +23,13 @@ public abstract class _BizLoaderModel extends io.nop.core.resource.component.Abs */ private KeyedList _args = KeyedList.emptyList(); + /** + * + * xml name: argsNormalizer + * + */ + private java.lang.String _argsNormalizer ; + /** * * xml name: autoCreateField @@ -110,6 +117,25 @@ public boolean hasArgs(){ return !this._args.isEmpty(); } + /** + * + * xml name: argsNormalizer + * + */ + + public java.lang.String getArgsNormalizer(){ + return _argsNormalizer; + } + + + public void setArgsNormalizer(java.lang.String value){ + checkAllowChange(); + + this._argsNormalizer = value; + + } + + /** * * xml name: autoCreateField @@ -246,6 +272,7 @@ protected void outputJson(IJsonHandler out){ super.outputJson(out); out.putNotNull("args",this.getArgs()); + out.putNotNull("argsNormalizer",this.getArgsNormalizer()); out.putNotNull("autoCreateField",this.isAutoCreateField()); out.putNotNull("cache",this.getCache()); out.putNotNull("disabled",this.isDisabled()); @@ -264,6 +291,7 @@ protected void copyTo(BizLoaderModel instance){ super.copyTo(instance); instance.setArgs(this.getArgs()); + instance.setArgsNormalizer(this.getArgsNormalizer()); instance.setAutoCreateField(this.isAutoCreateField()); instance.setCache(this.getCache()); instance.setDisabled(this.isDisabled()); diff --git a/nop-graphql/nop-graphql-core/src/main/java/io/nop/graphql/core/reflection/ReflectionBizModelBuilder.java b/nop-graphql/nop-graphql-core/src/main/java/io/nop/graphql/core/reflection/ReflectionBizModelBuilder.java index ec541be07..0e9327534 100644 --- a/nop-graphql/nop-graphql-core/src/main/java/io/nop/graphql/core/reflection/ReflectionBizModelBuilder.java +++ b/nop-graphql/nop-graphql-core/src/main/java/io/nop/graphql/core/reflection/ReflectionBizModelBuilder.java @@ -274,6 +274,9 @@ private GraphQLFieldDefinition buildActionField(String bizObjName, Object bean, GraphQLFieldDefinition field = new GraphQLFieldDefinition(); field.setFunctionModel(func); + field.setLocation(loc); + field.setName(GraphQLNameHelper.getOperationName(bizObjName, name)); + field.setServiceAction(action); ReflectionGraphQLTypeFactory.INSTANCE.getArgDefinitions(field, func, registry); @@ -299,9 +302,6 @@ private GraphQLFieldDefinition buildActionField(String bizObjName, Object bean, field.setArgsNormalizer(new LazyGraphQLArgsNormalizer(argsNormalizer.value())); } - field.setLocation(loc); - field.setName(GraphQLNameHelper.getOperationName(bizObjName, name)); - field.setServiceAction(action); field.setFetcher(fetcher); if (func.getAsyncReturnType().getRawClass() == ApiResponse.class) diff --git a/nop-xdefs/src/main/resources/_vfs/nop/schema/biz/xbiz.xdef b/nop-xdefs/src/main/resources/_vfs/nop/schema/biz/xbiz.xdef index 4aed90653..6764ae454 100644 --- a/nop-xdefs/src/main/resources/_vfs/nop/schema/biz/xbiz.xdef +++ b/nop-xdefs/src/main/resources/_vfs/nop/schema/biz/xbiz.xdef @@ -28,7 +28,7 @@ + async="!boolean=false" xdef:bean-tag-prop="type" argsNormalizer="bean-name"> @@ -51,8 +51,6 @@ - - @@ -80,7 +78,8 @@ - +