【问题标题】:graphql trouble accessing items in objectgraphql 访问对象中的项目时遇到问题
【发布时间】:2018-11-28 02:38:32
【问题描述】:

我仍在尝试学习 graphql,但在访问数据库对象中的项目时遇到问题。在我的客户端代码中,id 和 createdAt 的数据显示得很好,就在我添加对象时,我得到了错误:

Expected Iterable, but did not find one for field Users.profile

我不确定我的代码缺少什么:

解析器:

 Query: {
    getUser(root, args, { userId }) {
      const {id } = args;
      const user = User.findOne({
          id
        });

        return user;
    }
  },

架构

const User = `
type User{
 id: String!
 createdAt: Date
 profile: [Profile]
}

type Profile {
 name: String!
 email: String!
}

extend type Query {
  getUser(
    id: String!
  ): User
}

我如何在我的客户端代码中调用它:

const getUser = gql`
  query getUser($id: String!) {
    getUser(id: $id) {
      id
      createdAt
      profile {
        name
        email
      }
    }
  }
`;

这是它在 MongoDB 数据库中的样子:

user{
  _id: "22222"
  createdAt: 11/22/2018
  profile:{
    name: "Chris"
    email: "chris@emample.com"
  }
}   `

【问题讨论】:

    标签: graphql graphql-js


    【解决方案1】:

    以防将来对某人有所帮助,我必须将我的对象设置为 JSON 才能使其正常工作。

    const User = `
    type User{
     id: String!
     createdAt: Date
     profile: JSON
    }
    
    extend type Query {
      getUser(
        id: String!
      ): User
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-11
      • 1970-01-01
      • 2020-10-25
      • 2020-07-03
      • 2021-03-31
      • 1970-01-01
      相关资源
      最近更新 更多