Mutation to create a relationship record

Asked

Viewed 80 times

0

Ambience:

Prisma GraphQL GraphQL Yoga NodeJS PostGresql

I’m trying to make a mutation register a person and an address for that person. However Person and Address are separate tables, and I’m not able to make the relationship between them.

In the documentation of Prisma is directed to use the Create but it doesn’t work, and I always get the same mistake.

Documentation: https://www.prisma.io/docs/prisma-client/basic-data-access/writing-data-JAVASCRIPT-rsc6/#nested-Object-Writes

Error:

{
  "data": null,
  "errors": [
    {
      "message": "Variable \"$_v0_data\" got invalid value { cnpj: \"059.852.698.22\", nome: \"Greice K Tomasi\", user: { connect: [Object] }, create: { endereco: [Object] } }; Field value.endereco of required type EnderecoCreateOneInput! was not provided.\nVariable \"$_v0_data\" got invalid value { cnpj: \"059.852.698.22\", nome: \"Greice K Tomasi\", user: { connect: [Object] }, create: { endereco: [Object] } }; Field \"user\" is not defined by type PessoaCreateInput.\nVariable \"$_v0_data\" got invalid value { cnpj: \"059.852.698.22\", nome: \"Greice K Tomasi\", user: { connect: [Object] }, create: { endereco: [Object] } }; Field \"create\" is not defined by type PessoaCreateInput.",
      "locations": [],
      "path": []
    }
  ]

I’m trying this way:

datamodel.prism

 type Pessoa {
  id: ID ! @unique
  cnpj: String!
  nome: String!
  endereco: Endereco!
}
type Endereco {
  id: ID ! @unique
  rua: String!
  numero: String
  cep: String!
  pessoa: Pessoa!
}

schema.graphql

type Mutation {

createPessoa(cnpj: String!, nome: String!, endereco: EnderecoInput!): Pessoa!

}

type Pessoa {
  id: ID !
  cnpj: String!
  nome: String!
  endereco: Endereco!
}

type Endereco {
  id: ID !
  rua: String!
  numero: String
  cep: String!
}

input EnderecoInput {
  rua: String!
  numero: String
  cep: String!
}

Mutation.js

async function createPessoa (_, args, ctx, info) {
  const userId = getUserId(ctx)
  return ctx.db.mutation.createPessoa({
    data: {
      cnpj: args.cnpj,
      nome: args.nome,
      user: {
        connect: {  id: userId }
      },
      endereco: {
        rua: args.endereco.rua,
        numero: args.endereco.numero,
        cep: args.endereco.cep
        }
     }
  })
}

module.exports = {
  createPessoa
}

I’m trying to populate Mutation this way:

mutation {
  createPessoa (
    nome:"Greice K Tomasi", 
    cnpj:"059.852.698.22",
    endereco: {
      rua: "Germano Klann",
      numero: "547",
      cep: "89254220"
    }
  )
  {
    nome
  }
}

I put the Project in Git in case anyone would rather watch over it:

https://github.com/Denis-String/CursoGraphQL

(To run the project you need Docker)

Steps to start: Inside the project folder:

  • npm install

  • Docker-Compose up

In another terminal (with the command docker-compose up spinning)

  • prism deploy

1 answer

0


Solved the problem:

In my mutation.js needed to disconnect the User with Pessoa for mine datamodel.prisma wasn’t modeled to make that call.

After that, I had to put the note create after endereco and thus catch as args.endereco.

Follow code snippet after changes.

async function createPessoa (_, args, ctx, info) {
  const userId = getUserId(ctx)
  return ctx.db.mutation.createPessoa({
    data: {
      cnpj: args.cnpj,
      nome: args.nome,
      endereco: {
        create: {
            rua: args.endereco.rua,
            numero: args.endereco.numero,
            cep: args.endereco.cep
        }
      } 
     }
  })
}

module.exports = {
  createPessoa
}

Solved with the help of @Plínio Naves

Browser other questions tagged

You are not signed in. Login or sign up in order to post.