1

我正在尝试将一组字符串从 reactjs 发送到 micronaut,以测试数据是否成功传递,我在前端有这样的东西:

const [filterName, setName] = useState(["test"]);
const age = 1;

const { data: people } = useSWR([`
     query ($name:[String], $age:Int){
        getName(name:$name, age:$age){ ... }`, filterName, age], (query, filterName, age) => fetcher(query, {filterName, age}))

我的 micronaut 后端看起来像这样:

@GraphQLQuery(name = "getName")
List<User> getName (@GraphQLArgument(name = "name") List<String> name,
                    @GraphQLArgument(name = "age") Integer age ){

    System.out.println("name: " + name.toString());
    System.out.println("age: " + age);
}

我可以打印年龄,但名称打印为空。难道我做错了什么?我尝试使用 ArrayList 代替 List,仍然无法正确接收名称。希望您能提供帮助。谢谢!

4

1 回答 1

1

它是 fetcher 中的变量。它需要匹配 micronaut 中声明的 api 名称。本来应该是这样的:

const [filterName, setName] = useState(["test"]);
const age = 1;

const { data: people } = useSWR([`
     query ($name:[String], $age:Int){
        getName(name:$name, age:$age){ ... }`, filterName, age], (query, filterName, age) => fetcher(query, {name:filterName, age}))
于 2021-04-22T08:33:55.490 回答