Skip to content

Commit

Permalink
TypeScript: switch to importsNotUsedAsValues: error (#5668)
Browse files Browse the repository at this point in the history
This requires us to use `import type` if the symbols brought in by an
`import` aren't used at runtime.

Like the default value of remove, this gives the good performance of not
doing a runtime import for imports that are only used for typing. But by
enforcing the use of `import type`, you can easily tell with a glance if
a given import will be preserved for runtime or not.
  • Loading branch information
glasser authored Aug 27, 2021
1 parent 3baf16b commit 6954aff
Show file tree
Hide file tree
Showing 70 changed files with 136 additions and 116 deletions.
2 changes: 1 addition & 1 deletion packages/apollo-datasource-rest/src/HTTPCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
InMemoryLRUCache,
PrefixingKeyValueCache,
} from 'apollo-server-caching';
import { CacheOptions } from './RESTDataSource';
import type { CacheOptions } from './RESTDataSource';

export class HTTPCache {
private keyValueCache: KeyValueCache;
Expand Down
2 changes: 1 addition & 1 deletion packages/apollo-datasource-rest/src/RESTDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
fetch,
} from 'apollo-server-env';

import { ValueOrPromise } from 'apollo-server-types';
import type { ValueOrPromise } from 'apollo-server-types';

import { DataSource, DataSourceConfig } from 'apollo-datasource';

Expand Down
2 changes: 1 addition & 1 deletion packages/apollo-datasource/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { KeyValueCache } from 'apollo-server-caching';
import type { KeyValueCache } from 'apollo-server-caching';

export interface DataSourceConfig<TContext> {
context: TContext;
Expand Down
8 changes: 4 additions & 4 deletions packages/apollo-server-azure-functions/src/ApolloServer.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Context, HttpRequest } from '@azure/functions';
import { HttpResponse } from 'azure-functions-ts-essentials';
import type { Context, HttpRequest } from '@azure/functions';
import type { HttpResponse } from 'azure-functions-ts-essentials';
import { ApolloServerBase } from 'apollo-server-core';
import { GraphQLOptions } from 'apollo-server-core';
import type { GraphQLOptions } from 'apollo-server-core';
import { graphqlAzureFunction } from './azureFunctionApollo';
import { LandingPage } from 'apollo-server-plugin-base';
import type { LandingPage } from 'apollo-server-plugin-base';

export interface CreateHandlerOptions {
cors?: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import testSuite, {
schema as Schema,
CreateAppOptions,
} from 'apollo-server-integration-testsuite';
import { Config } from 'apollo-server-core';
import type { Config } from 'apollo-server-core';
import url from 'url';
import { IncomingMessage, ServerResponse } from 'http';
import type { IncomingMessage, ServerResponse } from 'http';
import typeis from 'type-is';

const createAzureFunction = async (options: CreateAppOptions = {}) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Context, HttpRequest, AzureFunction } from '@azure/functions';
import type { Context, HttpRequest, AzureFunction } from '@azure/functions';
import {
GraphQLOptions,
isHttpQueryError,
runHttpQuery,
} from 'apollo-server-core';
import { Headers } from 'apollo-server-env';
import { ValueOrPromise } from 'apollo-server-types';
import type { ValueOrPromise } from 'apollo-server-types';

