Json GraphiQL在获取所有记录时返回空值

Json GraphiQL在获取所有记录时返回空值,json,graphql,graphql-js,graphiql,Json,Graphql,Graphql Js,Graphiql,我正在尝试构建一个简单的GraphQL模式。我在localhost上运行了一个json服务器端点,它返回一些简单的json数据 我可以在提供id时返回用户。在架构中执行此操作的查询如下所示: staff: { type: StaffType, args: { id: { type: GraphQLInt }}, resolve(parentValue, { id }) { return axios.get(`http://localhost:3000/st

我正在尝试构建一个简单的GraphQL模式。我在localhost上运行了一个json服务器端点,它返回一些简单的json数据

我可以在提供id时返回用户。在架构中执行此操作的查询如下所示:

staff: {
    type: StaffType,
    args: { id: { type: GraphQLInt }},
    resolve(parentValue, { id }) {
        return axios.get(`http://localhost:3000/staff/${id}`)
            .then(resp => resp.data);
    }
}
{
    staff(id: 1){
        id
        Name
    }
}
type: new GraphQLList(StaffType),
GraphQL查询如下所示:

staff: {
    type: StaffType,
    args: { id: { type: GraphQLInt }},
    resolve(parentValue, { id }) {
        return axios.get(`http://localhost:3000/staff/${id}`)
            .then(resp => resp.data);
    }
}
{
    staff(id: 1){
        id
        Name
    }
}
type: new GraphQLList(StaffType),
它在GraphiQL客户端中返回:

{
  "data": {
    "staff": {
      "id": 1,
      "Name": "John Doe"
    }
  }
}
当我尝试返回所有用户时,问题就开始了。我创建了另一个查询,试图从
http://localhost:3000/staff
没有传入参数。如果我在浏览器中直接访问此url,它将按预期返回所有用户。但是,如果我通过GraphiQL尝试,它似乎返回一个对象,但所有字段都为空。GraphiQL或控制台中都没有给出错误。代码示例如下:

模式中的查询

allStaff: {
    type: StaffType,
    resolve(parentValue, args) {
        return axios.get(`http://localhost:3000/staff/`)
            .then(resp => resp.data);
    }
}
GraphiQL查询

{
  allStaff {
    id
    Name
  }
}
我得到的结果

{
  "data": {
    "allStaff": {
      "id": null,
      "Name": null,
    }
  }
}
如果我控制台记录resp.data,则会返回数据,正如我所说,当我直接通过浏览器访问数据时,端点会完全按照预期返回数据

我错过什么了吗


感谢

JSON数据可能是一个用户对象数组,但GraphQL仍然只需要一个。这是因为在您的模式中,您已经将
allStaff
的返回类型定义为
StaffType
。您需要使用列表包装器对其进行包装,如下所示:

staff: {
    type: StaffType,
    args: { id: { type: GraphQLInt }},
    resolve(parentValue, { id }) {
        return axios.get(`http://localhost:3000/staff/${id}`)
            .then(resp => resp.data);
    }
}
{
    staff(id: 1){
        id
        Name
    }
}
type: new GraphQLList(StaffType),

JSON数据可能是一个用户对象数组,但GraphQL仍然只需要一个。这是因为在您的模式中,您已经将
allStaff
的返回类型定义为
StaffType
。您需要使用列表包装器对其进行包装,如下所示:

staff: {
    type: StaffType,
    args: { id: { type: GraphQLInt }},
    resolve(parentValue, { id }) {
        return axios.get(`http://localhost:3000/staff/${id}`)
            .then(resp => resp.data);
    }
}
{
    staff(id: 1){
        id
        Name
    }
}
type: new GraphQLList(StaffType),

哦,当然!非常感谢,谢谢!我太笨了,不明白为什么这么简单的查询不起作用。现在有道理了,干杯,当然!非常感谢,谢谢!我太笨了,不明白为什么这么简单的查询不起作用。现在有道理了,干杯