You don't have access to this class

Keep learning! Join and start boosting your career

Aprovecha el precio especial y haz tu profesi贸n a prueba de IA

Antes: $249

Currency
$209
Suscr铆bete

Termina en:

2 D铆as
5 Hrs
4 Min
10 Seg

Filtros en Prisma

8/23
Resources

Contributions 5

Questions 2

Sort by:

Want to see more contributions, questions and answers from the community?

Al hacer el include de los Attributes con Prisma, el tipo que se retorna ya no es solo Avocado, sino: Avocado & { attributes: Attributes | null }, as铆 que deber铆amos cambiar esto en el tipo de retorno de nuestros resolvers. El c贸digo final se ver铆a algo como esto:

import { Avocado, Attributes } from '@prisma/client'

const getAllAvocados = async (
  parent: unknown,
  args: unknown,
  { orm }: ResolverContext
): Promise<(Avocado & { attributes: Attributes | null })[]> => {
  try {
    return await orm.avocado.findMany({
      include: {
        attributes: true
      }
    })
  } catch (error) {
    console.error('Error getting all the avocados')
    console.error(error)
    throw error
  }
}

const getOneAvocado = async (
  parent: unknown,
  {
    id
  }: {
    id: string
  },
  { orm }: ResolverContext
): Promise<
  | (Avocado & {
      attributes: Attributes | null
    })
  | null
> => {
  try {
    return await orm.avocado.findUnique({
      where: { id: parseInt(id) },
      include: {
        attributes: true
      }
    })
  } catch (error) {
    console.error('Error getting all the avocados')
    console.error(error)
    throw error
  }
}

export { getAllAvocados, getOneAvocado }

Inclusi贸n de relaciones

<h5>馃毀 Documentaci贸n</h5> <h5>馃洜 Commit</h5>

.
Con prisma, es posible manipular la respuesta deseada mediante dos opciones de retorno:

  1. Select - Para retornar campos espec铆ficos o de aquellos basadas en su relaci贸n.
  2. Include - Para incluir relaciones en la misma respuesta.

.
Ya que GraphQL permite din谩micamente seleccionar los campos, con Prisma solamente definimos que ser谩 tratado en el su request la distinci贸n de dichos campos.
.
Por ejemplo, siguiendo la clase del profesor, anexo simplemente la opci贸n include.

// Account.model.ts
export default class AccountModel extends Model<Account, Query, Payload> {
   // more code...
    /**
     * @description Find an account by id.
     * @param {Query} query
     * @returns Account */
    async findUnique(query: Query): Promise<Account> {
        return await this.client.findUnique({
            where: query,
            include: { directions: true },
        })
    }
   // more code...
}

Revisando que la referencia est茅 definida en el schema de graphql:

        type Account {
            id: ID!
            email: String!
            password: String!
            directions: [Direction]
        }

El avocado del profesor:

{
  "data": {
    "name": "Zutano Avocado",
    "price": 1.25,
    "image": "/images/zutano.jpg",
    "description": "The Zutano avocado is a cold hardy, consistent producing avocado variety. It resembles the Fuerte in appearance but is less flavorful but more cold hardy. The green fruits are abovate in shape with waxy bumps on the skin. The flesh has a low oil but high water content which causes it to have a more fibrous texture.",
    "shape": "Pear",
    "hardiness": "-5 潞C",
    "taste": "Splendid, is an avocado"
  }
}

Como recomendacion para poder leer archivos json en el navegador de una mejor manera les recomiendo esta extencion
extencion