Skip to content

Commit

Permalink
增加动态GraphQL类型的示例
Browse files Browse the repository at this point in the history
  • Loading branch information
entropy-cloud committed Jan 22, 2025
1 parent 1a7df8e commit c8730d0
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.nop.auth.service;

import io.nop.api.core.annotations.autotest.NopTestConfig;
import io.nop.api.core.beans.ApiRequest;
import io.nop.autotest.junit.JunitBaseTestCase;
import io.nop.core.model.selection.FieldSelectionBeanParser;
import io.nop.graphql.core.IGraphQLExecutionContext;
import io.nop.graphql.core.ast.GraphQLOperationType;
import io.nop.graphql.core.engine.IGraphQLEngine;
import jakarta.inject.Inject;
import org.junit.jupiter.api.Test;

import java.util.Map;

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

@NopTestConfig(localDb = true, initDatabaseSchema = true)
public class TestDynamicItem extends JunitBaseTestCase {
@Inject
IGraphQLEngine graphQLEngine;


@Test
public void testDynamicItem() {
ApiRequest<Map<String, Object>> request = new ApiRequest<>();
request.setData(Map.of("id", "123"));
request.setSelection(FieldSelectionBeanParser.fromText(null, "name, rows{nickName},rows2{userName}"));

IGraphQLExecutionContext context = graphQLEngine.newRpcContext(GraphQLOperationType.query,
"NopAuthUser__testDynamicItem", request);
Map<String, Object> result = (Map<String, Object>) graphQLEngine.executeRpc(context).get();
assertEquals("a", result.get("name"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.nop.auth.service.biz;

import io.nop.api.core.annotations.data.DataBean;
import io.nop.orm.IOrmEntity;

import java.util.List;

@DataBean
public class ItemData {
private String name;
private List<IOrmEntity> rows;
private List<IOrmEntity> rows2;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public List<IOrmEntity> getRows() {
return rows;
}

public void setRows(List<IOrmEntity> rows) {
this.rows = rows;
}

public List<IOrmEntity> getRows2() {
return rows2;
}

public void setRows2(List<IOrmEntity> rows2) {
this.rows2 = rows2;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<schema x:schema="/nop/schema/schema/schema.xdef" xmlns:x="/nop/schema/xdsl.xdef">
<props>
<prop name="name">
<schema type="String"/>
</prop>

<prop name="rows" graphql:type="[NopAuthUser]" />

<prop name="rows2">
<schema>
<item bizObjName="NopAuthUser" />
</schema>
</prop>
</props>
</schema>
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,21 @@
</bo:DoFindPage>
</source>
</query>

<query name="testDynamicItem">
<arg name="id" type="String" />
<return>
<schema x:extends="ItemSchema.schema.xml" />
</return>

<source>
import io.nop.auth.service.biz.ItemData;

const ret = new ItemData();
ret.name = "a";
ret.rows = [];
return ret;
</source>
</query>
</actions>
</biz>

0 comments on commit c8730d0

Please sign in to comment.