Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

apollo-cache

Package Overview
Dependencies
Maintainers
3
Versions
100
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

apollo-cache - npm Package Compare versions

Comparing version 1.0.2 to 1.1.0

lib/utils.d.ts

6

CHANGELOG.md
### vNext
### 1.1.0
- Add cache.writeData to base cache type & DataProxy [PR#2818](https://github.com/apollographql/apollo-client/pull/2818)
### 1.0.3
- depenedency updates
### 1.0.2

@@ -5,0 +11,0 @@ - package depenedency updates

@@ -7,2 +7,109 @@ (function (global, factory) {

function queryFromPojo(obj) {
var op = {
kind: 'OperationDefinition',
operation: 'query',
name: {
kind: 'Name',
value: 'GeneratedClientQuery',
},
selectionSet: selectionSetFromObj(obj),
};
var out = {
kind: 'Document',
definitions: [op],
};
return out;
}
function fragmentFromPojo(obj, typename) {
var frag = {
kind: 'FragmentDefinition',
typeCondition: {
kind: 'NamedType',
name: {
kind: 'Name',
value: typename || '__FakeType',
},
},
name: {
kind: 'Name',
value: 'GeneratedClientQuery',
},
selectionSet: selectionSetFromObj(obj),
};
var out = {
kind: 'Document',
definitions: [frag],
};
return out;
}
function selectionSetFromObj(obj) {
if (typeof obj === 'number' ||
typeof obj === 'boolean' ||
typeof obj === 'string' ||
typeof obj === 'undefined' ||
obj === null) {
return null;
}
if (Array.isArray(obj)) {
return selectionSetFromObj(obj[0]);
}
var selections = [];
Object.keys(obj).forEach(function (key) {
var field = {
kind: 'Field',
name: {
kind: 'Name',
value: key,
},
};
var nestedSelSet = selectionSetFromObj(obj[key]);
if (nestedSelSet) {
field.selectionSet = nestedSelSet;
}
selections.push(field);
});
var selectionSet = {
kind: 'SelectionSet',
selections: selections,
};
return selectionSet;
}
var justTypenameQuery = {
kind: 'Document',
definitions: [
{
kind: 'OperationDefinition',
operation: 'query',
name: null,
variableDefinitions: null,
directives: [],
selectionSet: {
kind: 'SelectionSet',
selections: [
{
kind: 'Field',
alias: null,
name: {
kind: 'Name',
value: '__typename',
},
arguments: [],
directives: [],
selectionSet: null,
},
],
},
},
],
};
var __assign = (undefined && undefined.__assign) || Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
var ApolloCache = (function () {

@@ -50,2 +157,27 @@ function ApolloCache() {

};
ApolloCache.prototype.writeData = function (_a) {
var id = _a.id, data = _a.data;
if (id) {
var typenameResult = null;
try {
typenameResult = this.read({
rootId: id,
optimistic: false,
query: justTypenameQuery,
});
}
catch (e) {
}
var __typename = (typenameResult && typenameResult.__typename) || '__ClientData';
var dataToWrite = __assign({ __typename: __typename }, data);
this.writeFragment({
id: id,
fragment: fragmentFromPojo(dataToWrite, __typename),
data: dataToWrite,
});
}
else {
this.writeQuery({ query: queryFromPojo(data), data: data });
}
};
return ApolloCache;

@@ -52,0 +184,0 @@ }());

1

lib/cache.d.ts

@@ -22,2 +22,3 @@ import { DocumentNode } from 'graphql';

writeFragment(options: Cache.WriteFragmentOptions): void;
writeData({id, data}: Cache.WriteDataOptions): void;
}

@@ -0,2 +1,11 @@

var __assign = (this && this.__assign) || Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
import { getFragmentQueryDocument } from 'apollo-utilities';
import { justTypenameQuery, queryFromPojo, fragmentFromPojo } from './utils';
var ApolloCache = (function () {

@@ -44,2 +53,27 @@ function ApolloCache() {

};
ApolloCache.prototype.writeData = function (_a) {
var id = _a.id, data = _a.data;
if (id) {
var typenameResult = null;
try {
typenameResult = this.read({
rootId: id,
optimistic: false,
query: justTypenameQuery,
});
}
catch (e) {
}
var __typename = (typenameResult && typenameResult.__typename) || '__ClientData';
var dataToWrite = __assign({ __typename: __typename }, data);
this.writeFragment({
id: id,
fragment: fragmentFromPojo(dataToWrite, __typename),
data: dataToWrite,
});
}
else {
this.writeQuery({ query: queryFromPojo(data), data: data });
}
};
return ApolloCache;

@@ -46,0 +80,0 @@ }());

@@ -28,3 +28,4 @@ import { DataProxy } from './DataProxy';

export import WriteFragmentOptions = DataProxy.WriteFragmentOptions;
export import WriteDataOptions = DataProxy.WriteDataOptions;
export import Fragment = DataProxy.Fragment;
}

@@ -1,2 +0,2 @@

import { DocumentNode } from "graphql";
import { DocumentNode } from 'graphql';
export declare namespace DataProxy {

@@ -19,2 +19,6 @@ interface Query {

}
interface WriteDataOptions {
data: any;
id?: string;
}
type DiffResult<T> = {

@@ -30,2 +34,3 @@ result?: T;

writeFragment(options: DataProxy.WriteFragmentOptions): void;
writeData(options: DataProxy.WriteDataOptions): void;
}

16

package.json
{
"name": "apollo-cache",
"version": "1.0.2",
"version": "1.1.0",
"description": "Core abstract of Caching layer for Apollo Client",

@@ -14,3 +14,3 @@ "author": "James Baxley <james@meteor.com>",

"module": "./lib/index.js",
"jsnext:main": "/lib/index.js",
"jsnext:main": "./lib/index.js",
"typings": "./lib/index.d.ts",

@@ -44,13 +44,13 @@ "repository": {

"dependencies": {
"apollo-utilities": "^1.0.3"
"apollo-utilities": "^1.0.4"
},
"devDependencies": {
"@types/graphql": "0.11.7",
"@types/jest": "21.1.8",
"browserify": "14.5.0",
"graphql": "0.11.7",
"graphql-tag": "2.4.2",
"@types/jest": "21.1.10",
"browserify": "15.0.0",
"graphql": "0.12.3",
"graphql-tag": "2.6.1",
"jest": "20.0.4",
"rimraf": "2.6.2",
"rollup": "0.52.0",
"rollup": "0.53.3",
"rollup-plugin-node-resolve": "3.0.0",

@@ -57,0 +57,0 @@ "ts-jest": "20.0.14",

@@ -5,2 +5,3 @@ import { DocumentNode } from 'graphql';

import { DataProxy, Cache } from './types';
import { justTypenameQuery, queryFromPojo, fragmentFromPojo } from './utils';

@@ -103,2 +104,36 @@ export type Transaction<T> = (c: ApolloCache<T>) => void;

}
public writeData({ id, data }: Cache.WriteDataOptions): void {
if (id) {
let typenameResult = null;
// Since we can't use fragments without having a typename in the store,
// we need to make sure we have one.
// To avoid overwriting an existing typename, we need to read it out first
// and generate a fake one if none exists.
try {
typenameResult = this.read({
rootId: id,
optimistic: false,
query: justTypenameQuery,
});
} catch (e) {
// Do nothing, since an error just means no typename exists
}
// tslint:disable-next-line
const __typename =
(typenameResult && typenameResult.__typename) || '__ClientData';
// Add a type here to satisfy the inmemory cache
const dataToWrite = { __typename, ...data };
this.writeFragment({
id,
fragment: fragmentFromPojo(dataToWrite, __typename),
data: dataToWrite,
});
} else {
this.writeQuery({ query: queryFromPojo(data), data });
}
}
}

@@ -35,3 +35,4 @@ import { DataProxy } from './DataProxy';

export import WriteFragmentOptions = DataProxy.WriteFragmentOptions;
export import WriteDataOptions = DataProxy.WriteDataOptions;
export import Fragment = DataProxy.Fragment;
}

@@ -1,2 +0,2 @@

import { DocumentNode } from "graphql"; // eslint-disable-line import/no-extraneous-dependencies, import/no-unresolved
import { DocumentNode } from 'graphql'; // eslint-disable-line import/no-extraneous-dependencies, import/no-unresolved

@@ -61,2 +61,12 @@ export namespace DataProxy {

export interface WriteDataOptions {
/**
* The data you will be writing to the store.
* It also takes an optional id property.
* The id is used to write a fragment to an existing object in the store.
*/
data: any;
id?: string;
}
export type DiffResult<T> = {

@@ -80,3 +90,3 @@ result?: T;

options: DataProxy.Query,
optimistic?: boolean
optimistic?: boolean,
): QueryType | null;

@@ -91,3 +101,3 @@

options: DataProxy.Fragment,
optimistic?: boolean
optimistic?: boolean,
): FragmentType | null;

@@ -106,2 +116,10 @@

writeFragment(options: DataProxy.WriteFragmentOptions): void;
/**
* Sugar for writeQuery & writeFragment.
* Writes data to the store without passing in a query.
* If you supply an id, the data will be written as a fragment to an existing object.
* Otherwise, the data is written to the root of the store.
*/
writeData(options: DataProxy.WriteDataOptions): void;
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc