Skip to content
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

Upgrade to @apollo/client@3 #2560

Merged
merged 83 commits into from
Aug 27, 2020
Merged

Upgrade to @apollo/client@3 #2560

merged 83 commits into from
Aug 27, 2020

Conversation

sirugh
Copy link
Contributor

@sirugh sirugh commented Jul 16, 2020

https://www.apollographql.com/docs/react/migrating/apollo-client-3-migration/

Notes from upgrade

  • Apollo freezes response objects. Must copy/clone before modifying.
  • read functions replace local resolvers
  • merge functions handle ambiguous normalization (ie. arrays of data, objects with no keys)
    • Be on the lookout for apollo client warnings about problems merging. These are usually found through manual steps and I've tried to capture and fix all that I could but it is possible I have missed some.

TODO:

  • Any PRs merged that use react-hooks or v2 syntax will need to get updated on this PR.
  • Upgrade to 3.2.0 for global next fetch policy (or open a followup ticket).
  • Double check package.json to ensure new client is added and old deps are removed.
  • Migrate gift option resolvers to type policiesDone
  • Migrate payment information resolvers to type policies Done
  • Convert to possibleTypes from dataFromObject - Done
  • Fix PDP Overfetching - Resolved by removing manual cache lookups in useProduct
  • Fix Cart Overfetching (and/or blowing away old data ie region/country). Could not repro anymore.
  • Release/Update apollo-link-mutation-queue since operation.toKey is no longer a function. Done.
  • Handle skip bug. - [email protected] resolved this.
  • graphiql schema view breaks now This appears broken on develop.

@sirugh sirugh added the version: Major This changeset includes incompatible API changes and its release necessitates a Major version bump. label Jul 16, 2020
@sirugh sirugh changed the title Apollogql upgrade v3 Upgrade to @apollo/client@3 Jul 16, 2020
@PWAStudioBot PWAStudioBot added pkg:pagebuilder pkg:peregrine pkg:pwa-buildpack pkg:upward-js Pertains to upward-js reference implementation of UPWARD. pkg:upward-spec Pertains to UPWARD specification package. pkg:venia-concept pkg:venia-ui labels Jul 16, 2020
@PWAStudioBot
Copy link
Contributor

PWAStudioBot commented Jul 16, 2020

Fails
🚫 Missing "Description" section. Please add it back, with detail.
🚫 Missing "Verification Steps" section. Please add it back, with detail.
🚫

No linked issue found. Please link a relevant open issue by adding the text "closes #<issue_number>" or "closes JIRA-<issue_number>" in your PR.

Warnings
⚠️ Found the word "TODO" in the PR description. Just letting you know incase you forgot :)
Messages
📖

Access a deployed version of this PR here. Make sure to wait for the "pwa-pull-request-deploy" job to complete.

📖 DangerCI Failures related to missing labels/description/linked issues/etc will persist until the next push or next nightly build run (assuming they are fixed).

If your PR is missing information, check against the original template here. At a minimum you must have the section headers from the template and provide some information in each section.

Generated by 🚫 dangerJS against d1877ca

@devops-pwa-codebuild
Copy link
Collaborator

devops-pwa-codebuild commented Jul 21, 2020

Performance Test Results

The following fails have been reported by WebpageTest. These numbers indicates a possible performance issue with the PR which requires further manual testing to validate.

https://pr-2560.pwa-venia.com : LH Performance Expected 0.85 Actual 0.35, LH Accessibility Expected 1 Actual 0.97, LH Best Practices Expected 1 Actual 0.93, WPT Cache Expected 90 Actual 35.666666666667
https://pr-2560.pwa-venia.com/venia-tops.html : LH Performance Expected 0.75 Actual 0.31, LH Best Practices Expected 1 Actual 0.93
https://pr-2560.pwa-venia.com/valeria-two-layer-tank.html : LH Performance Expected 0.8 Actual 0.57, LH Accessibility Expected 0.9 Actual 0.89, LH Best Practices Expected 1 Actual 0.93, WPT Cache Expected 65 Actual 44

