Search code examples
pythongraphqlhasura

Is there any way to insert data in parent and child model in one graphql mutation


I have a model named Project and a CustomProject model that inherits from it. Currently, I have two mutations one to add a project and one to add a custom project

class Project(models.Model):
    url = models.URLField(blank=True)
    description = models.TextField()

class CustomProject(Project): 
    role = models.CharField(max_length=15, null=True, blank=True)

mutation InsertProjects(
  $projects: [graph_project_insert_input!]! = [
    {
      url: "https://github.com/smsajjadzaidi/FixedRouteSuggestion"
      description: "_test_desc_"
    }
  ]
) {
  projects: insert_graph_project(
    objects: $projects
    on_conflict: {
      constraint: graph_project_pkey
      update_columns: [url, description]
    }
  ) {
    returning {
      id
    }
    affected_rows
  }
}


mutation InsertCustomProject(
  $projects: [custom_project_insert_input!]! = [
    {
      project_ptr_id: 2
      role:"author"
    }
  ]
) {
  customprojects: insert_custom_project(
    objects: $projects
    on_conflict: {
      constraint: custom_project_pkey
      update_columns: [role]
    }
  ) {
    returning {
      graph_project{
        id
      }
    }
    affected_rows
  }
}

Need a way to do the above in one mutation


Solution

  • mutation InsertCustomProject(
      $url: String = "_____test______"
      $description: String = "___test____"
      $role: String = "author"
      
      $title: String = "__test__"
    ) {
      repositories: insert_customproject_one(
        object: {
          role: $role
          project: {
            data: {
              url: $url
              description: $description
              
            }
            on_conflict: {
              constraint: project_pkey
              update_columns: [url, description]
            }
          }
        }
        on_conflict: {
          constraint: customproject_pkey
          update_columns: [role]
        }
      ) {
        project_id: project_ptr_id
      }
    }