Skip to content

Commit

Permalink
通过@GraphQLReturn来指定动态属性的graphQL的列表元素对象类型
Browse files Browse the repository at this point in the history
  • Loading branch information
entropy-cloud committed Jan 20, 2025
1 parent a7eca10 commit 2b6d027
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import io.nop.api.core.annotations.core.PropertySetter;
import io.nop.api.core.annotations.data.DataBean;
import io.nop.api.core.annotations.data.ImmutableBean;
import io.nop.api.core.annotations.graphql.GraphQLReturn;
import io.nop.api.core.convert.ConvertHelper;
import io.nop.api.core.exceptions.NopException;
import io.nop.commons.util.ClassHelper;
Expand Down Expand Up @@ -200,7 +201,7 @@ private void initDefaultValues(BeanModel beanModel) {
Object defaultValue = propModel.getGetter()
.getProperty(bean, propModel.getName(), DisabledEvalScope.INSTANCE);
((BeanPropertyModel) propModel).setDefaultValue(defaultValue);
}catch (Exception e){
} catch (Exception e) {
LOG.trace("nop.reflect.ignore-default-value-error", e);
}
}
Expand Down Expand Up @@ -457,6 +458,10 @@ private void processPropAnnotations(BeanPropertyModel prop, IAnnotatedElement an
BizObjName bizObjName = ann.getAnnotation(BizObjName.class);
if (bizObjName != null) {
prop.setBizObjName(bizObjName.value());
} else {
GraphQLReturn graphQLReturn = ann.getAnnotation(GraphQLReturn.class);
if (graphQLReturn != null && !graphQLReturn.bizObjName().isEmpty())
prop.setBizObjName(graphQLReturn.bizObjName());
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.nop.graphql.core.reflection;

import io.nop.api.core.annotations.graphql.GraphQLReturn;
import io.nop.core.reflect.hook.IPropGetMissingHook;
import io.nop.graphql.core.ast.GraphQLType;
import io.nop.graphql.core.schema.TypeRegistry;
import org.junit.jupiter.api.Test;

import java.util.List;

import static org.junit.jupiter.api.Assertions.assertTrue;

public class TestReflectionGraphQLTypeFactory {
@Test
public void testDynamicObjName() {
TypeRegistry registry = new TypeRegistry();
GraphQLType type = ReflectionGraphQLTypeFactory.INSTANCE.buildGraphQLType(ResultBean.class, registry, false);
String source = registry.getType(type.getNamedTypeName()).toSource();
System.out.println(source);
assertTrue(source.contains("[MyObject]"));
}

public static class ResultBean {
List<IPropGetMissingHook> list;

@GraphQLReturn(bizObjName = "MyObject")
public List<IPropGetMissingHook> getList() {
return list;
}
}
}

0 comments on commit 2b6d027

Please sign in to comment.