apollo-link-state icon indicating copy to clipboard operation
apollo-link-state copied to clipboard

Fragments for mixed local/remote queries

Open silvainSayduck opened this issue 6 years ago • 6 comments

I am trying to setup a query which fetches data both from the local cache and from the server. A problem arises when I introduce fragments, as the fragment used for my local query is sent to the server, which crashes the query.

query Query {
  remoteObject {
    ...RemoteObjectFields
  }
  localObject @client {
    ...LocalObjectFields
  }
}
fragment RemoteObjectFields on RemoteObject {
  remoteField1
  remoteField2
}
fragment LocalObjectFields on LocalObject {
  localField1
  localField2
}

This results in the server returning

errors = [
  { message: "No such type LocalObject, so it can't be a fragment condition", ... },
  { message: "Fragment LocalObjectFields was defined, but not used", ... }
]

Hardcoding the local fields as below works

query Query {
  remoteObject {
    ...RemoteObjectFields
  }
  localObject @client {
    localField1
    localField2
  }
}
fragment RemoteObjectFields on RemoteObject {
  remoteField1
  remoteField2
}

Is there a way to declare a fragment as local, or am I just missing something completely?

silvainSayduck avatar Jun 26 '18 08:06 silvainSayduck

I think this is also a duplicate of https://github.com/apollographql/apollo-client/issues/3371

silvainSayduck avatar Jun 26 '18 08:06 silvainSayduck

@silvainSayduck Have you found any viable workarounds for this?

jtag05 avatar Oct 24 '18 18:10 jtag05

Nope, still using hardcoded local fields...

silvainSayduck avatar Oct 29 '18 15:10 silvainSayduck

In the soon to be released new version of this project, fragments that only have @client fields / selection sets are automatically excluded from being sent to the server. So if you adjusted your local fragment definition like:

fragment LocalObjectFields on LocalObject {
  localField1 @client
  localField2 @client
}

it will be fully stripped before your query hits the server.

hwillson avatar Oct 29 '18 18:10 hwillson

any update on this?

evgenyboxer avatar Nov 19 '18 00:11 evgenyboxer

@hwillson Is there a PR I can contribute to?

HofmannZ avatar Dec 07 '18 09:12 HofmannZ