FieldPolicy and Nested Fields

I’m consuming a GraphQL api which create “namespace” wrappers around all queries. So all queries have the format similar to:

query Planet($id: ID!){
  appName {
    schemaName {
      planet(id:$id) {
        name
        mass
        distance
      }
    }
  }
}

Automatically, all types are prepended with appName_schemaName_. So the above’s cache id would be appName_schemaName_Planet:$id.

My understanding is this is not standard design and there wouldn’t exist options to set the application namespace or schema namespace.

I can see in the Apollo Client that the cache is building correctly. I can also use the explorer and tell it to Load from cache, and that works. But if I run the same useQuery twice, it will never use the cache.

I believe their “namespace” means I need to setup Field Policies anywhere I would like to use caching. Please let me know if that is not correct. When attempting to setup a Query Field Policy, I cannot figure out how to set one up for anything but “appName”.

const cache = new InMemoryCache({
    typePolicies: {
        Query: {
            fields: {
                appName: {
                    read() {
                        console.log('This logs on all queries');
                    }
                },
                schemaName: {
                    read() {
                        console.log('This never logs');
                    }
                },
                planet: {
                    read() {
                        console.log('This never logs');
                    }
                }
            }
        }
    }
})

Am I correct that I need to setup a FieldPolicy? And how would I set one up for planet?

Thanks!

@system Did you get this figured out? I also have the same problem

Update: mine wasn’t working because I also had a field policy for the namespace. Make sure you don’t have typePolicies: { appName: () => {...} } defined.

1 Like