Search code examples
apitypesgraphqlschemaapollo

what would be the correct type for "tools" in EnterpriseInput input?


input EnterpriseInput {
    cnpj: String
    name: String
    email: String
    password: String
    tools: Tool
        
}

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!
    # updateEnterprise( id: ID!, input: EnterpriseInput!): Enterprise!
    deleteEnterprise( id: ID!): Boolean,

}

Error message on console:

C:\Users\Elias\code\web\backEnd-gestordeferramentas\node_modules\graphql\type\validate.js:69
throw new Error(errors.map(function (error) {
^
Error: The type of EnterpriseInput.tools must be Input Type but got: Tool.
at assertValidSchema (C:\Users\Elias\code\web\backEnd-…


Solution

  • In a mutation you cannot use standard Object type and must use Input type instead. And in input type you must use input type

    So instead of

    input EnterpriseInput {
    cnpj: String
    name: String
    email: String
    password: String
    tools: Tool      
    }
    

    Try this

    input ToolInput {
       id: ID!
       name: String!
       brand: String!
       cod: String!
       qnt: Int!
    }
    
    input EnterpriseInput {
      cnpj: String
      name: String
      email: String
      password: String
      tools: ToolInput    
    }