“无法查询类型为“查询”的字段“可用”

时间:2019-02-14 20:38:54

标签: graphql

当我尝试获取包括可用属性的所有产品时,收到错误消息“无法查询字段“可用””,类型为“查询”。

查询呼叫

const res = await fetch(`graphql?query={products{
    id
    name
    available
  }}`);

架构

export default `
type Product {
  id: ID!
  name: String! 
  available: [Available]
}

type Available  {
  stock: String
  size: String
}

input AvailableInput {
  stock: String
  size: String
}

type Query {
  product(name: String!): Product
  products: [Product] 
}

type Mutation {
  addProduct(name: String! available:[AvailableInput] ): Product
}
`;

2 个答案:

答案 0 :(得分:0)

您的type Product在架构中的外观如何?在查询available字段作为Product

的子字段时,您可能缺少该字段

答案 1 :(得分:0)

您的available字段返回type Available的对象,因此第一个错误是查询字符串中没有子字段。

尝试一下:

const res = await fetch(`graphql?query={products{
    id
    name
    available {
        stock
        size
    }
  }}`);