manikandanraji/twitter-clone-backend

How can I add many to many relation values in mutation step

volkankorkmaz opened this issue · 2 comments

Hi,

I have two tables as below. I try to add a page with multiple modules value. If I send one module value, it works. But I want to send multiple values. How can I send them? Thanks

Table Structures

type Module {
id: ID!
name: String!
pages: [Page]
createdBy: User
updatedBy: User
createdAt: String
updatedAt: String
}

type Page {
id: ID!
name: String!
url: String!
modules: [Module]
createdBy: User
updatedBy: User
createdAt: String
updatedAt: String
}

Add Page Schema

type Mutation {
addPage(
name: String!
url: String!
# relation fields
modules: [ModuleInput]
createdBy: ID!
updatedBy: ID!
): Page!
}

input ModuleInput {
id: ID
}

Add Page Mutation

  const page = await ctx.prisma
    .createPage({
      ...args,
      modules: {
        connect: {
          id: args.modules[0].id,
        },
      },
      createdBy: {
        connect: {
          id: args.createdBy,
        },
      },
      updatedBy: {
        connect: {
          id: args.updatedBy,
        },
      },
    })
    .$fragment(PAGE_FRAGMENT);
// args -> name, url, createdBy, updatedBy, modules
const { name, url, createdBy, updatedBy, modules } = args;

// first the create the page
const page = await ctx.prisma.createPage({
	name,
	url,
	modules: {
	  set: modules
	}
	createdBy: {
	  connect: {
	    id: createdBy
	  }
	},
	updatedBy: {
	  connect: {
	    id: updatedBy
	  }
	}
});

Thanks for your quick response. But I got below error.

Reason: 'modules.set' Field 'set' is not defined in the input type 'ModuleCreateManyWithoutPagesInput'

Prisma Schema

type Module {
id: ID! @id
name: String!

pages: [Page] @relation(name: "ModuleToPage")
companies: [Company!]! @relation(name: "ModuleToCompany")

isActive: Boolean! @default(value:false)
isDelete: Boolean! @default(value:false)
createdBy: User @relation(name:"ModuleToUserCreated")
createdAt: DateTime! @createdat
updatedBy: User @relation(name:"ModuleToUserUpdated")
updatedAt: DateTime! @updatedAt
deletedBy: User @relation(name:"ModuleToUserDeleted")
deletedAt: DateTime
}

type Page {
id: ID! @id
name: String!
url: String!
modules: [Module] @relation(name: "ModuleToPage")
isActive: Boolean! @default(value:false)
isDelete: Boolean! @default(value:false)
createdBy: User @relation(name:"PageToUserCreated")
createdAt: DateTime! @createdat
updatedBy: User @relation(name:"PageToUserUpdated")
updatedAt: DateTime! @updatedAt
deletedBy: User @relation(name:"PageToUserDeleted")
deletedAt: DateTime
}