export interface AzureFunctionGraphQLOptionsFunction {
(request: HttpRequest, context: Context): ValueOrPromise<GraphQLOptions>;
Expand Down
5 changes: 4 additions & 1 deletion packages/apollo-server-cache-memcached/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { KeyValueCache, KeyValueCacheSetOptions } from 'apollo-server-caching';
import type {
KeyValueCache,
KeyValueCacheSetOptions,
} from 'apollo-server-caching';
import Memcached from 'memcached';
import { promisify } from 'util';

Expand Down
5 changes: 4 additions & 1 deletion packages/apollo-server-cache-redis/src/BaseRedisCache.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { KeyValueCache, KeyValueCacheSetOptions } from 'apollo-server-caching';
import type {
KeyValueCache,
KeyValueCacheSetOptions,
} from 'apollo-server-caching';
import DataLoader from 'dataloader';

interface BaseRedisClient {
Expand Down
2 changes: 1 addition & 1 deletion packages/apollo-server-caching/src/InMemoryLRUCache.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import LRUCache from 'lru-cache';
import { KeyValueCache } from './KeyValueCache';
import type { KeyValueCache } from './KeyValueCache';

function defaultLengthCalculation(item: any) {
if (Array.isArray(item) || typeof item === 'string') {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { KeyValueCache, KeyValueCacheSetOptions } from './KeyValueCache';
import type { KeyValueCache, KeyValueCacheSetOptions } from './KeyValueCache';

// PrefixingKeyValueCache wraps another cache and adds a prefix to all keys used
// by all operations. This allows multiple features to share the same
Expand Down
2 changes: 1 addition & 1 deletion packages/apollo-server-caching/src/testsuite.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { KeyValueCache } from './KeyValueCache';
import type { KeyValueCache } from './KeyValueCache';

/**
* runKeyValueCacheTests is a function you can call from a test that exercises
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import testSuite, {
schema as Schema,
CreateAppOptions,
} from 'apollo-server-integration-testsuite';
import { Config } from 'apollo-server-core';
import type { Config } from 'apollo-server-core';
import express = require('express');
import bodyParser = require('body-parser');
import request from 'supertest';
Expand Down
4 changes: 2 additions & 2 deletions packages/apollo-server-cloudflare/src/ApolloServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { graphqlCloudflare } from './cloudflareApollo';

import { ApolloServerBase } from 'apollo-server-core';
export { GraphQLOptions } from 'apollo-server-core';
import { GraphQLOptions } from 'apollo-server-core';
import { Request } from 'apollo-server-env';
import type { GraphQLOptions } from 'apollo-server-core';
import type { Request } from 'apollo-server-env';

export class ApolloServer extends ApolloServerBase {
// This translates the arguments from the middleware into graphQL options It
Expand Down
2 changes: 1 addition & 1 deletion packages/apollo-server-cloudflare/src/cloudflareApollo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
} from 'apollo-server-core';

import { Request, Response, URL } from 'apollo-server-env';
import { ValueOrPromise } from 'apollo-server-types';
import type { ValueOrPromise } from 'apollo-server-types';

// Design principles:
// - You can issue a GET or POST with your query.
Expand Down
11 changes: 8 additions & 3 deletions packages/apollo-server-core/src/ApolloServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,14 @@ import type {
LandingPage,
} from 'apollo-server-plugin-base';

import { GraphQLServerOptions } from './graphqlOptions';
import type { GraphQLServerOptions } from './graphqlOptions';

import { Config, Context, ContextFunction, PluginDefinition } from './types';
import type {
Config,
Context,
ContextFunction,
PluginDefinition,
} from './types';

import { generateSchemaHash } from './utils/schemaHash';
import {
Expand All @@ -36,7 +41,7 @@ import {

import { Headers } from 'apollo-server-env';
import { buildServiceDefinition } from '@apollographql/apollo-tools';
import { Logger, SchemaHash, ApolloConfig } from 'apollo-server-types';
import type { Logger, SchemaHash, ApolloConfig } from 'apollo-server-types';
import { cloneObject } from './runHttpQuery';
import isNodeLike from './utils/isNodeLike';
import { determineApolloConfig } from './determineApolloConfig';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ApolloServerBase } from '../ApolloServer';
import { buildServiceDefinition } from '@apollographql/apollo-tools';
import { gql } from '../';
import { ApolloServerPlugin } from 'apollo-server-plugin-base';
import type { ApolloServerPlugin } from 'apollo-server-plugin-base';
import type { GraphQLSchema } from 'graphql';
import { Logger } from 'apollo-server-types';
import type { Logger } from 'apollo-server-types';

const typeDefs = gql`
type Query {
Expand Down
2 changes: 1 addition & 1 deletion packages/apollo-server-core/src/__tests__/logger.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ApolloServerBase } from '../..';
import { Logger } from 'apollo-server-types';
import type { Logger } from 'apollo-server-types';
import { PassThrough } from 'stream';
import gql from 'graphql-tag';

Expand Down
11 changes: 7 additions & 4 deletions packages/apollo-server-core/src/__tests__/runQuery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ import {
DocumentNode,
} from 'graphql';

import { GraphQLResponse } from 'apollo-server-types';
import type { GraphQLResponse } from 'apollo-server-types';

import { processGraphQLRequest, GraphQLRequest } from '../requestPipeline';
import { Request } from 'apollo-server-env';
import { GraphQLOptions, Context as GraphQLContext } from 'apollo-server-core';
import {
import type { Request } from 'apollo-server-env';
import type {
GraphQLOptions,
Context as GraphQLContext,
} from 'apollo-server-core';
import type {
ApolloServerPlugin,
GraphQLRequestExecutionListener,
GraphQLRequestListener,
Expand Down
2 changes: 1 addition & 1 deletion packages/apollo-server-core/src/determineApolloConfig.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ApolloConfig, ApolloConfigInput } from 'apollo-server-types';
import type { ApolloConfig, ApolloConfigInput } from 'apollo-server-types';
import createSHA from './utils/createSHA';

// This function combines the `apollo` constructor argument and some environment
Expand Down
10 changes: 5 additions & 5 deletions packages/apollo-server-core/src/graphqlOptions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {
import type {
GraphQLSchema,
ValidationContext,
GraphQLFieldResolver,
Expand All @@ -7,10 +7,10 @@ import {
GraphQLFormattedError,
ParseOptions,
} from 'graphql';
import { KeyValueCache, InMemoryLRUCache } from 'apollo-server-caching';
import { DataSource } from 'apollo-datasource';
import { ApolloServerPlugin } from 'apollo-server-plugin-base';
import {
import type { KeyValueCache, InMemoryLRUCache } from 'apollo-server-caching';
import type { DataSource } from 'apollo-datasource';
import type { ApolloServerPlugin } from 'apollo-server-plugin-base';
import type {
GraphQLExecutor,
ValueOrPromise,
GraphQLResponse,
Expand Down
2 changes: 1 addition & 1 deletion packages/apollo-server-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export {

// This currently provides the ability to have syntax highlighting as well as
// consistency between client and server gql tags
import { DocumentNode } from 'graphql';
import type { DocumentNode } from 'graphql';
import gqlTag from 'graphql-tag';
export const gql: (
template: TemplateStringsArray | string,
Expand Down
2 changes: 1 addition & 1 deletion packages/apollo-server-core/src/nodeHttpToRequest.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IncomingMessage } from 'http';
import type { IncomingMessage } from 'http';
import { Request, Headers } from 'apollo-server-env';

export function convertNodeHttpToRequest(req: IncomingMessage): Request {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
ApolloServerPluginCacheControl,
ApolloServerPluginCacheControlOptions,
} from '../';
import {
import type {
GraphQLRequestContextWillSendResponse,
GraphQLResponse,
} from 'apollo-server-plugin-base';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GraphQLSchema, graphql } from 'graphql';
import { CacheHint } from 'apollo-server-types';
import type { CacheHint } from 'apollo-server-types';
import {
ApolloServerPluginCacheControl,
ApolloServerPluginCacheControlOptions,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {
import type {
GraphQLScalarType,
GraphQLFieldResolver,
GraphQLTypeResolver,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { CacheAnnotation, CacheHint } from 'apollo-server-types';
import { CacheScope } from 'apollo-server-types';
import type { CacheScope } from 'apollo-server-types';
import {
DirectiveNode,
getNamedType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// specifying `version` when installing the plugin.

import { renderPlaygroundPage } from '@apollographql/graphql-playground-html';
import {
import type {
ApolloServerPlugin,
GraphQLServerListener,
} from 'apollo-server-plugin-base';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import nock from 'nock';
import { schemaReportGql, SchemaReporter } from '../schemaReporter';
import {
import type {
SchemaReportMutation,
ReportSchemaResponse,
SchemaReportMutationVariables,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { fetch } from 'apollo-server-env';
import { SchemaReporter } from './schemaReporter';
import createSHA from '../../utils/createSHA';
import { schemaIsFederated } from '../schemaIsFederated';
import { SchemaReport } from './operations';
import type { SchemaReport } from './operations';

export interface ApolloServerPluginSchemaReportingOptions {
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { gql } from '../..';
import { fetch, Headers, Request } from 'apollo-server-env';
import { GraphQLRequest, Logger } from 'apollo-server-types';
import type { GraphQLRequest, Logger } from 'apollo-server-types';
import { print } from 'graphql';
import {
import type {
SchemaReport,
SchemaReportMutationVariables,
SchemaReportMutation,
Expand Down
2 changes: 1 addition & 1 deletion packages/apollo-server-core/src/plugin/traceTreeBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// ApolloServerPluginInlineTrace.
import { GraphQLError, GraphQLResolveInfo, ResponsePath } from 'graphql';
import { Trace, google } from 'apollo-reporting-protobuf';
import { Logger } from 'apollo-server-types';
import type { Logger } from 'apollo-server-types';

function internalError(message: string) {
return new Error(`[internal apollo-server error] ${message}`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { Trace, Report, ITrace } from 'apollo-reporting-protobuf';
import pluginTestHarness from '../../../utils/pluginTestHarness';
import nock from 'nock';
import { gunzipSync } from 'zlib';
import { ApolloServerPluginUsageReportingOptions } from '../options';
import type { ApolloServerPluginUsageReportingOptions } from '../options';

const quietLogger = loglevel.getLogger('quiet');
quietLogger.setLevel(loglevel.levels.WARN);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Trace } from 'apollo-reporting-protobuf';
import type { Trace } from 'apollo-reporting-protobuf';

/**
* Iterates over the entire trace, calling `f` on each Trace.Node found. It
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GraphQLError, DocumentNode } from 'graphql';
import {
import type { GraphQLError, DocumentNode } from 'graphql';
import type {
GraphQLRequestContextDidResolveOperation,
Logger,
GraphQLRequestContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import retry from 'async-retry';
import { defaultUsageReportingSignature } from 'apollo-graphql';
import { Report, ReportHeader, Trace } from 'apollo-reporting-protobuf';
import { Response, fetch, Headers } from 'apollo-server-env';
import {
import type {
GraphQLRequestListener,
GraphQLServerListener,
} from 'apollo-server-plugin-base';
Expand All @@ -16,7 +16,7 @@ import {
GraphQLRequestContextWillSendResponse,
} from 'apollo-server-types';
import { createSignatureCache, signatureCacheKey } from './signatureCache';
import {
import type {
ApolloServerPluginUsageReportingOptions,
SendValuesBaseOptions,
} from './options';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import LRUCache from 'lru-cache';
import { Logger } from 'apollo-server-types';
import type { Logger } from 'apollo-server-types';

export function createSignatureCache({
logger,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Trace } from 'apollo-reporting-protobuf';
import { VariableValueOptions } from './options';
import type { VariableValueOptions } from './options';

// Creates trace details from request variables, given a specification for modifying
// values of private or sensitive variables.
Expand Down
8 changes: 4 additions & 4 deletions packages/apollo-server-core/src/requestPipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import {
Kind,
ParseOptions,
} from 'graphql';
import { DataSource } from 'apollo-datasource';
import { PersistedQueryOptions } from './graphqlOptions';
import type { DataSource } from 'apollo-datasource';
import type { PersistedQueryOptions } from './graphqlOptions';
import {
symbolExecutionDispatcherWillResolveField,
enablePluginsForSchemaResolvers,
Expand All @@ -30,15 +30,15 @@ import {
formatApolloErrors,
UserInputError,
} from 'apollo-server-errors';
import {
import type {
GraphQLRequest,
GraphQLResponse,
GraphQLRequestContext,
GraphQLExecutor,
GraphQLExecutionResult,
ValidationRule,
} from 'apollo-server-types';
import {
import type {
ApolloServerPlugin,
GraphQLRequestListener,
GraphQLRequestContextDidResolveSource,
Expand Down
Loading

0 comments on commit 6954aff

Please sign in to comment.