From feeb9e0a407937f568a2efad6ca91a81790cb679 Mon Sep 17 00:00:00 2001
From: viktorstrate <viktorstrate@gmail.com>
Date: Thu, 23 Sep 2021 19:00:14 +0200
Subject: [PATCH] Remove Album.coverID + fix thumbnail refresh

- Album.coverID has been removed from `schema.graphql`
- Make sure the proper informations are fetched from the set/reset album
  cover mutations such that Apollo can update the cache accordingly.
---
 api/graphql/generated.go                      | 44 +------------------
 api/graphql/schema.graphql                    |  2 -
 ui/src/Pages/AlbumPage/AlbumPage.tsx          |  2 +-
 .../AlbumPage/__generated__/albumQuery.ts     |  1 +
 ui/src/Pages/AllAlbumsPage/AlbumsPage.tsx     |  5 +--
 .../__generated__/getMyAlbums.ts              | 19 ++++----
 ui/src/Pages/SharePage/AlbumSharePage.tsx     |  1 +
 .../__generated__/shareAlbumQuery.ts          |  1 +
 ui/src/components/sidebar/AlbumCovers.tsx     | 14 +++++-
 .../sidebar/__generated__/resetAlbumCover.ts  | 21 +++++++++
 .../sidebar/__generated__/setAlbumCover.ts    | 23 +++++++++-
 11 files changed, 73 insertions(+), 60 deletions(-)

