Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ import {setNextDeprecationDate} from '../../../private/node/context/deprecations
import {test, vi, expect, describe, beforeEach, beforeAll} from 'vitest'
import {TypedDocumentNode} from '@graphql-typed-document-node/core'

vi.mock('./graphql.js')
vi.mock('./graphql.js', async (importOriginal) => {
const actual = await importOriginal<typeof import('./graphql.js')>()
return {
...actual,
graphqlRequestDoc: vi.fn(),
}
})
vi.mock('../../../private/node/context/deprecations-store.js')
vi.mock('../context/fqdn.js')

Expand Down
30 changes: 2 additions & 28 deletions packages/cli-kit/src/public/node/api/app-management.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {addCursorAndFiltersToAppLogsUrl} from './utilities.js'
import {CacheOptions, GraphQLResponse, UnauthorizedHandler, graphqlRequestDoc} from './graphql.js'
import {CacheOptions, UnauthorizedHandler, graphqlRequestDoc, handleDeprecations} from './graphql.js'
import {appManagementFqdn} from '../context/fqdn.js'
import {setNextDeprecationDate} from '../../../private/node/context/deprecations-store.js'
import {buildHeaders} from '../../../private/node/api/headers.js'
import {RequestModeInput} from '../http.js'
import Bottleneck from 'bottleneck'
Expand Down Expand Up @@ -94,29 +93,4 @@ export async function appManagementRequestDoc<TResult, TVariables extends Variab
return result
}

interface Deprecation {
supportedUntilDate?: string
}

interface WithDeprecations {
deprecations: Deprecation[]
}

/**
* Sets the next deprecation date from [GraphQL response extensions](https://www.apollographql.com/docs/resources/graphql-glossary/#extensions)
* if `response.extensions.deprecations` objects contain a `supportedUntilDate` (ISO 8601-formatted string).
*
* @param response - The response of the query.
*/
export function handleDeprecations<T>(response: GraphQLResponse<T>): void {
if (!response.extensions) return

const deprecationDates: Date[] = []
for (const deprecation of (response.extensions as WithDeprecations).deprecations) {
if (deprecation.supportedUntilDate) {
deprecationDates.push(new Date(deprecation.supportedUntilDate))
}
}

setNextDeprecationDate(deprecationDates)
}
export {handleDeprecations}
10 changes: 8 additions & 2 deletions packages/cli-kit/src/public/node/api/business-platform.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import {CacheOptions, GraphQLVariables, UnauthorizedHandler, graphqlRequest, graphqlRequestDoc} from './graphql.js'
import {handleDeprecations} from './partners.js'
import {
CacheOptions,
GraphQLVariables,
UnauthorizedHandler,
graphqlRequest,
graphqlRequestDoc,
handleDeprecations,
} from './graphql.js'
import {businessPlatformFqdn} from '../context/fqdn.js'
import {TypedDocumentNode} from '@graphql-typed-document-node/core'
import {Variables} from 'graphql-request'
Expand Down
3 changes: 1 addition & 2 deletions packages/cli-kit/src/public/node/api/functions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {handleDeprecations} from './app-management.js'
import {graphqlRequestDoc, UnauthorizedHandler} from './graphql.js'
import {graphqlRequestDoc, handleDeprecations, UnauthorizedHandler} from './graphql.js'
import {appManagementFqdn} from '../context/fqdn.js'
import {TypedDocumentNode} from '@graphql-typed-document-node/core'
import {Variables} from 'graphql-request'
Expand Down
31 changes: 30 additions & 1 deletion packages/cli-kit/src/public/node/api/graphql.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {graphqlRequest, graphqlRequestDoc} from './graphql.js'
import {graphqlRequest, graphqlRequestDoc, handleDeprecations, GraphQLResponse} from './graphql.js'
import {setNextDeprecationDate} from '../../../private/node/context/deprecations-store.js'
import * as api from '../../../private/node/api.js'
import * as debugRequest from '../../../private/node/api/graphql.js'
import {requestIdsCollection} from '../../../private/node/request-ids.js'
Expand All @@ -16,6 +17,8 @@ import {TypedDocumentNode} from '@graphql-typed-document-node/core'
import {setupServer} from 'msw/node'
import {graphql, HttpResponse} from 'msw'

vi.mock('../../../private/node/context/deprecations-store.js')

let mockedRequestId = 'request-id-123'

vi.spyOn(debugRequest, 'debugLogRequestInfo').mockResolvedValue(undefined)
Expand Down Expand Up @@ -688,3 +691,29 @@ describe('graphqlRequest with caching', () => {
expect(retryAwareSpy).toHaveBeenCalled()
})
})

