Skip to content

Commit

Permalink
Merge pull request #444 from convoyinc/jdeff/apollo-v3
Browse files Browse the repository at this point in the history
Update apollo client to v3 and TypeScript to latest
  • Loading branch information
Jayme Deffenbaugh authored Aug 6, 2020
2 parents 3d44bd1 + a6d82a8 commit 8679ef0
Show file tree
Hide file tree
Showing 23 changed files with 329 additions and 137 deletions.
22 changes: 14 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@
"@types/chai": "^4.0.10",
"@types/chai-as-promised": "^7.1.0",
"@types/deep-freeze-strict": "^1.1.0",
"@types/graphql": "^0.12.4",
"@types/lodash": "^4.14.102",
"@types/lodash.findindex": "^4.6.3",
"@types/lodash.isequal": "^4.3.3",
"@types/lodash.set": "^4.3.3",
"@types/node": "^8.9.4",
"@types/node": "^14.0.27",
"@types/react": "^16.9.44",
"audit-ci": "^2.4.0",
"chai": "^4.1.2",
"chai-as-promised": "^7.1.1",
Expand All @@ -53,23 +53,29 @@
"eslint-plugin-import": "^2.8.0",
"eslint-plugin-jest": "^22.0.0",
"eslint-plugin-typescript": "^0.12.0",
"graphql": "^0.13.1",
"graphql-tag": "^2.6.0",
"graphql": "^15.3.0",
"graphql-tag": "^2.11.0",
"jest": "^23.0.0",
"jest-junit": "^5.0.0",
"lodash": "^4.17.5",
"react": "^16.13.1",
"rimraf": "^2.6.2",
"typescript": "~2.8.0",
"typescript": "^3.9.7",
"typescript-eslint-parser": "^20.0.0"
},
"dependencies": {
"apollo-cache": "^1.0.0",
"apollo-utilities": "^1.0.0",
"@apollo/client": "^3.1.2",
"@wry/equality": "^0.2.0",
"deep-freeze-strict": "^1.0.0",
"lodash.findindex": "^4.0.0",
"lodash.isequal": "^4.5.0",
"lodash.set": "^4.0.0",
"make-error": "^1.3.0",
"tslib": "^1.9.0"
"subscriptions-transport-ws": "^0.9.17",
"ts-invariant": "^0.4.4",
"tslib": "^2.0.0"
},
"peerDependencies": {
"@apollo/client": "^3.1.2"
}
}
6 changes: 1 addition & 5 deletions src/Cache.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Cache as CacheInterface } from 'apollo-cache';
import { Cache as CacheInterface } from '@apollo/client';

import { CacheSnapshot } from './CacheSnapshot';
import { CacheTransaction } from './CacheTransaction';
Expand Down Expand Up @@ -59,10 +59,6 @@ export class Cache implements Queryable {
);
}

evict(_query: RawOperation): { success: boolean } { // eslint-disable-line class-methods-use-this
throw new Error(`evict() is not implemented on Cache`);
}

/**
* Reads the selection expressed by a query from the cache.
*
Expand Down
8 changes: 0 additions & 8 deletions src/CacheTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,6 @@ export class CacheTransaction implements Queryable {
this._snapshot = new CacheSnapshot(current.baseline, optimistic, optimisticQueue);
}

/**
* Removes values from the current transaction
*/
// eslint-disable-next-line class-methods-use-this
evict(_query: RawOperation): { success: boolean } {
throw new Error('evict() is not implemented on CacheTransaction');
}

