Link one to many relationship

prisma

#1

How do I link the Vendor type to a Bill Type?

When using the createBill function, I get an error “Error: The type of Mutation.createBill(vendor:) must be Input Type but got: Vendor.”

type Vendor {
  id: ID!
  createdAt: DateTime!
  name: String!
  contact: String!
  address: String!
  addressTwo: String!
  city: String!
  state: String!
  country: String!
  postedBy: User
}

type Bill {
  id: ID!
  vendor: Vendor
  date: String!
  account: String!
  amount: Int!
  postedBy: User
}

type Mutation {
  createBill(vendor: Vendor, date: String!, account: String!, amount: Int!): Bill
}

function createBill(parent, args, context) {
  return context.prisma.createBill({
      vendor: { connect: { id: args.vendor}},
      date: args.date,
      account: args.account,
      amount: args.amount,
  })
}


#2

in your createBill mutation, you should have vendor: ID instead of vendor: Vendor.


#4

This is a side project to learn GraphQL base on the Hackernews tutorial. Below is my solution, not sure if it’s the right approach but I learned something and it works.

type Mutation {
  createBill(vendor: String, date: String!, account: String!, amount: Int!): Bill
}

function createBill(parent, args, context) {
  return context.prisma.createBill({
    vendor: args.vendor,
    date: args.date,
    account: args.account,
    amount: args.amount,
    vendorId: {connect: {id: args.vendor}},
    accountId: {connect: {id: args.account}},
  })
}

function vendorId(parent, args, context){
  return context.prisma.bill({id: parent.id}).vendorId()
}

function bills(parent, args, context) {
  return context.prisma.vendor({ id: parent.id }).bills()
}

Below is my mutation

mutation {
  createBill(
     vendor: "cjw00gvoset9i0b42ko0g5b6a"
     date: "12/31/2018"
     account: "cjvx96w7n1qhp0b42vd29n036"
     amount: 100
  ){
    id
    date
    amount
    vendorId{
      name
    }
    accountId{
      name
    }
  }
}

{
  "data": {
    "createBill": {
      "id": "cjwis1j65c4fs0b12f73y98aw",
      "date": "12/31/2018",
      "amount": 100,
      "vendorId": {
        "name": "Stumptown Coffee"
      },
      "accountId": {
        "name": "Supplies Expense"
      }
    }
  }
}

#5

Thanks for the tip, you were right about the id.


#6

This topic was automatically closed 45 days after the last reply. New replies are no longer allowed.