Do type policies apply to every reference object of that type?


I am trying to setup some local fields for a specific type used in my system (let’s just call it Student). This Student type is returned in a number of queries, a students query that returns Student[], a student query that returns just the single Student, and other queries such as a course query that returns and array of students inside of it like this (where students is and array of Student):

query Course($id: string!) {
  course(id: $id) {
    students {
      fullName @client

And my type policy for Student is defined as such:

const cache = new InMemoryCache({
  typePolicies: {
    Student: {
      fields: {
        fullName: {
          read(_, { readField }) {
            const firstName = readField<string>('firstName');
            const lastName = readField<string>('lastName');
            return `${firstName} ${lastName}`;

So the type policy is being respected/used for my student and students query, however, when the Student object is nested inside of another object being queried like that course query above, the type policy doesn’t seem to be used. My local fields are not part of the Student object in the response or in cache.

I was wondering if this was expected behavior? Or if maybe there’s something more that has to be done to get this to work? I was also wondering if the type policy should be respected for mutations’ responses as well (imagine an updateCourse mutation that returns a similar object as the course query does)?


Hi @dorkycam :wave: welcome to the forum!

First of all, yes, a typePolicy defined for a key should apply to all objects with a __typename that matches that key. In this case, "Student".

I think the implementation details matter here. What is the data being returned by the server for your Course query? I’m looking specifically at whether the elements in the students list have "__typename": "Student"so that the proper type policy is being invoked. Apologies if you’ve already looked into this, just trying to better understand your application.