Skip to content

Commit 6d2f69b

Browse files
committed
Move utility functions out of apollo-env, leaving polyfills.
The `apollo-env` package has a number of purposes today, including providing polyfills for runtimes that lack more modern features, providing utility functions (like, `createHash` that works on non-Node.js environments) and offering some common predicate functions like `isNotNullOrUndefined` - a method that we use in many places in the Apollo ecosystem. The polyfills have proven useful on environments that necessitate them, offering newer ECMAScript functionality in runtimes that don't yet have them, but they're automatically loaded from the main module entry point of this library, rather than being selectively enabled. For example, it's not uncommon to see `import 'apollo-env';` in other packages that rely on the functionality. It's this automatic eager loading of polyfills that proves to be problematic when the _utilities_ and _predicate_ functions of this library come into play though. Imagine a package that merely needs to use a version of `createHash` that simply falls back gracefully to a JavaScript version, or a function that wants to utilize the useful `mapValues` function, but in a circumstance where we - or our users - don't want to be subjected to the nature of polyfills affecting the runtime (see related issues like apollographql/apollo-server#2634). Another circumstance that arises, which was the most motivating factor in this case, is that the `apollo-env` package has consistently proven difficult for bundlers to traverse, possibly because of the polyfills, possibly because of the side-effects, possibly because of both! However, in my times trying to bundle our Apollo packages over the years, I've encountered problems with Webpack, Snowpack, Rollup.js and now esbuild. Most of these functions are small enough and simple enough that the duplication cost isn't all that high. For example, in my changes in this commit, rather than paying the cost of relying on an other package for what is basically a one-line function, I've just copied and pasted `isNotNullOrUndefined` into a couple places where it is needed. Furthermore, since most of the utilities that were being leveraged from `apollo-env` were being leveraged _by_ the `apollo-graphql` package, I've just moved those into that package instead. Any runtimes that still necessitate polyfills like `Array.prototype.flat` or `Array.prototype.flatMap` can still `import "apollo-env";` directly for those side-effects to be applied to their runtme, and packages that just need the utilities can rely on `apollo-graphql`. Again though, most of our packages are depending on both already so creating the more distinct separation seems not-all-that-unreasonable. Furthermore, while `apollo-env` does also provide some `fetch` polyfills, once Node.js 12 becomes the minimum version we support, I don't think we even need those polyfills anymore at all.
1 parent be117f3 commit 6d2f69b

12 files changed

Lines changed: 18 additions & 17 deletions

File tree

packages/apollo-env/src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,3 @@ import "./polyfills";
22

33
export * from "./typescript-utility-types";
44
export * from "./fetch";
5-
export * from "./utils";

packages/apollo-env/src/utils/index.ts

Lines changed: 0 additions & 4 deletions
This file was deleted.

packages/apollo-env/src/utils/predicates.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

packages/apollo-graphql/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
"node": ">=6"
1212
},
1313
"dependencies": {
14-
"apollo-env": "file:../apollo-env",
1514
"lodash.sortby": "^4.7.0"
1615
},
1716
"peerDependencies": {

packages/apollo-graphql/src/operationId.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@
5353
// defaultOperationRegistrySignature, which are slightly different normalization
5454
// functions used in other contextes.
5555
import { DocumentNode } from "graphql";
56-
import { createHash } from "apollo-env";
5756
import {
5857
printWithReducedWhitespace,
5958
dropUnusedDefinitions,
@@ -62,6 +61,7 @@ import {
6261
removeAliases,
6362
hideLiterals
6463
} from "./transforms";
64+
import { createHash } from "./utilities/createHash";
6565

6666
// The usage reporting signature function consists of removing extra whitespace,
6767
// sorting the AST in a deterministic manner, hiding literals, and removing

packages/apollo-graphql/src/schema/buildSchemaFromSDL.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import {
3030
UniqueDirectivesPerLocationRule,
3131
ValidationRule
3232
} from "graphql/validation";
33-
import { mapValues, isNotNullOrUndefined } from "apollo-env";
33+
import { mapValues } from "../utilities/mapValues";
3434

3535
export interface GraphQLSchemaModule {
3636
typeDefs: DocumentNode;
@@ -295,3 +295,9 @@ export function addResolversToSchema(
295295
}
296296
}
297297
}
298+
299+
function isNotNullOrUndefined<T>(
300+
value: T | null | undefined
301+
): value is T {
302+
return value !== null && typeof value !== "undefined";
303+
}

packages/apollo-graphql/src/schema/transformSchema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {
2121
GraphQLInputObjectType,
2222
GraphQLInputFieldConfigMap
2323
} from "graphql";
24-
import { mapValues } from "apollo-env";
24+
import { mapValues } from "../utilities/mapValues";
2525

2626
type TypeTransformer = (
2727
type: GraphQLNamedType
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)