DEV Community

Sivaprasadgalla
Sivaprasadgalla

Posted on

The variables input contains a field name 'file' that is not defined for input object type 'CreateProductInput'

please help me to resolve this error

schema.graphql

type Market @model @searchable @auth(rules: [{ allow : owner}, { allow: private, operations: [read]}]){
  id: ID!
  products: [Product] @hasMany
  name: String!
  tags: [String]
  owner: String!
  createAt: String
}

type Product @model @auth(rules: [{ allow : owner}, { allow: private, operations: [read]}]){
  id: ID!
  description: String!
  market: Market @hasOne
  file: S3Object!
  price: Float!
  shipped: Boolean!
  owner: String
  createAt: String
}

type S3Object @model @auth(rules: [{ allow : owner}, {allow: private, operations: [read]}]){
  bucket: String!
  region: String!
  key: String!
}

type User @auth(rules: [{ allow : owner}, { allow: private, operations: [read]}])
@model(
  queries: { get: "getUser" },
  mutations : { create:"regesterUser", update : "updateUser" },
  subscriptions : null
){
  id: ID!
  orders:[Order] @hasMany
  username: String!
  email: String!
  registered: Boolean
}

type Order @model(
  queries: null,
  mutations: { create: "createOrder" },
  subscriptions: null
)
@auth(rules: [{ allow : owner}, { allow: private, operations: [read]}])
{
  id: ID!
  product: Product @hasOne
  user: User @belongsTo
  ShippingAddress: ShippingAddress 
}

type ShippingAddress @model @auth(rules: [{ allow : owner}, {allow: private, operations: [read]}]){
  city: String!
  country: String!
  address_line1: String!
  address_state: String!
  address_zip: String!
}
Enter fullscreen mode Exit fullscreen mode

Note: graphql transimission version is 2

CreateProduct :

handleAddProduct = async() => {
     try{
       this.setState({ isUploading: true });
       const visibility = "public";
       const {identityId} = await Auth.currentCredentials()
       const filename = `/${visibility}/${identityId}/${Date.now()}-${this.state.image.name}`
       const uploadedFile = await Storage.put(filename, this.state.image.file, {
         contentType: this.state.image.type,
         progressCallback: progress => {
          console.log(`Uploaded: ${progress.loaded}/${progress.total}`);
          const percentUploaded = Math.round((progress.loaded/progress.total) * 100)
          this.setState({ percentUploaded });
         }
       });
       const file = {
         key: uploadedFile.key,
         bucket: aws_exports.aws_user_files_s3_bucket,
         region: aws_exports.aws_project_region
       }
       const input = {
         productMarketId: this.props.marketId,
         description: this.state.description,
         shipped: this.state.shipped,
         price: convertCentsToDollar(this.state.price),
         file
       }
       const result = API.graphql(graphqlOperation(createProduct, { input }))
       console.log('Created Product', result);
       Notification({
         title: "success",
         message: "Product Successfully created!",
         type: "success" 
       })
       this.setState({ ...initialState });

     }catch(err){
      console.error('Error adding product', err);
     }
   }
Enter fullscreen mode Exit fullscreen mode

Please check the code and help me to resolve the error

Thank you

Top comments (0)