Skip to content

Commit

Permalink
Enforce export type for all type-level exports. (#11115)
Browse files Browse the repository at this point in the history
* Enforce `export type` for all type-level exports.

* Apply suggestions from code review

Co-authored-by: Jerel Miller <jerelmiller@gmail.com>

* format some more files

---------

Co-authored-by: Jerel Miller <jerelmiller@gmail.com>
  • Loading branch information
phryneas and jerelmiller authored Aug 1, 2023
1 parent a803061 commit 78739e3
Show file tree
Hide file tree
Showing 19 changed files with 149 additions and 162 deletions.
5 changes: 5 additions & 0 deletions .changeset/cold-comics-sleep.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@apollo/client': patch
---

Enforce `export type` for all type-level exports.
6 changes: 5 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
"overrides": [
{
"files": ["**/*.ts", "**/*.tsx"],
"excludedFiles": ["**/__tests__/**/*.*"],
"excludedFiles": ["**/__tests__/**/*.*", "*.d.ts"],
"parserOptions": {
"project": "./tsconfig.json"
},
"rules": {
"@typescript-eslint/consistent-type-imports": [
"error",
Expand All @@ -32,6 +35,7 @@
"fixStyle": "separate-type-imports"
}
],
"@typescript-eslint/consistent-type-exports": ["error"],
"@typescript-eslint/no-import-type-side-effects": "error",
"no-restricted-syntax": [
"error",
Expand Down
3 changes: 0 additions & 3 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ node_modules/
/src/core/__tests__/QueryManager/multiple-results.ts
/src/core/__tests__/QueryManager/recycler.ts
/src/core/__tests__/fetchPolicies.ts
/src/core/index.ts
/src/core/networkStatus.ts
/src/core/types.ts
/src/core/watchQueryOptions.ts
Expand Down Expand Up @@ -168,7 +167,6 @@ node_modules/
/src/link/utils/validateOperation.ts
/src/link/ws/__tests__/webSocketLink.ts
/src/link/ws/index.ts
/src/testing/core/index.ts
/src/testing/core/itAsync.ts
/src/testing/core/mocking/mockClient.ts
/src/testing/core/mocking/mockFetch.ts
Expand Down Expand Up @@ -217,7 +215,6 @@ node_modules/
/src/utilities/graphql/getFromAST.ts
/src/utilities/graphql/storeUtils.ts
/src/utilities/graphql/transform.ts
/src/utilities/index.ts
/src/utilities/observables/Concast.ts
/src/utilities/observables/Observable.ts
/src/utilities/observables/__tests__/Concast.ts
Expand Down
33 changes: 13 additions & 20 deletions src/cache/index.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
import '../utilities/globals/index.js';

export { Transaction, ApolloCache } from './core/cache.js';
export type { Transaction } from './core/cache.js';
export { ApolloCache } from './core/cache.js';
export { Cache } from './core/types/Cache.js';
export { DataProxy } from './core/types/DataProxy.js';
export {
export type { DataProxy } from './core/types/DataProxy.js';
export type {
MissingTree,
Modifier,
Modifiers,
ModifierDetails,
MissingFieldError,
ReadFieldOptions
} from './core/types/common.js';
export { MissingFieldError } from './core/types/common.js';

export {
Reference,
isReference,
makeReference,
} from '../utilities/index.js';
export type { Reference } from '../utilities/index.js';
export { isReference, makeReference } from '../utilities/index.js';

export { EntityStore } from './inmemory/entityStore.js';
export {
Expand All @@ -28,30 +26,25 @@ export {
InMemoryCache,
} from './inmemory/inMemoryCache.js';

export {
ReactiveVar,
makeVar,
cacheSlot,
} from './inmemory/reactiveVars.js';
export type { ReactiveVar } from './inmemory/reactiveVars.js';
export { makeVar, cacheSlot } from './inmemory/reactiveVars.js';

export {
export type {
TypePolicies,
TypePolicy,
FieldPolicy,
FieldReadFunction,
FieldMergeFunction,
FieldFunctionOptions,
PossibleTypesMap,
Policies,
} from './inmemory/policies.js';
export { Policies } from './inmemory/policies.js';

export {
canonicalStringify,
} from './inmemory/object-canon.js';

export {
FragmentRegistryAPI,
createFragmentRegistry,
} from './inmemory/fragmentRegistry.js';
export type { FragmentRegistryAPI } from './inmemory/fragmentRegistry.js';
export { createFragmentRegistry } from './inmemory/fragmentRegistry.js';

export * from './inmemory/types.js';
4 changes: 2 additions & 2 deletions src/cache/inmemory/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { DocumentNode, FieldNode } from 'graphql';

import type { Transaction } from '../core/cache.js';
import {
import type {
StoreObject,
StoreValue,
Reference,
Expand All @@ -23,7 +23,7 @@ import type {

import type { FragmentRegistryAPI } from './fragmentRegistry.js';

export { StoreObject, StoreValue, Reference }
export type { StoreObject, StoreValue, Reference }

export interface IdGetterObj extends Object {
__typename?: string;
Expand Down
75 changes: 36 additions & 39 deletions src/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
/* Core */

export {
ApolloClient,
ApolloClientOptions,
DefaultOptions,
mergeOptions,
} from './ApolloClient.js';
export {
ObservableQuery,
export type { ApolloClientOptions, DefaultOptions } from "./ApolloClient.js";
export { ApolloClient, mergeOptions } from "./ApolloClient.js";
export type {
FetchMoreOptions,
UpdateQueryOptions,
} from './ObservableQuery.js';
export {
} from "./ObservableQuery.js";
export { ObservableQuery } from "./ObservableQuery.js";
export type {
QueryOptions,
WatchQueryOptions,
MutationOptions,
Expand All @@ -21,65 +17,66 @@ export {
ErrorPolicy,
FetchMoreQueryOptions,
SubscribeToMoreOptions,
} from './watchQueryOptions.js';
export { NetworkStatus, isNetworkRequestSettled } from './networkStatus.js';
export * from './types.js';
export {
Resolver,
FragmentMatcher,
} from './LocalState.js';
export { isApolloError, ApolloError } from '../errors/index.js';
} from "./watchQueryOptions.js";
export { NetworkStatus, isNetworkRequestSettled } from "./networkStatus.js";
export * from "./types.js";
export type { Resolver, FragmentMatcher } from "./LocalState.js";
export { isApolloError, ApolloError } from "../errors/index.js";
/* Cache */

export {
// All the exports (types and values) from ../cache, minus cacheSlot,
export type {
// All the exports (types) from ../cache, minus cacheSlot,
// which we want to keep semi-private.
Cache,
ApolloCache,
Transaction,
DataProxy,
InMemoryCache,
InMemoryCacheConfig,
MissingFieldError,
defaultDataIdFromObject,
ReactiveVar,
makeVar,
TypePolicies,
TypePolicy,
FieldPolicy,
FieldReadFunction,
FieldMergeFunction,
FieldFunctionOptions,
PossibleTypesMap,
} from '../cache/index.js';
} from "../cache/index.js";
export {
Cache,
ApolloCache,
InMemoryCache,
MissingFieldError,
defaultDataIdFromObject,
makeVar,
} from "../cache/index.js";

export * from '../cache/inmemory/types.js';
export * from "../cache/inmemory/types.js";

/* Link */

export * from '../link/core/index.js';
export * from '../link/http/index.js';
export * from "../link/core/index.js";
export * from "../link/http/index.js";
export type { ServerError } from "../link/utils/index.js";
export {
fromError,
toPromise,
fromPromise,
ServerError,
throwServerError,
} from '../link/utils/index.js';
} from "../link/utils/index.js";

/* Utilities */

export {
DocumentTransform,
export type {
DocumentTransformCacheKey,
Observable,
Observer,
ObservableSubscription,
Reference,
StoreObject,
} from "../utilities/index.js";
export {
DocumentTransform,
Observable,
isReference,
makeReference,
StoreObject,
} from '../utilities/index.js';
} from "../utilities/index.js";

/* Supporting */

Expand All @@ -88,7 +85,7 @@ export {
// "warn", "error", or "silent" to setVerbosity ("log" is the default).
// Note that all invariant.* logging is hidden in production.
import { setVerbosity } from "ts-invariant";
export { setVerbosity as setLogVerbosity }
export { setVerbosity as setLogVerbosity };
setVerbosity(__DEV__ ? "log" : "silent");

// Note that importing `gql` by itself, then destructuring
Expand All @@ -109,4 +106,4 @@ export {
disableFragmentWarnings,
enableExperimentalFragmentVariables,
disableExperimentalFragmentVariables,
} from 'graphql-tag';
} from "graphql-tag";
2 changes: 1 addition & 1 deletion src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type { QueryOptions } from './watchQueryOptions.js';
import type { Cache } from '../cache/index.js';
import type { IsStrictlyAny } from '../utilities/index.js';

export { TypedDocumentNode } from '@graphql-typed-document-node/core';
export type { TypedDocumentNode } from '@graphql-typed-document-node/core';

export type MethodKeys<T> = {
[P in keyof T]: T[P] extends Function ? P : never
Expand Down
3 changes: 2 additions & 1 deletion src/link/batch/batchLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { ApolloLink } from '../core/index.js';
import type { Observable } from '../../utilities/index.js';
import type { BatchHandler } from './batching.js';
import { OperationBatcher } from './batching.js';
export { OperationBatcher, BatchableRequest, BatchHandler } from './batching.js';
export type { BatchableRequest, BatchHandler } from './batching.js';
export { OperationBatcher } from './batching.js';


export namespace BatchLink {
Expand Down
4 changes: 2 additions & 2 deletions src/link/core/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { ExecutionResult, GraphQLError } from "graphql";
import { DocumentNode } from "graphql";
import type { DocumentNode } from "graphql";
import type { DefaultContext } from "../../core/index.js";
export { DocumentNode };
export type { DocumentNode };

import type { Observable } from "../../utilities/index.js";

Expand Down
15 changes: 5 additions & 10 deletions src/link/http/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
import '../../utilities/globals/index.js';

export type { ServerParseError } from './parseAndCheckHttpResponse.js';
export { parseAndCheckHttpResponse } from './parseAndCheckHttpResponse.js';
export type { ClientParseError } from './serializeFetchParameter.js';
export { serializeFetchParameter } from './serializeFetchParameter.js';
export type { HttpOptions, UriFunction } from './selectHttpOptionsAndBody.js';
export {
parseAndCheckHttpResponse,
ServerParseError
} from './parseAndCheckHttpResponse.js';
export {
serializeFetchParameter,
ClientParseError
} from './serializeFetchParameter.js';
export {
HttpOptions,
fallbackHttpConfig,
defaultPrinter,
selectHttpOptionsAndBody,
selectHttpOptionsAndBodyInternal, // needed by ../batch-http but not public
UriFunction
} from './selectHttpOptionsAndBody.js';
export { checkFetcher } from './checkFetcher.js';
export { createSignalIfSupported } from './createSignalIfSupported.js';
Expand Down
2 changes: 1 addition & 1 deletion src/link/remove-typename/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export type { RemoveTypenameFromVariablesOptions } from "./removeTypenameFromVariables.js";
export {
removeTypenameFromVariables,
KEEP,
RemoveTypenameFromVariablesOptions,
} from "./removeTypenameFromVariables.js";
3 changes: 2 additions & 1 deletion src/link/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import '../../utilities/globals/index.js';
export { fromError } from './fromError.js';
export { toPromise } from './toPromise.js';
export { fromPromise } from './fromPromise.js';
export { ServerError, throwServerError } from './throwServerError.js';
export type { ServerError } from './throwServerError.js';
export { throwServerError } from './throwServerError.js';
export { validateOperation } from './validateOperation.js';
export { createOperation } from './createOperation.js';
export { transformOperation } from './transformOperation.js';
Expand Down
13 changes: 6 additions & 7 deletions src/react/context/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import "../../utilities/globals/index.js";

export { ApolloConsumer, ApolloConsumerProps } from "./ApolloConsumer.js";
export {
ApolloContextValue,
getApolloContext,
resetApolloContext,
} from "./ApolloContext.js";
export { ApolloProvider, ApolloProviderProps } from "./ApolloProvider.js";
export type { ApolloConsumerProps } from "./ApolloConsumer.js";
export { ApolloConsumer } from "./ApolloConsumer.js";
export type { ApolloContextValue } from "./ApolloContext.js";
export { getApolloContext, resetApolloContext } from "./ApolloContext.js";
export type { ApolloProviderProps } from "./ApolloProvider.js";
export { ApolloProvider } from "./ApolloProvider.js";
10 changes: 4 additions & 6 deletions src/react/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@ export { useQuery } from "./useQuery.js";
export * from "./useSubscription.js";
export * from "./useReactiveVar.js";
export * from "./useFragment.js";
export {
useSuspenseQuery,
export type {
UseSuspenseQueryResult,
FetchMoreFunction,
RefetchFunction,
SubscribeToMoreFunction,
} from "./useSuspenseQuery.js";
export {
useBackgroundQuery,
UseBackgroundQueryResult,
} from "./useBackgroundQuery.js";
export { useSuspenseQuery } from "./useSuspenseQuery.js";
export type { UseBackgroundQueryResult } from "./useBackgroundQuery.js";
export { useBackgroundQuery } from "./useBackgroundQuery.js";
export { useReadQuery } from "./useReadQuery.js";
Loading

0 comments on commit 78739e3

Please sign in to comment.