Skip to content

Commit

Permalink
将GraphQL服务暴露为Grpc接口,并测试通过最简单的接口调用
Browse files Browse the repository at this point in the history
  • Loading branch information
entropy-cloud committed Jan 21, 2024
1 parent a267ee1 commit 709faf6
Show file tree
Hide file tree
Showing 47 changed files with 642 additions and 98 deletions.
11 changes: 11 additions & 0 deletions nop-commons/src/main/java/io/nop/commons/util/StringHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -4448,4 +4448,15 @@ public static boolean isValidNopModuleName(String moduleName) {
return true;
}

@Description("判断字符串是否是有效的USASCII字符串")
@Deterministic
public static boolean isUSASCII(@Name("input") String input) {
for (int i = 0, n = input.length(); i < n; i++) {
char c = input.charAt(i);
if (c < 32 || c > 126) {
return false;
}
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public interface GraphQLErrors {

String ARG_OBJ_NAME = "objName";
String ARG_FIELD_NAME = "fieldName";

String ARG_PROP_ID = "propId";
String ARG_ALLOWED_NAMES = "allowedNames";

String ARG_PARENT_NAME = "parentName";
Expand Down Expand Up @@ -195,7 +197,7 @@ public interface GraphQLErrors {

ErrorCode ERR_GRAPHQL_METHOD_PARAM_NO_REFLECTION_NAME_ANNOTATION = define(
"nop.err.graphql.method-param-no-reflection-name-annotation",
"方法[{methodName}]上的参数[{argName}]没有@ReflectionName注解,也不是引擎可以识别的内部参数", ARG_METHOD_NAME, ARG_ARG_NAME);
"方法[{methodName}]上的参数[{argName}]没有@Name注解,也不是引擎可以识别的内部参数", ARG_METHOD_NAME, ARG_ARG_NAME);

ErrorCode ERR_GRAPHQL_ONLY_ALLOW_ONE_CONTEXT_SOURCE_PARAM = define(
"nop.err.graphql.only-allow-one-context-source-param", "方法[{methodName}]上最多只允许一个参数具有@ContextSource注解",
Expand Down Expand Up @@ -295,4 +297,9 @@ public interface GraphQLErrors {
define("nop.err.graphql.action-return-type-must-not-be-api-response",
"NopGraphQL的服务方法[{methodName}]的返回值类型不需要用ApiResponse包装,直接返回内部结果类型即可",
ARG_METHOD_NAME, ARG_RETURN_TYPE, ARG_CLASS);

ErrorCode ERR_GRAPHQL_FIELD_PROP_ID_CONFLICT =
define("nop.err.graphql.field-prop-id-conflict",
"对象[{objType}]的字段[{fieldName}]的propId[{propId}]与已存在的propId冲突",
ARG_OBJ_TYPE, ARG_FIELD_NAME, ARG_PROP_ID);
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,17 @@ public class GraphQLFieldDefinition extends _GraphQLFieldDefinition {

private IGraphQLArgsNormalizer argsNormalizer;

private int propId;

public int getPropId() {
return propId;
}

public void setPropId(int propId) {
this.propId = propId;
}

public int getPropIdFromMeta() {
if (propMeta != null) {
Integer propId = propMeta.getPropId();
if (propId == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,18 @@
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.BitSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static io.nop.graphql.core.GraphQLErrors.ARG_FIELD_NAME;
import static io.nop.graphql.core.GraphQLErrors.ARG_OBJ_NAME;
import static io.nop.graphql.core.GraphQLErrors.ARG_OBJ_TYPE;
import static io.nop.graphql.core.GraphQLErrors.ARG_PROP_ID;
import static io.nop.graphql.core.GraphQLErrors.ERR_GRAPHQL_FIELD_NO_TYPE;
import static io.nop.graphql.core.GraphQLErrors.ERR_GRAPHQL_FIELD_PROP_ID_CONFLICT;

public class GraphQLObjectDefinition extends _GraphQLObjectDefinition implements INeedInit {
static final Logger LOG = LoggerFactory.getLogger(GraphQLObjectDefinition.class);
Expand Down Expand Up @@ -62,6 +66,31 @@ public void init() {
}
}

public void initPropId() {
BitSet propIds = new BitSet();
int maxPropId = 0;
for (GraphQLFieldDefinition field : fields) {
int propId = field.getPropIdFromMeta();
if (propId > 0) {
if (propIds.get(propId))
throw new NopException(ERR_GRAPHQL_FIELD_PROP_ID_CONFLICT)
.param(ARG_FIELD_NAME, field.getName())
.param(ARG_PROP_ID, propId)
.param(ARG_OBJ_TYPE, getName());
propIds.set(propId);
maxPropId = Math.max(propId, maxPropId);
field.setPropId(propId);
}
}

for (GraphQLFieldDefinition field : fields) {
int propId = field.getPropId();
if (propId <= 0) {
field.setPropId(++maxPropId);
}
}
}

public void removeFieldsNotInMeta() {
if (objMeta != null) {
List<GraphQLFieldDefinition> removed = null;
Expand Down Expand Up @@ -223,6 +252,7 @@ public void mergeField(GraphQLFieldDefinition field, boolean replace) {
old.setType(field.getType());
}


if (old.getArgsNormalizer() == null) {
old.setArgsNormalizer(field.getArgsNormalizer());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
import io.nop.graphql.core.IBizModelImpl;
import io.nop.graphql.core.IDataFetcher;
import io.nop.graphql.core.IDataFetchingEnvironment;
import io.nop.graphql.core.ast.GraphQLArgumentDefinition;
import io.nop.graphql.core.ast.GraphQLFieldDefinition;
import io.nop.graphql.core.ast.GraphQLObjectDefinition;
import io.nop.graphql.core.ast.GraphQLOperationType;
Expand Down Expand Up @@ -275,9 +274,8 @@ private GraphQLFieldDefinition buildActionField(String bizObjName, Object bean,
GraphQLFieldDefinition field = new GraphQLFieldDefinition();
field.setFunctionModel(func);

List<GraphQLArgumentDefinition> argDefs = ReflectionGraphQLTypeFactory.INSTANCE.getArgDefinitions(func,
ReflectionGraphQLTypeFactory.INSTANCE.getArgDefinitions(field, func,
registry);
field.setArguments(argDefs);

Description description = func.getAnnotation(Description.class);
if (description != null)
Expand Down Expand Up @@ -360,9 +358,8 @@ public GraphQLFieldDefinition buildFetcherField(Object bean, SourceLocation loc,
def.setLocation(loc);
def.setFunctionModel(func);

List<GraphQLArgumentDefinition> argDefs = ReflectionGraphQLTypeFactory.INSTANCE.getArgDefinitions(func,
ReflectionGraphQLTypeFactory.INSTANCE.getArgDefinitions(def, func,
registry);
def.setArguments(argDefs);

IDataFetcher fetcher = buildFetcher(bean, loc, name, func);
def.setFetcher(fetcher);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,20 @@
public class ReflectionGraphQLTypeFactory {
public static ReflectionGraphQLTypeFactory INSTANCE = new ReflectionGraphQLTypeFactory();

public List<GraphQLArgumentDefinition> getArgDefinitions(IFunctionModel func, TypeRegistry registry) {
return getArgDefinitions(func, registry, new HashMap<>());
public void getArgDefinitions(GraphQLFieldDefinition field, IFunctionModel func, TypeRegistry registry) {
getArgDefinitions(field, func, registry, new HashMap<>());
}

private List<GraphQLArgumentDefinition> getArgDefinitions(IFunctionModel func, TypeRegistry registry,
Map<String, GraphQLTypeDefinition> creatingTypes) {
private void getArgDefinitions(GraphQLFieldDefinition field, IFunctionModel func, TypeRegistry registry,
Map<String, GraphQLTypeDefinition> creatingTypes) {
List<GraphQLArgumentDefinition> argDefs = new ArrayList<>();

for (IFunctionArgument arg : func.getArgs()) {
if (arg.isAnnotationPresent(RequestBean.class)) {
return getArgTypes(func.getName(), arg, arg.getType(), registry, creatingTypes);
List<GraphQLArgumentDefinition> args = getArgTypes(func.getName(), arg, arg.getType(), registry, creatingTypes);
field.setArguments(args);

break;
} else if (arg.isAnnotationPresent(Name.class)) {
GraphQLType type = buildGraphQLType(arg.getType(), null, registry, creatingTypes, true);
GraphQLArgumentDefinition argDef = new GraphQLArgumentDefinition();
Expand All @@ -77,11 +80,12 @@ private List<GraphQLArgumentDefinition> getArgDefinitions(IFunctionModel func, T

argDefs.add(argDef);
} else if (arg.getType().getRawClass() == ApiRequest.class) {
return getArgTypes(func.getName(), arg, arg.getType().getTypeParameters().get(0), registry,
List<GraphQLArgumentDefinition> args = getArgTypes(func.getName(), arg, arg.getType().getTypeParameters().get(0), registry,
creatingTypes);
field.setArguments(args);
break;
}
}
return argDefs;
}

private List<GraphQLArgumentDefinition> getArgTypes(String funcName, IFunctionArgument arg, IGenericType type,
Expand Down
19 changes: 10 additions & 9 deletions nop-ioc/src/main/java/io/nop/ioc/impl/BeanDefinition.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import io.nop.ioc.model.BeanModel;
import io.nop.ioc.model.BeanPointcutModel;
import io.nop.ioc.model.BeanValue;
import io.nop.ioc.model.IBeanModel;
import io.nop.xlang.expr.ExprConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -109,7 +110,7 @@ public void addConfigDependant(String beanId) {
configDependants.add(beanId);
}

public boolean isIocForceInit(){
public boolean isIocForceInit() {
return beanModel.isIocForceInit();
}

Expand All @@ -133,16 +134,16 @@ public void setConstructorAutowired(boolean constructorAutowired) {
this.constructorAutowired = constructorAutowired;
}

public boolean isRemoved(){
public boolean isRemoved() {
return removed;
}

public void setRemoved(boolean removed){
public void setRemoved(boolean removed) {
this.removed = removed;
}

public boolean isIocDefault(){
if(beanModel instanceof BeanModel)
public boolean isIocDefault() {
if (beanModel instanceof BeanModel)
return ((BeanModel) beanModel).isIocDefault();
return false;
}
Expand Down Expand Up @@ -271,14 +272,14 @@ public String toString() {
}

public String getId() {
if (beanModel instanceof BeanModel)
return ((BeanModel) beanModel).getId();
if (beanModel instanceof IBeanModel)
return ((IBeanModel) beanModel).getId();
return beanModel.getEmbeddedId();
}

public Set<String> getNames() {
if (beanModel instanceof BeanModel)
return ((BeanModel) beanModel).getName();
if (beanModel instanceof IBeanModel)
return ((IBeanModel) beanModel).getName();
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import io.nop.api.core.ioc.BeanContainerStartMode;
import io.nop.api.core.ioc.IBeanContainer;
import io.nop.api.core.util.Guard;
import io.nop.api.core.util.SourceLocation;
import io.nop.commons.lang.IClassLoader;
import io.nop.core.lang.xml.XNode;
Expand All @@ -22,6 +23,7 @@
import io.nop.ioc.impl.IBeanClassIntrospection;
import io.nop.ioc.model.BeanAliasModel;
import io.nop.ioc.model.BeanConditionModel;
import io.nop.ioc.model.BeanConfigModel;
import io.nop.ioc.model.BeanConstantModel;
import io.nop.ioc.model.BeanConstantValue;
import io.nop.ioc.model.BeanImportModel;
Expand Down Expand Up @@ -201,6 +203,16 @@ BeansDefinition buildDefinition(BeansModel beansModel, String trace) {
beans.addBean(bean);
}

for (BeanConfigModel configModel : beansModel.getIocConfigs()) {
BeanDefinition bean = new BeanDefinition(configModel);
bean.setTrace(trace);

Guard.notEmpty(bean.getId(), "id");

normalizeDefaultBean(configModel);
beans.addBean(bean);
}

return beans;
}

Expand All @@ -218,7 +230,6 @@ Class<?> tryGetStaticFieldType(BeanDefinition bean, BeanConstantValue model) {

/**
* 条件构建的bean的id可能和default bean相同,为了最后能融合在一个全局bean配置文件中,自动为default bean的id增加一个前缀。
*
*/
void normalizeDefaultBean(BeanModel beanModel) {
if (!beanModel.isIocDefault())
Expand All @@ -239,6 +250,25 @@ void normalizeDefaultBean(BeanModel beanModel) {
}
}

void normalizeDefaultBean(BeanConfigModel beanModel) {
if (!beanModel.isIocDefault())
return;

beanModel.setIocDefault(false);
String id = beanModel.getId();
if (!id.startsWith(IocConstants.DEFAULT_ID_PREFIX)) {
beanModel.setId(IocConstants.DEFAULT_ID_PREFIX + id);
beanModel.addName(id);

BeanConditionModel condition = beanModel.getIocCondition();
if (condition == null) {
condition = new BeanConditionModel();
beanModel.setIocCondition(condition);
}
condition.addMissingBean(id);
}
}

BeanDefinition newUtilFactoryBean(SourceLocation loc, String id, int initOrder, boolean iocDefault,
IBeanPropValue value) {
BeanModel beanModel = new BeanModel();
Expand Down
14 changes: 13 additions & 1 deletion nop-ioc/src/main/java/io/nop/ioc/model/BeanConfigModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,20 @@

import io.nop.ioc.model._gen._BeanConfigModel;

public class BeanConfigModel extends _BeanConfigModel {
import java.util.HashSet;
import java.util.Set;

public class BeanConfigModel extends _BeanConfigModel implements IBeanModel {
public BeanConfigModel() {

}

public void addName(String name) {
Set<String> names = getName();
if (names == null) {
names = new HashSet<>();
setName(names);
}
names.add(name);
}
}
2 changes: 1 addition & 1 deletion nop-ioc/src/main/java/io/nop/ioc/model/BeanModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import java.util.HashSet;
import java.util.Set;

public class BeanModel extends _BeanModel {
public class BeanModel extends _BeanModel implements IBeanModel {

public BeanModel() {

Expand Down
4 changes: 4 additions & 0 deletions nop-ioc/src/main/java/io/nop/ioc/model/BeanValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ public BeanValue() {

}

public String getId(){
return null;
}

@JsonIgnore
public String getEmbeddedId() {
return embeddedId;
Expand Down
9 changes: 9 additions & 0 deletions nop-ioc/src/main/java/io/nop/ioc/model/IBeanModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.nop.ioc.model;

import java.util.Set;

public interface IBeanModel {
String getId();

Set<String> getName();
}
Loading

0 comments on commit 709faf6

Please sign in to comment.