-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Prevent @client
fields from being passed into the Link chain
#5982
Conversation
84466b2
to
011b62e
Compare
Codecov Report
@@ Coverage Diff @@
## master #5982 +/- ##
==========================================
- Coverage 95.06% 95.05% -0.01%
==========================================
Files 89 89
Lines 3705 3702 -3
Branches 911 879 -32
==========================================
- Hits 3522 3519 -3
Misses 160 160
Partials 23 23 Continue to review full report at Codecov.
|
011b62e
to
e53d34b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great! Can we add something to the migration guide about this?
@@ -35,6 +35,9 @@ | |||
- **[BREAKING?]** Remove `fixPolyfills.ts`, except when bundling for React Native. If you have trouble with `Map` or `Set` operations due to frozen key objects in React Native, either update React Native to version 0.59.0 (or 0.61.x, if possible) or investigate why `fixPolyfills.native.js` is not included in your bundle. <br/> | |||
[@benjamn](https://github.com/benjamn) in [#5962](https://github.com/apollographql/apollo-client/pull/5962) | |||
|
|||
- **[BREAKING]** Apollo Client 2.x allowed `@client` fields to be passed into the `link` chain if `resolvers` were not set in the constructor. This allowed `@client` fields to be passed into Links like `apollo-link-state`. Apollo Client 3 enforces that `@client` fields are local only, meaning they are no longer passed into the `link` chain, under any circumstances. <br/> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very clear and to the point!
I've got a question around this topic. Going forward, how do I get auto-mocked results for @client directives? Previously, I would use SchemaLink and Switching to MockedProvider is painful and I hate trying to exactly match the request/result dance. Using a real ApolloProvider with a mocked schema is a WAY better DX... but I can't see how I'm going to get @client directive queries automatically mocked in AC3. I'd love to see some documentation around this topic! Let me know if you'd like an issue opened for tracking or anything else. PS: Love the work you guys are doing here ❤️ |
@jeffreyffs I totally agree that a real ApolloProvider with a mocked schema is way better DX. We use that for all our client side tests. In our case, we don't mock the |
Thanks for the feedback @jeffreyffs @dylanwulf. We're making this change as we really want to enforce that const cache = new InMemoryCache({
typePolicies: {
Query: {
fields: {
isLoggedIn() {
return faker.random.boolean();
},
user() {
return {
firstName: faker.name.firstName(),
lastName: faker.name.lastName(),
__typename: 'User'
}
}
}
}
}
}); This is just a quick example, as there are definitely more sophisticated and automatic methods that could be developed by the community for this (e.g. a function that wraps all of your type policies and when called in |
e53d34b
to
b93016a
Compare
@hwillson Thanks for the detailed response! Don't get me wrong, I'm all for simplifying local state management. The current implementation is probably my least favourite thing about Apollo Client. I like that idea and I look forward to utilizing Type/field policies in the near future. |
Apollo Client 2.x allowed `@client` fields to be passed into the `link` chain if `resolvers` were not set in the constructor. This allowed `@client` fields to be passed into Links like `apollo-link-state`. Apollo Client 3 enforces that `@client` fields are local only, meaning they are no longer passed into the `link` chain, under any circumstances. We're making this change as we're slowly starting to deprecate the Local State API (and local resolvers) in favor of the new Type Policies API. Since we want people to be able to use the Type Policies API (and `@client`) without local resolvers, we don't want to force them to always define `resolvers: {}` in their `ApolloClient` constructor, to prevent `@client` fields from being passed into the `link` chain. If anyone wants to preserve this behavior in AC 3, they can consider using their own directive (instead of `@client`), and using that directive in the `link` chain.
3d5d395
to
7e2fa1c
Compare
To continue on @freshollie point, this now means that when mocking For anyone passing by, here's a simple example that should give you an idea on how to mock local queries: const GET_POSTS = gql`
query {
GetPosts @client {
id
}
}
`;
// broken - doesn't work because the query has the @client directive
const mock = {
request: {
query: GET_POSTS,
},
result: {
data: {
GetPosts: [{ id: 1 }],
},
},
};
render(
<MockedProvider mocks={[mock]} addTypename={false}>
<Posts />
</MockedProvider>
);
// works as expected
const resolvers = {
Query: {
GetPosts: () => [{ id: 1 }],
},
};
render(
<MockedProvider resolvers={resolvers} addTypename={false}>
<Posts />
</MockedProvider>
); |
Apollo Client 2.x allowed
@client
fields to be passed into thelink
chain ifresolvers
were not set in the constructor. This allowed@client
fields to be passed into Links likeapollo-link-state
. Apollo Client 3 enforces that@client
fields are local only, meaning they are no longer passed into thelink
chain, under any circumstances. We're making this change as we're slowly starting to deprecate the Local State API (and local resolvers) in favor of the new Type Policies API. Since we want people to be able to use the Type Policies API (and@client
) without local resolvers, we don't want to force them to always defineresolvers: {}
in theirApolloClient
constructor, to prevent@client
fields from being passed into thelink
chain.If anyone wants to preserve this behavior in AC 3, they can consider using their own directive (instead of
@client
), and using that directive in thelink
chain.