|
| 1 | +import { CombinedError } from '@urql/core'; |
| 2 | +import gql from 'graphql-tag'; |
| 3 | + |
| 4 | +import { ExpoGraphqlClient } from '../../commandUtils/context/contextUtils/createGraphqlClient'; |
| 5 | +import { AppPlatform, EmbeddedUpdate } from '../generated'; |
| 6 | +import { Connection } from '../../utils/relay'; |
| 7 | +import { withErrorHandlingAsync } from '../client'; |
| 8 | + |
| 9 | +export function isEmbeddedUpdateNotFoundError(error: unknown): boolean { |
| 10 | + return ( |
| 11 | + error instanceof CombinedError && |
| 12 | + error.graphQLErrors.some(e => e.extensions?.['errorCode'] === 'EMBEDDED_UPDATE_NOT_FOUND') |
| 13 | + ); |
| 14 | +} |
| 15 | + |
| 16 | +// Query result types are defined manually because the embeddedUpdates query fields |
| 17 | +// are not yet included in the GraphQL codegen schema. |
| 18 | +export type EmbeddedUpdateFragment = Pick< |
| 19 | + EmbeddedUpdate, |
| 20 | + 'id' | 'platform' | 'runtimeVersion' | 'channel' | 'createdAt' |
| 21 | +>; |
| 22 | + |
| 23 | +type ViewEmbeddedUpdateByIdQueryResult = { |
| 24 | + embeddedUpdates: { |
| 25 | + byId: EmbeddedUpdateFragment; |
| 26 | + }; |
| 27 | +}; |
| 28 | + |
| 29 | +type ViewEmbeddedUpdateByIdQueryVariables = { |
| 30 | + embeddedUpdateId: string; |
| 31 | + appId: string; |
| 32 | +}; |
| 33 | + |
| 34 | +type ViewEmbeddedUpdatesPaginatedQueryResult = { |
| 35 | + app: { |
| 36 | + byId: { |
| 37 | + embeddedUpdatesPaginated: { |
| 38 | + edges: { cursor: string; node: EmbeddedUpdateFragment }[]; |
| 39 | + pageInfo: { |
| 40 | + hasNextPage: boolean; |
| 41 | + hasPreviousPage: boolean; |
| 42 | + startCursor: string | null; |
| 43 | + endCursor: string | null; |
| 44 | + }; |
| 45 | + }; |
| 46 | + }; |
| 47 | + }; |
| 48 | +}; |
| 49 | + |
| 50 | +type ViewEmbeddedUpdatesPaginatedQueryVariables = { |
| 51 | + appId: string; |
| 52 | + first: number; |
| 53 | + after?: string; |
| 54 | + filter?: EmbeddedUpdateFilter; |
| 55 | +}; |
| 56 | + |
| 57 | +export type EmbeddedUpdateFilter = { |
| 58 | + platform?: AppPlatform; |
| 59 | + runtimeVersion?: string; |
| 60 | + channel?: string; |
| 61 | +}; |
| 62 | + |
| 63 | +export const EmbeddedUpdateQuery = { |
| 64 | + async viewByIdAsync( |
| 65 | + graphqlClient: ExpoGraphqlClient, |
| 66 | + { embeddedUpdateId, appId }: { embeddedUpdateId: string; appId: string } |
| 67 | + ): Promise<EmbeddedUpdateFragment> { |
| 68 | + const data = await withErrorHandlingAsync( |
| 69 | + graphqlClient |
| 70 | + .query<ViewEmbeddedUpdateByIdQueryResult, ViewEmbeddedUpdateByIdQueryVariables>( |
| 71 | + gql` |
| 72 | + query ViewEmbeddedUpdateById($embeddedUpdateId: ID!, $appId: ID!) { |
| 73 | + embeddedUpdates { |
| 74 | + byId(embeddedUpdateId: $embeddedUpdateId, appId: $appId) { |
| 75 | + id |
| 76 | + platform |
| 77 | + runtimeVersion |
| 78 | + channel |
| 79 | + createdAt |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + `, |
| 84 | + { embeddedUpdateId, appId }, |
| 85 | + { additionalTypenames: ['EmbeddedUpdate'] } |
| 86 | + ) |
| 87 | + .toPromise() |
| 88 | + ); |
| 89 | + return data.embeddedUpdates.byId; |
| 90 | + }, |
| 91 | + |
| 92 | + async viewPaginatedAsync( |
| 93 | + graphqlClient: ExpoGraphqlClient, |
| 94 | + { |
| 95 | + appId, |
| 96 | + filter, |
| 97 | + first, |
| 98 | + after, |
| 99 | + }: { |
| 100 | + appId: string; |
| 101 | + filter?: EmbeddedUpdateFilter; |
| 102 | + first: number; |
| 103 | + after?: string; |
| 104 | + } |
| 105 | + ): Promise<Connection<EmbeddedUpdateFragment>> { |
| 106 | + const data = await withErrorHandlingAsync( |
| 107 | + graphqlClient |
| 108 | + .query<ViewEmbeddedUpdatesPaginatedQueryResult, ViewEmbeddedUpdatesPaginatedQueryVariables>( |
| 109 | + gql` |
| 110 | + query ViewEmbeddedUpdatesPaginated( |
| 111 | + $appId: String! |
| 112 | + $first: Int! |
| 113 | + $after: String |
| 114 | + $filter: EmbeddedUpdateFilterInput |
| 115 | + ) { |
| 116 | + app { |
| 117 | + byId(appId: $appId) { |
| 118 | + id |
| 119 | + embeddedUpdatesPaginated(first: $first, after: $after, filter: $filter) { |
| 120 | + edges { |
| 121 | + cursor |
| 122 | + node { |
| 123 | + id |
| 124 | + platform |
| 125 | + runtimeVersion |
| 126 | + channel |
| 127 | + createdAt |
| 128 | + } |
| 129 | + } |
| 130 | + pageInfo { |
| 131 | + hasNextPage |
| 132 | + hasPreviousPage |
| 133 | + startCursor |
| 134 | + endCursor |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + `, |
| 141 | + { appId, first, after, filter }, |
| 142 | + { additionalTypenames: ['EmbeddedUpdate'] } |
| 143 | + ) |
| 144 | + .toPromise() |
| 145 | + ); |
| 146 | + return data.app.byId.embeddedUpdatesPaginated; |
| 147 | + }, |
| 148 | +}; |
0 commit comments