/**
* Complete the transaction, returning the new snapshot and the ids of any
* nodes that were edited.
Expand Down
5 changes: 3 additions & 2 deletions src/ParsedQueryNode.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isEqual, valueFromNode } from 'apollo-utilities';
import isEqual from '@wry/equality';

import { CacheContext } from './context';
import { ConflictingFieldsError } from './errors';
Expand All @@ -11,6 +11,7 @@ import {
ValueNode,
isObject,
fieldHasStaticDirective,
valueFromNode,
} from './util';

export type JsonAndVariables = JsonScalar | VariableArgument;
Expand Down Expand Up @@ -187,7 +188,7 @@ export function areChildrenDynamic(children?: ParsedQueryWithVariables) {
/**
* Build the map of arguments to their natural JS values (or variables).
*/
function _buildFieldArgs(variables: Set<string>, argumentsNode?: ArgumentNode[]) {
function _buildFieldArgs(variables: Set<string>, argumentsNode?: readonly ArgumentNode[]) {
if (!argumentsNode) return undefined;

const args = {};
Expand Down
6 changes: 0 additions & 6 deletions src/Queryable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,4 @@ export interface Queryable {
* Writes values for a selection to the cache.
*/
write(query: RawOperation, payload: JsonObject): void;

/**
* Removes values for a selection to the cache
*/
evict(query: RawOperation): { success: boolean };

}
6 changes: 3 additions & 3 deletions src/apollo/Hermes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import {
Transaction,
Cache as CacheInterface,
ApolloCache,
} from 'apollo-cache';
} from '@apollo/client';

import { CacheContext } from '../context';
import { Cache, MigrationMap } from '../Cache';
import { CacheSnapshot } from '../CacheSnapshot';
import { CacheContext } from '../context';
import { GraphSnapshot } from '../GraphSnapshot';

import { ApolloQueryable } from './Queryable';
Expand All @@ -16,7 +16,7 @@ import { buildRawOperationFromQuery } from './util';
/**
* Apollo-specific interface to the cache.
*/
export class Hermes extends ApolloQueryable implements ApolloCache<GraphSnapshot> {
export class Hermes extends ApolloQueryable<GraphSnapshot> {
/** The underlying Hermes cache. */
protected _queryable: Cache;

Expand Down
31 changes: 16 additions & 15 deletions src/apollo/Queryable.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Cache, DataProxy } from 'apollo-cache';
import { removeDirectivesFromDocument } from 'apollo-utilities';
import { ApolloCache, Cache, DataProxy, Reference, makeReference } from '@apollo/client';
import { removeDirectivesFromDocument } from '@apollo/client/utilities';

import { UnsatisfiedCacheError } from '../errors';
import { JsonObject } from '../primitive';
Expand All @@ -11,7 +11,7 @@ import { buildRawOperationFromQuery, buildRawOperationFromFragment } from './uti
/**
* Apollo-specific interface to the cache.
*/
export abstract class ApolloQueryable implements DataProxy {
export abstract class ApolloQueryable<TSerialized> extends ApolloCache<TSerialized> {
/** The underlying Hermes cache. */
protected abstract _queryable: Queryable;

Expand Down Expand Up @@ -49,52 +49,53 @@ export abstract class ApolloQueryable implements DataProxy {
// TODO: Support nested fragments.
const rawOperation = buildRawOperationFromFragment(
options.fragment,
options.id,
options.id!,
options.variables as any,
options.fragmentName,
);
return this._queryable.read(rawOperation, optimistic).result as any;
}

write(options: Cache.WriteOptions): void {
write(options: Cache.WriteOptions): Reference | undefined {
const rawOperation = buildRawOperationFromQuery(options.query, options.variables as JsonObject, options.dataId);
this._queryable.write(rawOperation, options.result);

return makeReference(rawOperation.rootId);
}

writeQuery<TData = any, TVariables = any>(options: Cache.WriteQueryOptions<TData, TVariables>): void {
writeQuery<TData = any, TVariables = any>(options: Cache.WriteQueryOptions<TData, TVariables>): Reference | undefined {
const rawOperation = buildRawOperationFromQuery(options.query, options.variables as any);
this._queryable.write(rawOperation, options.data as any);

return makeReference(rawOperation.rootId);
}

writeFragment<TData = any, TVariables = any>(options: Cache.WriteFragmentOptions<TData, TVariables>): void {
writeFragment<TData = any, TVariables = any>(options: Cache.WriteFragmentOptions<TData, TVariables>): Reference | undefined {
// TODO: Support nested fragments.
const rawOperation = buildRawOperationFromFragment(
options.fragment,
options.id,
options.id!,
options.variables as any,
options.fragmentName,
);
this._queryable.write(rawOperation, options.data as any);
}

writeData(): void { // eslint-disable-line class-methods-use-this
throw new Error(`writeData is not implemented by Hermes yet`);
return makeReference(rawOperation.rootId);
}

transformDocument(doc: DocumentNode): DocumentNode {
return this._queryable.transformDocument(doc);
}

public transformForLink(document: DocumentNode): DocumentNode { // eslint-disable-line class-methods-use-this
transformForLink(document: DocumentNode): DocumentNode { // eslint-disable-line class-methods-use-this
// @static directives are for the cache only.
return removeDirectivesFromDocument(
[{ name: 'static' }],
document
)!;
}

evict(options: Cache.EvictOptions): Cache.EvictionResult {
const rawOperation = buildRawOperationFromQuery(options.query, options.variables);
return this._queryable.evict(rawOperation);
evict(_options: Cache.EvictOptions): boolean { // eslint-disable-line class-methods-use-this
throw new Error(`evict() is not implemented in Hermes`);
}
}
7 changes: 3 additions & 4 deletions src/apollo/Transaction.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { ApolloCache, Cache, Transaction } from 'apollo-cache';
import { JsonValue } from 'apollo-utilities';
import { Cache, Transaction } from '@apollo/client';
import lodashIsEqual = require('lodash.isequal');

import { CacheTransaction } from '../CacheTransaction';
import { GraphSnapshot } from '../GraphSnapshot';
import { PathPart } from '../primitive';
import { PathPart, JsonValue } from '../primitive';
import { NodeId } from '../schema';
import { DocumentNode, verboseTypeof, deepGet } from '../util';

Expand All @@ -22,7 +21,7 @@ function getOriginalFieldArguments(id: NodeId): { [argName: string]: string } |
/**
* Apollo-specific transaction interface.
*/
export class ApolloTransaction extends ApolloQueryable implements ApolloCache<GraphSnapshot> {
export class ApolloTransaction extends ApolloQueryable<GraphSnapshot> {

constructor(
/** The underlying transaction. */
Expand Down
2 changes: 1 addition & 1 deletion src/apollo/util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getFragmentQueryDocument } from 'apollo-utilities';
import { getFragmentQueryDocument } from '@apollo/client/utilities';

import { JsonObject } from '../primitive';
import { NodeId, RawOperation, StaticNodeId } from '../schema';
Expand Down
3 changes: 2 additions & 1 deletion src/context/CacheContext.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { addTypenameToDocument, isEqual } from 'apollo-utilities';
import { addTypenameToDocument } from '@apollo/client/utilities';
import isEqual from '@wry/equality';

import { ApolloTransaction } from '../apollo/Transaction';
import { CacheSnapshot } from '../CacheSnapshot';
Expand Down
2 changes: 1 addition & 1 deletion src/operations/SnapshotEditor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isEqual } from 'apollo-utilities';
import isEqual from '@wry/equality';

import { CacheContext } from '../context';
import { InvalidPayloadError, OperationError } from '../errors';
Expand Down
44 changes: 32 additions & 12 deletions src/util/ast.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
import { valueFromNode, FragmentMap } from 'apollo-utilities';
// We only depend on graphql for its types; nothing at runtime.
import { // eslint-disable-line import/no-extraneous-dependencies
ArgumentNode,
DocumentNode,
FieldNode,
OperationDefinitionNode,
ValueNode,
OperationTypeNode,
SelectionNode,
SelectionSetNode,
FieldNode,
ValueNode,
} from 'graphql';
import { getOperationDefinition, FragmentMap } from '@apollo/client/utilities';
import invariant from 'ts-invariant';

import { JsonValue } from '../primitive';

import { isObject } from './primitive';

export {
getOperationDefinitionOrDie as getOperationOrDie,
variablesInOperation,
valueFromNode,
FragmentMap,
} from 'apollo-utilities';
import { valueFromNode } from './store';

// AST types for convenience.
export {
Expand All @@ -28,8 +26,30 @@ export {
SelectionNode,
SelectionSetNode,
ValueNode,
// FieldNode,
} from 'graphql';
FragmentMap,
};

export function getOperationOrDie(
document: DocumentNode,
): OperationDefinitionNode {
const def = getOperationDefinition(document);
invariant(def, `GraphQL document is missing an operation`);
return def as OperationDefinitionNode;
}

/**
* Returns the names of all variables declared by the operation.
*/
export function variablesInOperation(operation: OperationDefinitionNode): Set<string> {
const names = new Set<string>();
if (operation.variableDefinitions) {
for (const definition of operation.variableDefinitions) {
names.add(definition.variable.name.value);
}
}

return names;
}

/**
* Returns the default values of all variables in the operation.
Expand Down
1 change: 1 addition & 0 deletions src/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export * from './ast';
export * from './collection';
export * from './primitive';
export * from './references';
export * from './store';
export * from './tree';
2 changes: 1 addition & 1 deletion src/util/references.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isEqual } from 'apollo-utilities';
import isEqual from '@wry/equality';

import { NodeReference, NodeSnapshot } from '../nodes';
import { PathPart } from '../primitive';
Expand Down
42 changes: 42 additions & 0 deletions src/util/store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// We only depend on graphql for its types; nothing at runtime.
// eslint-disable-next-line import/no-extraneous-dependencies
import { ValueNode, VariableNode } from 'graphql';
import { InvariantError } from 'ts-invariant';
import { VariableValue } from '@apollo/client/utilities';

// Note: These functions were originally a part of apollo-utilities, but were
// removed 87c7a2bc as they were unused within the apollo-client project.

function defaultValueFromVariable(_node: VariableNode) {
throw new InvariantError(`Variable nodes are not supported by valueFromNode`);
}

/**
* Evaluate a ValueNode and yield its value in its natural JS form.
*/
export function valueFromNode(
node: ValueNode,
onVariable: VariableValue = defaultValueFromVariable,
): any {
switch (node.kind) {
case 'Variable':
return onVariable(node);
case 'NullValue':
return null;
case 'IntValue':
return parseInt(node.value);
case 'FloatValue':
return parseFloat(node.value);
case 'ListValue':
return node.values.map(v => valueFromNode(v, onVariable));
case 'ObjectValue': {
const value: { [key: string]: any } = {};
for (const field of node.fields) {
value[field.name.value] = valueFromNode(field.value, onVariable);
}
return value;
}
default:
return node.value;
}
}
4 changes: 2 additions & 2 deletions test/env/base.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as chai from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
import chai from 'chai';
import chaiAsPromised from 'chai-as-promised';
import chaiJestDiff from 'chai-jest-diff';

// Chai
Expand Down
Loading

0 comments on commit 8679ef0

Please sign in to comment.