diff --git a/api/graphql/generated.go b/api/graphql/generated.go
index 63ad314..b8220c9 100644
--- a/api/graphql/generated.go
+++ b/api/graphql/generated.go
@@ -57,7 +57,6 @@ type DirectiveRoot struct {
 
 type ComplexityRoot struct {
 	Album struct {
-		CoverID     func(childComplexity int) int
 		FilePath    func(childComplexity int) int
 		ID          func(childComplexity int) int
 		Media       func(childComplexity int, order *models.Ordering, paginate *models.Pagination, onlyFavorites *bool) int
@@ -371,13 +370,6 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in
 	_ = ec
 	switch typeName + "." + field {
 
-	case "Album.coverID":
-		if e.complexity.Album.CoverID == nil {
-			break
-		}
-
-		return e.complexity.Album.CoverID(childComplexity), true
-
 	case "Album.filePath":
 		if e.complexity.Album.FilePath == nil {
 			break
@@ -1947,7 +1939,7 @@ type Album {
 
   shares: [ShareToken!]!
 
-  coverID: Int
+  #coverID: Int
 }
 
 type MediaURL {
@@ -3337,38 +3329,6 @@ func (ec *executionContext) _Album_shares(ctx context.Context, field graphql.Col
 	return ec.marshalNShareToken2ᚕᚖgithubᚗcomᚋphotoviewᚋphotoviewᚋapiᚋgraphqlᚋmodelsᚐShareTokenᚄ(ctx, field.Selections, res)
 }
 
-func (ec *executionContext) _Album_coverID(ctx context.Context, field graphql.CollectedField, obj *models.Album) (ret graphql.Marshaler) {
-	defer func() {
-		if r := recover(); r != nil {
-			ec.Error(ctx, ec.Recover(ctx, r))
-			ret = graphql.Null
-		}
-	}()
-	fc := &graphql.FieldContext{
-		Object:     "Album",
-		Field:      field,
-		Args:       nil,
-		IsMethod:   false,
-		IsResolver: false,
-	}
-
-	ctx = graphql.WithFieldContext(ctx, fc)
-	resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) {
-		ctx = rctx // use context from middleware stack in children
-		return obj.CoverID, nil
-	})
-	if err != nil {
-		ec.Error(ctx, err)
-		return graphql.Null
-	}
-	if resTmp == nil {
-		return graphql.Null
-	}
-	res := resTmp.(*int)
-	fc.Result = res
-	return ec.marshalOInt2ᚖint(ctx, field.Selections, res)
-}
-
 func (ec *executionContext) _AuthorizeResult_success(ctx context.Context, field graphql.CollectedField, obj *models.AuthorizeResult) (ret graphql.Marshaler) {
 	defer func() {
 		if r := recover(); r != nil {
@@ -10361,8 +10321,6 @@ func (ec *executionContext) _Album(ctx context.Context, sel ast.SelectionSet, ob
 				}
 				return res
 			})
-		case "coverID":
-			out.Values[i] = ec._Album_coverID(ctx, field, obj)
 		default:
 			panic("unknown field " + strconv.Quote(field.Name))
 		}
diff --git a/api/graphql/schema.graphql b/api/graphql/schema.graphql
index b9cbbce..e534491 100644
--- a/api/graphql/schema.graphql
+++ b/api/graphql/schema.graphql
@@ -278,8 +278,6 @@ type Album {
   path: [Album!]!
 
   shares: [ShareToken!]!
-
-  coverID: Int
 }
 
 type MediaURL {
diff --git a/ui/src/Pages/AlbumPage/AlbumPage.tsx b/ui/src/Pages/AlbumPage/AlbumPage.tsx
index a8d1031..869b809 100644
--- a/ui/src/Pages/AlbumPage/AlbumPage.tsx
+++ b/ui/src/Pages/AlbumPage/AlbumPage.tsx
@@ -25,6 +25,7 @@ const ALBUM_QUERY = gql`
         id
         title
         thumbnail {
+          id
           thumbnail {
             url
           }
@@ -93,7 +94,6 @@ function AlbumPage({ match }: AlbumPageProps) {
       offset: 0,
       limit: 200,
     },
-    fetchPolicy: 'network-only',
   })
 
   const { containerElem, finished: finishedLoadingMore } =
diff --git a/ui/src/Pages/AlbumPage/__generated__/albumQuery.ts b/ui/src/Pages/AlbumPage/__generated__/albumQuery.ts
index 698ea85..2414a76 100644
--- a/ui/src/Pages/AlbumPage/__generated__/albumQuery.ts
+++ b/ui/src/Pages/AlbumPage/__generated__/albumQuery.ts
@@ -19,6 +19,7 @@ export interface albumQuery_album_subAlbums_thumbnail_thumbnail {
 
 export interface albumQuery_album_subAlbums_thumbnail {
   __typename: 'Media'
+  id: string
   /**
    * URL to display the media in a smaller resolution
    */
diff --git a/ui/src/Pages/AllAlbumsPage/AlbumsPage.tsx b/ui/src/Pages/AllAlbumsPage/AlbumsPage.tsx
index 9f6326e..b0fe58a 100644
--- a/ui/src/Pages/AllAlbumsPage/AlbumsPage.tsx
+++ b/ui/src/Pages/AllAlbumsPage/AlbumsPage.tsx
@@ -10,6 +10,7 @@ const getAlbumsQuery = gql`
       id
       title
       thumbnail {
+        id
         thumbnail {
           url
         }
@@ -19,9 +20,7 @@ const getAlbumsQuery = gql`
 `
 
 const AlbumsPage = () => {
-  const { error, data } = useQuery<getMyAlbums>(getAlbumsQuery, {
-    fetchPolicy: 'network-only',
-  })
+  const { error, data } = useQuery<getMyAlbums>(getAlbumsQuery)
 
   return (
     <Layout title="Albums">
diff --git a/ui/src/Pages/AllAlbumsPage/__generated__/getMyAlbums.ts b/ui/src/Pages/AllAlbumsPage/__generated__/getMyAlbums.ts
index 33fb85e..51041d3 100644
--- a/ui/src/Pages/AllAlbumsPage/__generated__/getMyAlbums.ts
+++ b/ui/src/Pages/AllAlbumsPage/__generated__/getMyAlbums.ts
@@ -8,34 +8,35 @@
 // ====================================================
 
 export interface getMyAlbums_myAlbums_thumbnail_thumbnail {
-  __typename: "MediaURL";
+  __typename: 'MediaURL'
   /**
    * URL for previewing the image
    */
-  url: string;
+  url: string
 }
 
 export interface getMyAlbums_myAlbums_thumbnail {
-  __typename: "Media";
+  __typename: 'Media'
+  id: string
   /**
    * URL to display the media in a smaller resolution
    */
-  thumbnail: getMyAlbums_myAlbums_thumbnail_thumbnail | null;
+  thumbnail: getMyAlbums_myAlbums_thumbnail_thumbnail | null
 }
 
 export interface getMyAlbums_myAlbums {
-  __typename: "Album";
-  id: string;
-  title: string;
+  __typename: 'Album'
+  id: string
+  title: string
   /**
    * An image in this album used for previewing this album
    */
-  thumbnail: getMyAlbums_myAlbums_thumbnail | null;
+  thumbnail: getMyAlbums_myAlbums_thumbnail | null
 }
 
 export interface getMyAlbums {
   /**
    * List of albums owned by the logged in user.
    */
-  myAlbums: getMyAlbums_myAlbums[];
+  myAlbums: getMyAlbums_myAlbums[]
 }
diff --git a/ui/src/Pages/SharePage/AlbumSharePage.tsx b/ui/src/Pages/SharePage/AlbumSharePage.tsx
index 084c1fa..1549376 100644
--- a/ui/src/Pages/SharePage/AlbumSharePage.tsx
+++ b/ui/src/Pages/SharePage/AlbumSharePage.tsx
@@ -27,6 +27,7 @@ export const SHARE_ALBUM_QUERY = gql`
         id
         title
         thumbnail {
+          id
           thumbnail {
             url
           }
diff --git a/ui/src/Pages/SharePage/__generated__/shareAlbumQuery.ts b/ui/src/Pages/SharePage/__generated__/shareAlbumQuery.ts
index f97fc19..fa6cb19 100644
--- a/ui/src/Pages/SharePage/__generated__/shareAlbumQuery.ts
+++ b/ui/src/Pages/SharePage/__generated__/shareAlbumQuery.ts
@@ -19,6 +19,7 @@ export interface shareAlbumQuery_album_subAlbums_thumbnail_thumbnail {
 
 export interface shareAlbumQuery_album_subAlbums_thumbnail {
   __typename: 'Media'
+  id: string
   /**
    * URL to display the media in a smaller resolution
    */
diff --git a/ui/src/components/sidebar/AlbumCovers.tsx b/ui/src/components/sidebar/AlbumCovers.tsx
index 034b2ac..5db328f 100644
--- a/ui/src/components/sidebar/AlbumCovers.tsx
+++ b/ui/src/components/sidebar/AlbumCovers.tsx
@@ -17,13 +17,25 @@ const RESET_ALBUM_COVER_MUTATION = gql`
   mutation resetAlbumCover($albumID: ID!) {
     resetAlbumCover(albumID: $albumID) {
       id
+      thumbnail {
+        id
+        thumbnail {
+          url
+        }
+      }
     }
   }
 `
 const SET_ALBUM_COVER_MUTATION = gql`
   mutation setAlbumCover($coverID: ID!) {
     setAlbumCover(coverID: $coverID) {
-      coverID
+      id
+      thumbnail {
+        id
+        thumbnail {
+          url
+        }
+      }
     }
   }
 `
diff --git a/ui/src/components/sidebar/__generated__/resetAlbumCover.ts b/ui/src/components/sidebar/__generated__/resetAlbumCover.ts
index dbd452f..10a3627 100644
--- a/ui/src/components/sidebar/__generated__/resetAlbumCover.ts
+++ b/ui/src/components/sidebar/__generated__/resetAlbumCover.ts
@@ -7,9 +7,30 @@
 // GraphQL mutation operation: resetAlbumCover
 // ====================================================
 
+export interface resetAlbumCover_resetAlbumCover_thumbnail_thumbnail {
+  __typename: 'MediaURL'
+  /**
+   * URL for previewing the image
+   */
+  url: string
+}
+
+export interface resetAlbumCover_resetAlbumCover_thumbnail {
+  __typename: 'Media'
+  id: string
+  /**
+   * URL to display the media in a smaller resolution
+   */
+  thumbnail: resetAlbumCover_resetAlbumCover_thumbnail_thumbnail | null
+}
+
 export interface resetAlbumCover_resetAlbumCover {
   __typename: 'Album'
   id: string
+  /**
+   * An image in this album used for previewing this album
+   */
+  thumbnail: resetAlbumCover_resetAlbumCover_thumbnail | null
 }
 
 export interface resetAlbumCover {
diff --git a/ui/src/components/sidebar/__generated__/setAlbumCover.ts b/ui/src/components/sidebar/__generated__/setAlbumCover.ts
index beba8fa..b37d258 100644
--- a/ui/src/components/sidebar/__generated__/setAlbumCover.ts
+++ b/ui/src/components/sidebar/__generated__/setAlbumCover.ts
@@ -7,9 +7,30 @@
 // GraphQL mutation operation: setAlbumCover
 // ====================================================
 
+export interface setAlbumCover_setAlbumCover_thumbnail_thumbnail {
+  __typename: 'MediaURL'
+  /**
+   * URL for previewing the image
+   */
+  url: string
+}
+
+export interface setAlbumCover_setAlbumCover_thumbnail {
+  __typename: 'Media'
+  id: string
+  /**
+   * URL to display the media in a smaller resolution
+   */
+  thumbnail: setAlbumCover_setAlbumCover_thumbnail_thumbnail | null
+}
+
 export interface setAlbumCover_setAlbumCover {
   __typename: 'Album'
-  coverID: number
+  id: string
+  /**
+   * An image in this album used for previewing this album
+   */
+  thumbnail: setAlbumCover_setAlbumCover_thumbnail | null
 }
 
 export interface setAlbumCover {
