You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GraphQL中EnterpriseInput的tools字段应使用什么正确类型

报错原因

GraphQL 严格区分输出类型(用type关键字定义,用于接口返回值结构声明)和输入类型(用input关键字定义,用于接口入参结构声明),规则要求:所有嵌套在输入类型内的自定义类型,必须也是输入类型,不能直接使用普通输出类型。
你当前代码中直接在EnterpriseInput里使用了输出类型Tool,不符合语法规则,因此触发报错。

解决方法

根据你的业务场景选择对应方案即可:

方案1:创建企业时需要同步新增关联的Tool数据

如果调用createEnterprise接口时需要传入Tool的完整字段,由后端同时创建企业和关联的Tool数据,你需要先定义Tool对应的输入类型,再修改EnterpriseInput的tools字段:

  1. 新增Tool输入类型:
input ToolInput {
    # 若Tool的id由后端自动生成,不需要前端传入,可省略id字段
    name: String!
    brand: String!
    cod: String!
    qnt: Int!
}
  1. 修改EnterpriseInput的tools字段:
input EnterpriseInput {
    cnpj: String
    name: String
    email: String
    password: String
    # 和输出类型的数组结构对应,用ToolInput数组作为类型
    tools: [ToolInput!]
}

方案2:创建企业时仅关联已有的Tool数据

如果Tool已经提前创建完成,调用接口仅需要绑定已有Tool的id,不需要传入Tool的完整字段,你可以直接修改EnterpriseInput的tools字段为ID数组即可,不需要额外定义ToolInput:

input EnterpriseInput {
    cnpj: String
    name: String
    email: String
    password: String
    tools: [ID!]
}
修正后完整Schema示例(按方案1实现)
# 新增Tool输入类型
input ToolInput {
    name: String!
    brand: String!
    cod: String!
    qnt: Int!
}

input EnterpriseInput {
    cnpj: String
    name: String
    email: String
    password: String
    tools: [ToolInput!]
}

type Enterprise {
    id: ID!
    cnpj: String!
    name: String!
    email: String!
    password: String!
    tools: [Tool]
}

type Tool {
    id: ID!
    name: String!
    brand: String!
    cod: String!
    qnt: Int!
}

type Query {
    enterprises: [Enterprise]
    enterprise( id: ID!): Enterprise
}

type Mutation {
    createEnterprise( input: EnterpriseInput!): Enterprise!
    deleteEnterprise( id: ID!): Boolean
}

内容的提问来源于stack exchange,提问作者Elias Amaral

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 18:54:01