Skip to content

Commit

Permalink
如果在xbiz中重定义method,此前的情况是会丢弃java中定义的argsNormalizer。现在修改逻辑,保留java中的auth…
Browse files Browse the repository at this point in the history
…和argsNormalizer配置
  • Loading branch information
entropy-cloud committed Jan 4, 2025
1 parent 629eaf1 commit 00e90d4
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 14 deletions.
19 changes: 19 additions & 0 deletions docs/dev-guide/graphql/graphql-java.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> 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}}`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
<auth roles="user" />
</query>

<query name="findPage">
</query>

<query name="my_findPage">
<arg name="query" type="io.nop.api.core.beans.query.QueryBean" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions nop-biz/src/main/java/io/nop/biz/model/_gen/_BizActionModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 ;

/**
*
Expand Down Expand Up @@ -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;
Expand Down
28 changes: 28 additions & 0 deletions nop-biz/src/main/java/io/nop/biz/model/_gen/_BizLoaderModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ public abstract class _BizLoaderModel extends io.nop.core.resource.component.Abs
*/
private KeyedList<io.nop.biz.model.BizActionArgModel> _args = KeyedList.emptyList();

/**
*
* xml name: argsNormalizer
*
*/
private java.lang.String _argsNormalizer ;

/**
*
* xml name: autoCreateField
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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());
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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)
Expand Down
7 changes: 3 additions & 4 deletions nop-xdefs/src/main/resources/_vfs/nop/schema/biz/xbiz.xdef
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<query xdef:name="BizActionModel" name="!string" displayName="string"
executor="string" bizSequential="!boolean=false" task:name="string" task:version="long"
timeout="long" disabled="!boolean=false" idempotent="!boolean=false" safe="!boolean=false"
async="!boolean=false" xdef:bean-tag-prop="type">
async="!boolean=false" xdef:bean-tag-prop="type" argsNormalizer="bean-name">

<auth xdef:ref="../schema/auth.xdef"/>

Expand All @@ -51,8 +51,6 @@
<cache-evict cacheName="string" cacheKeyExpr="expr" xdef:name="BizCacheEvictModel"/>
</cache-evicts>

<args-normalizer xdef:value="xpl-fn:(args:Map,svcCtx:io.nop.core.context.IServiceContext)=>Map"/>

<arg xdef:name="BizActionArgModel" name="!var-name" mandatory="!boolean=false"
displayName="string" xdef:unique-attr="name" type="generic-type" graphql:type="string"
kind="enum:io.nop.api.core.annotations.biz.BizActionArgKind">
Expand Down Expand Up @@ -80,7 +78,8 @@
</actions>

<loaders xdef:body-type="list" xdef:key-attr="name">
<loader name="!string" xdef:name="BizLoaderModel" disabled="!boolean=false" autoCreateField="!boolean=false">
<loader name="!string" xdef:name="BizLoaderModel" disabled="!boolean=false" autoCreateField="!boolean=false"
argsNormalizer="bean-name">
<!--
是否缓存action调用结果
-->
Expand Down

0 comments on commit 00e90d4

Please sign in to comment.