@@ -18,7 +18,7 @@ export const useCartPage = props => {

const { called, data, loading } = useQuery(getCartDetails, {
fetchPolicy: 'cache-and-network',
// Don't make this call if we don't have a cartId
nextFetchPolicy: 'cache-first',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This answers one of my previous questions, thanks for the context 👍

@@ -1,5 +1,5 @@
const AbstractCompiledResource = require('./AbstractCompiledResource');
const gql = require('graphql-tag');
const gql = require('@apollo/client');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting that they chose to make gql the default export of @apollo/client.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After looking at the other imports, this one needs to be changed.

import { gql } from '@apollo/client';

But actually, I don't even know what this file is. Is this generated code?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly, not sure if this file is even used at all. I'll poke around, but yes this does need to be changed to at least:

const apolloClient = require('apollo/client');
const { gql } = apolloClient;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

read(cached) {
return cached || null;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually pretty readable

introspectionQueryResultData: UNION_AND_INTERFACE_TYPES
})
typePolicies,
possibleTypes: POSSIBLE_TYPES
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May want to drop a comment where POSSIBLE_TYPES comes from since it's not otherwise present in this file

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done 8eafc91

Query: {
fields: {
cart: {
// Replaces @connection(key: "Cart")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes me think we should update our Mutation PII Cheat Sheet.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if the use of the policy for the Cart field warrants the update, but I think I get where you're going with this. We could preempt this and move all the connection directives from mutations to this policies file. For example this mutation:

export const REMOVE_GIFT_CARD_MUTATION = gql`
    mutation removeGiftCard($cartId: String!, $giftCardCode: String!) {
        removeGiftCardFromCart(
            input: { cart_id: $cartId, gift_card_code: $giftCardCode }
        ) @connection(key: "removeGiftCardFromCart") {
            ...
       }
    }
`;

would become

{
   Mutation: {
       fields: {
           removeGiftCardFromCart: {
               keyArgs: () => 'removeGiftCardFromCart'
           }
       }
   }
}

Then the rule in the cheatsheet, and something we'd have to enforce in reviews, would be to add a field keyArg rule to the policies file when adding a new mutation.

I'd like to get opinions on that though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image from Gyazo

Copy link
Contributor Author

@sirugh sirugh Aug 25, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thoughts:

  1. Have each component use the useTypePolicies hook to add type policy keys for their mutations at runtime. This would prevent initial bundle bloat and aid in chunking.

  2. Declare all mutation keys up front. Adds bytes to bundle and hurts chunking if we aren't using all mutations.
    note: We could generate this automatically from the schema if we knew what fields we consider "pii".

  3. Use a global no-cache policy for mutations. This would remove the need to do any of this, but would force us to refactor our app because we use mutation results to auto update cache. We would have to switch to using refetchQueries after each mutation.

/**
* @deprecated Resolvers are deprecated in ApolloClient v3.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I never know where to draw that line.

  1. This is going to be a major change anyway, right?
  2. When do we actually remove this code?

jimbo
jimbo previously approved these changes Aug 24, 2020
Copy link
Contributor

@jimbo jimbo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is good from my end. There are still minor things to resolve, but I'm good when others' feedback is resolved.

/**
* @deprecated Resolvers are deprecated in ApolloClient v3.
Copy link
Contributor

@jimbo jimbo Aug 24, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Yes, it's absolutely going to be a major change.
  2. We haven't actually formalized a policy for removal, only deprecation.

I think the pacing of our major releases is a bit too fast for us to change code from "canonical" to "deprecated" to "removed" just like that. Now that we're properly marking things with @deprecated, we can start the conversation around an actual removal timeframe.

Copy link
Contributor

@tjwiebell tjwiebell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice improvements with making type policies composable, this looks ready for QA 👍

@dpatil-magento dpatil-magento merged commit b5b9da3 into develop Aug 27, 2020
@dpatil-magento dpatil-magento deleted the apollogql-upgrade-v3 branch August 27, 2020 16:56
sirugh added a commit that referenced this pull request Aug 27, 2020
* Copy frozen array before sorting.

* Upgrade to Apollo v3 - imports and package.json

Signed-off-by: sirugh <[email protected]>

* Fixup some frozen object and array modifications

Signed-off-by: sirugh <[email protected]>

* Replaces cacheKeyFromType

Signed-off-by: sirugh <[email protected]>

* Remove apollo/client from direct peregrine dependency

Signed-off-by: sirugh <[email protected]>

* prettier

Signed-off-by: sirugh <[email protected]>

* Upgrade to v3.0.2

Signed-off-by: sirugh <[email protected]>

* Add more type policies for cart fields

* Always use incoming items as result

Signed-off-by: sirugh <[email protected]>

* Require url for ProductImage key

Signed-off-by: sirugh <[email protected]>

* Fixup typePolicies

Signed-off-by: sirugh <[email protected]>

* Replace cart connection key with keyArgs

Signed-off-by: sirugh <[email protected]>

* Use constants for custom key fields

Signed-off-by: sirugh <[email protected]>

* Ignore CartPrices id

Signed-off-by: sirugh <[email protected]>

* Trying to perfect usage of type policy

Signed-off-by: sirugh <[email protected]>

* Update mutation link version and ensure 3.0.2 client is used

Signed-off-by: sirugh <[email protected]>

* Use a skippable query hook as a stop gap while skip is fixed in apolloclient core

Signed-off-by: sirugh <[email protected]>

* Revert "Use a skippable query hook as a stop gap while skip is fixed in apolloclient core"

This reverts commit 832f6f4.

* Upgrade to apolloclient v3.1.2

Signed-off-by: sirugh <[email protected]>

* Undo pagebuilder package auto format

* Re add updated product cache hit logic.

* use client import

Signed-off-by: sirugh <[email protected]>

* Move price  query under correct parent

* Fall back to use product id when no url_key

* Protect against undefined cachedata

* Ensure currency is fetched on sign in to properly merge carts

Signed-off-by: sirugh <[email protected]>

* ProductSearch must prefetch just like category
page does otherwise the pdp will crash from missing cache data

Signed-off-by: sirugh <[email protected]>

* Use apollo client import for new communications page

Signed-off-by: sirugh <[email protected]>

* Dont throw if fragment is missing all data from cache

Signed-off-by: sirugh <[email protected]>

* Revert "ProductSearch must prefetch just like category"

This reverts commit fef660f.

* revert package indentation

Signed-off-by: sirugh <[email protected]>

* only try to merge incoming if exists

Signed-off-by: sirugh <[email protected]>

* Merge cart shipping addresses using index within address array. This is not perfect but the alternative is to overfetch since cache data would always be incoming.

Signed-off-by: sirugh <[email protected]>

* Use nocache in lazy query definition otherwise it caches. Removes unnecessary code fetch from fragment

Signed-off-by: sirugh <[email protected]>

* Remove unused connection key from cart query

Signed-off-by: sirugh <[email protected]>

* use nextFetchPolicy of cache-first to prevent network request when cache data changes

Signed-off-by: sirugh <[email protected]>

* Rename gift cards query to make similar to applied coupons

Signed-off-by: sirugh <[email protected]>

* Handle addition and removal of gift cards in client merge function

Signed-off-by: sirugh <[email protected]>

* Rename stuff to prepare function for moving to util

Signed-off-by: sirugh <[email protected]>

* Rename react-hooks imports

Signed-off-by: sirugh <[email protected]>

* Missed import

Signed-off-by: sirugh <[email protected]>

* Refactor deprecated gift_option resolvers

* Add missing stock status to items review fragment

Signed-off-by: sirugh <[email protected]>

* Remove deprecated payment information resolvers.

Signed-off-by: sirugh <[email protected]>

* Use no-cache policy to avoid requery on cache changes

Signed-off-by: sirugh <[email protected]>

* Add merge for CustomerAddress.street as value is in array

Signed-off-by: sirugh <[email protected]>

* Readd graphql resolution as upgrading to 15 or removing it entirely causes validator to fail

Signed-off-by: sirugh <[email protected]>

* Fixup gql validation errors

Signed-off-by: sirugh <[email protected]>

* Fix incorrect boolean response from type policy

Signed-off-by: sirugh <[email protected]>

* Removes unused/invalid import

Co-authored-by: Jimmy Sanford <[email protected]>

* Cast errors to boolean

Co-authored-by: Jimmy Sanford <[email protected]>

* Applied coupons is an array with single value or null, so we don't need the merge function

Signed-off-by: sirugh <[email protected]>

* Add useTypePolicies hook and use in product talon

* Only show indicator if loading and no data

* Simplify read fn

Signed-off-by: sirugh <[email protected]>

* Update mapProduct docs

* Add docs for getPossibleTypes

Signed-off-by: sirugh <[email protected]>

* Revert currency fetch where unnecessary. Add comment explaining what happens if currency value is not fetched in merge mutation.

* Coupon Code uses skip instead of lazy query

Signed-off-by: sirugh <[email protected]>

* Use merge:true, same thing anyways

Signed-off-by: sirugh <[email protected]>

* Removes prefetch optimizations and url_key identifier use that resulted in cache misses. We found that url_key is not unique so it cannot be used as product interface id

Signed-off-by: sirugh <[email protected]>

* Move type policies and merge functions into separate files

Signed-off-by: sirugh <[email protected]>

* add line breaks and comments

Signed-off-by: sirugh <[email protected]>

* Always use incoming values when merging gift cards and shipping addresses

* Update comments.

* Feedback

Signed-off-by: sirugh <[email protected]>

* Fixup tests

Signed-off-by: sirugh <[email protected]>

* Update jsdoc for useTypePolicies

Signed-off-by: sirugh <[email protected]>

* use apollo/client instead of react-hooks

Signed-off-by: sirugh <[email protected]>

Co-authored-by: Jimmy Sanford <[email protected]>
Co-authored-by: Devagouda <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg:pagebuilder pkg:peregrine pkg:pwa-buildpack pkg:upward-js Pertains to upward-js reference implementation of UPWARD. pkg:upward-spec Pertains to UPWARD specification package. pkg:venia-concept pkg:venia-ui version: Major This changeset includes incompatible API changes and its release necessitates a Major version bump.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants