0
votes

I have the following datamodel:

type Tvshow {
  id: ID! @unique
  title: String!
  pricing: [Pricing]
  startDate: DateTime!
  endDate: DateTime!
  subscribers: [Tvshowsubscription!]
 .....
}

type FavoriteTvshow {
  id: ID! @unique
  tvshow: Tvshow!
  user: User!
}

type User {
  id: ID! @unique
  name: String
  email: String! @unique
  password: String
  googleID: String @unique
  resetToken: String
  resetTokenExpiry: String
  permissions: [Permission]
  address: Address
  phone: String
  favorites: [FavoriteTvshow!]
  tvshowSubscriptions: [Tvshowsubscription!]
}

I have my custom Tvshow resolver using addFragmentToInfo:

resolver-queries.js

const Query = {
 ...
  favoriteTvshows: forwardTo('db'),
  tvshow: (parent, args, ctx, info) => {
    const fragment = `fragment EnsureComputedFields on Tvshow { pricing { minQuantity maxQuantity unitPrice} subscribers { id }}`
    return ctx.db.query.tvshow({}, addFragmentToInfo(info, fragment))
  },
 ....
};

tvshow-resolver.js

const Tvshow = {
  countSubscribers: (parent) => {
    return parent.subscribers.length;
  },
}

This is an example, I have more computed fields for Tvshow

I can query Tvshows with countSubscribers, It works fine doing something like this:

query SINGLE_TVSHOW_QUERY($id: ID!) {
  tvshow(where: { id: $id }) {
    id
    title
    pricing {
      minQuantity
      maxQuantity
      unitPrice
    }
    startDate
    endDate
    countSubscribers
  }
}

But what I want to do is to get all the favorite Tvshows from an user returning the countSubscribers, a query for that could be something like this:

query FAVORITES_FROM_USER($userId: ID!) {
    favoriteTvshows(where: { user: {id: $userId} }) {
      tvshow {
        id
        title
        startDate
        endDate
        countSubscribers
      }
    }
  }

The problem is that when I query this, in the tvshow-resolver.js I mentioned before, the parent doesn’t have any subscribers object

1

1 Answers

0
votes

The error was very silly but I will post it anyway. I needed subscribers in the query

query FAVORITES_FROM_USER($userId: ID!) {
    favoriteTvshows(where: { user: {id: $userId} }) {
      tvshow {
        id
        title
        startDate
        endDate
        subscribers { <--- 
          id
          quantity
        }
        countSubscribers
      }
    }
  }

That way the parent in tvshow-resolver.js will have subscribers object