describe('handleDeprecations', () => {
test('does not call setNextDeprecationDate if response contains no deprecations', () => {
// Given
const response = {data: {}} as GraphQLResponse<object>

// When
handleDeprecations(response)

// Then
expect(setNextDeprecationDate).not.toBeCalled()
})

test('calls setNextDeprecationDate with response extensions deprecation dates', () => {
// Given
const deprecationDates = [new Date()]
const deprecations = deprecationDates.map((supportedUntilDate) => ({supportedUntilDate}))
const response = {data: {}, extensions: {deprecations}} as GraphQLResponse<object>

// When
handleDeprecations(response)

// Then
expect(setNextDeprecationDate).toHaveBeenLastCalledWith(deprecationDates)
})
})
28 changes: 28 additions & 0 deletions packages/cli-kit/src/public/node/api/graphql.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {setNextDeprecationDate} from '../../../private/node/context/deprecations-store.js'
import {buildHeaders, httpsAgent} from '../../../private/node/api/headers.js'
import {debugLogRequestInfo, errorHandler} from '../../../private/node/api/graphql.js'
import {addPublicMetadata, runWithTimer} from '../metadata.js'
Expand Down Expand Up @@ -309,3 +310,30 @@ export async function graphqlRequestDoc<TResult, TVariables extends Variables>(
queryAsString: resolveRequestDocument(options.query).query,
})
}

interface Deprecation {
supportedUntilDate?: string
}

interface WithDeprecations {
deprecations: Deprecation[]
}

/**
* Sets the next deprecation date from [GraphQL response extensions](https://www.apollographql.com/docs/resources/graphql-glossary/#extensions)
* if `response.extensions.deprecations` objects contain a `supportedUntilDate` (ISO 8601-formatted string).
*
* @param response - The response of the query.
*/
export function handleDeprecations<T>(response: GraphQLResponse<T>): void {
if (!response.extensions) return

const deprecationDates: Date[] = []
for (const deprecation of (response.extensions as WithDeprecations).deprecations) {
if (deprecation.supportedUntilDate) {
deprecationDates.push(new Date(deprecation.supportedUntilDate))
}
}

setNextDeprecationDate(deprecationDates)
}
8 changes: 7 additions & 1 deletion packages/cli-kit/src/public/node/api/partners.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ import {setNextDeprecationDate} from '../../../private/node/context/deprecations

import {test, vi, expect, describe, beforeEach, beforeAll} from 'vitest'

vi.mock('./graphql.js')
vi.mock('./graphql.js', async (importOriginal) => {
const actual = await importOriginal<typeof import('./graphql.js')>()
return {
...actual,
graphqlRequest: vi.fn(),
}
})
vi.mock('../../../private/node/context/deprecations-store.js')
vi.mock('../context/fqdn.js')

Expand Down
30 changes: 2 additions & 28 deletions packages/cli-kit/src/public/node/api/partners.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {graphqlRequest, GraphQLVariables, GraphQLResponse, CacheOptions, UnauthorizedHandler} from './graphql.js'
import {graphqlRequest, GraphQLVariables, CacheOptions, UnauthorizedHandler, handleDeprecations} from './graphql.js'
import {partnersFqdn} from '../context/fqdn.js'
import {setNextDeprecationDate} from '../../../private/node/context/deprecations-store.js'
import {RequestModeInput} from '../http.js'
import Bottleneck from 'bottleneck'

Expand Down Expand Up @@ -63,29 +62,4 @@ export async function partnersRequest<T>(
return result
}

interface Deprecation {
supportedUntilDate?: string
}

interface WithDeprecations {
deprecations: Deprecation[]
}

/**
* Sets the next deprecation date from [GraphQL response extensions](https://www.apollographql.com/docs/resources/graphql-glossary/#extensions)
* if `response.extensions.deprecations` objects contain a `supportedUntilDate` (ISO 8601-formatted string).
*
* @param response - The response of the query.
*/
export function handleDeprecations<T>(response: GraphQLResponse<T>): void {
if (!response.extensions) return

const deprecationDates: Date[] = []
for (const deprecation of (response.extensions as WithDeprecations).deprecations) {
if (deprecation.supportedUntilDate) {
deprecationDates.push(new Date(deprecation.supportedUntilDate))
}
}

setNextDeprecationDate(deprecationDates)
}
export {handleDeprecations}
Loading
Loading