Skip to content
This repository has been archived by the owner on Apr 14, 2023. It is now read-only.

Shrink apollo-link[-http-common] packages. #969

Merged
merged 4 commits into from
Mar 5, 2019
Merged
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
636 changes: 378 additions & 258 deletions package-lock.json

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
{
"name": "apollo-link",
"path": "./packages/apollo-link/lib/bundle.min.js",
"maxSize": "1.3 Kb"
"maxSize": "1.1 Kb"
},
{
"name": "apollo-link-batch",
Expand Down Expand Up @@ -104,12 +104,12 @@
"pre-push": "lint-check",
"dependencies": {},
"devDependencies": {
"@condenast/bundlesize": "^0.18.1",
"@condenast/bundlesize": "0.18.1",
"@types/zen-observable": "0.8.0",
"check-if-folder-contents-changed-in-git-commit-range": "1.0.0",
"codecov": "3.2.0",
"danger": "3.9.0",
"jest": "^24.0.0",
"jest": "24.1.0",
"jest-junit": "6.3.0",
"lerna": "3.13.1",
"lint-staged": "7.3.0",
Expand All @@ -118,10 +118,11 @@
"prettier": "1.15.2",
"prettier-check": "2.0.0",
"rollup": "1.2.3",
"rollup-plugin-invariant": "0.4.2",
"rollup-plugin-node-resolve": "4.0.1",
"rollup-plugin-sourcemaps": "0.4.2",
"rollup-plugin-typescript2": "0.19.3",
"terser": "^3.16.1",
"terser": "3.16.1",
"ts-jest": "22.4.6",
"tslib": "1.9.3",
"typescript": "3.0.3"
Expand Down
8 changes: 8 additions & 0 deletions packages/apollo-link-http-common/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/apollo-link-http-common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"license": "MIT",
"dependencies": {
"apollo-link": "file:../apollo-link",
"ts-invariant": "^0.3.2",
"tslib": "^1.9.3"
},
"peerDependencies": {
Expand Down
7 changes: 4 additions & 3 deletions packages/apollo-link-http-common/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Operation } from 'apollo-link';
import { print } from 'graphql/language/printer';
import { InvariantError } from 'ts-invariant';

/*
* Http Utilities: shared across links that make http requests
Expand All @@ -24,7 +25,7 @@ export type ServerParseError = Error & {
bodyText: string;
};

export type ClientParseError = Error & {
export type ClientParseError = InvariantError & {
parseError: Error;
};

Expand Down Expand Up @@ -174,7 +175,7 @@ export const checkFetcher = (fetcher: GlobalFetch['fetch']) => {
if (!fetcher && typeof fetch === 'undefined') {
let library: string = 'unfetch';
if (typeof window === 'undefined') library = 'node-fetch';
throw new Error(`
throw new InvariantError(`
fetch is not found globally and no fetcher passed, to fix pass a fetch for
your environment like https://www.npmjs.com/package/${library}.

Expand Down Expand Up @@ -248,7 +249,7 @@ export const serializeFetchParameter = (p, label) => {
try {
serialized = JSON.stringify(p);
} catch (e) {
const parseError = new Error(
const parseError = new InvariantError(
`Network request failed. ${label} is not serializable: ${e.message}`,
) as ClientParseError;
parseError.parseError = e;
Expand Down
18 changes: 18 additions & 0 deletions packages/apollo-link/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/apollo-link/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
"watch": "tsc -w -p . & rollup -c -w"
},
"dependencies": {
"apollo-utilities": "^1.2.1",
JoviDeCroock marked this conversation as resolved.
Show resolved Hide resolved
"ts-invariant": "^0.3.2",
"tslib": "^1.9.3",
"zen-observable-ts": "file:../zen-observable-ts"
},
Expand Down
5 changes: 3 additions & 2 deletions packages/apollo-link/src/link.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Observable from 'zen-observable-ts';
import { invariant, InvariantError } from 'ts-invariant';

import {
GraphQLRequest,
Expand Down Expand Up @@ -60,7 +61,7 @@ export const concat = (
) => {
const firstLink = toLink(first);
if (isTerminating(firstLink)) {
console.warn(
invariant.warn(
new LinkError(
`You are calling concat on a terminating link, which will have no effect`,
firstLink,
Expand Down Expand Up @@ -115,7 +116,7 @@ export class ApolloLink {
operation: Operation,
forward?: NextLink,
): Observable<FetchResult> | null {
throw new Error('request is not implemented');
throw new InvariantError('request is not implemented');
}
}

Expand Down
18 changes: 5 additions & 13 deletions packages/apollo-link/src/linkUtils.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
import Observable from 'zen-observable-ts';
import { print } from 'graphql/language/printer';
import { DocumentNode, OperationDefinitionNode } from 'graphql';

import { GraphQLRequest, Operation } from './types';
import { ApolloLink } from './link';

export function getOperationName(doc: DocumentNode): string | null {
return (
doc.definitions
.filter(
definition =>
definition.kind === 'OperationDefinition' && definition.name,
)
.map((x: OperationDefinitionNode) => x.name.value)[0] || null
);
}
import { getOperationName } from 'apollo-utilities';
import { invariant, InvariantError } from 'ts-invariant';
export { getOperationName };

export function validateOperation(operation: GraphQLRequest): GraphQLRequest {
const OPERATION_FIELDS = [
Expand All @@ -26,7 +18,7 @@ export function validateOperation(operation: GraphQLRequest): GraphQLRequest {
];
for (let key of Object.keys(operation)) {
if (OPERATION_FIELDS.indexOf(key) < 0) {
throw new Error(`illegal argument: ${key}`);
throw new InvariantError(`illegal argument: ${key}`);
}
}

Expand All @@ -51,7 +43,7 @@ export function toPromise<R>(observable: Observable<R>): Promise<R> {
observable.subscribe({
next: data => {
if (completed) {
console.warn(
invariant.warn(
`Promise Wrapper does not support multiple results from Observable`,
);
} else {
Expand Down
3 changes: 2 additions & 1 deletion packages/apollo-link/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Observable from 'zen-observable-ts';
import { ExecutionResult, DocumentNode } from 'graphql';
import { DocumentNode } from 'graphql/language/ast';
import { ExecutionResult } from 'graphql/execution/execute';
export { ExecutionResult, DocumentNode };

export interface GraphQLRequest {
Expand Down
12 changes: 11 additions & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,27 @@ import sourcemaps from 'rollup-plugin-sourcemaps';
import node from 'rollup-plugin-node-resolve';
import typescript from 'typescript';
import typescriptPlugin from 'rollup-plugin-typescript2';
import invariantPlugin from 'rollup-plugin-invariant';

export const globals = {
// Apollo
'apollo-client': 'apollo.core',
'apollo-link': 'apolloLink.core',
'apollo-link-batch': 'apolloLink.batch',
'apollo-link-http-common': 'apolloLink.httpCommon',
'apollo-utilities': 'apolloUtilities',
'zen-observable-ts': 'apolloLink.zenObservable',
'subscriptions-transport-ws': 'subscriptions-transport-ws',

//GraphQL
// GraphQL
'graphql/language/printer': 'graphql.printer',
'graphql/execution/execute': 'graphql.execute',

// TypeScript
'tslib': 'tslib',

// Other
'ts-invariant': 'invariant',
'zen-observable': 'Observable',
};

Expand Down Expand Up @@ -46,6 +50,9 @@ export default name => [
},
},
}),
invariantPlugin({
errorCodes: true,
}),
sourcemaps()
],
},
Expand All @@ -70,6 +77,9 @@ export default name => [
},
},
}),
invariantPlugin({
errorCodes: true,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See apollographql/apollo-client#4521 for more explanation of what this option does.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's actually a really nice solution for having some errors in production. That rollup plugin is really smart

}),
sourcemaps()
],
},
Expand Down