apollo-link-state
Advanced tools
Comparing version 0.1.0 to 0.2.0
@@ -7,2 +7,10 @@ (function (global, factory) { | ||
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 connectionRemoveConfig = { | ||
@@ -22,2 +30,90 @@ test: function (directive) { return directive.name.value === 'client'; }, | ||
} | ||
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) { | ||
var frag = { | ||
kind: 'FragmentDefinition', | ||
typeCondition: { | ||
kind: 'NamedType', | ||
name: { | ||
kind: 'Name', | ||
value: '__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; | ||
} | ||
function addWriteDataToCache(cache) { | ||
cache.writeData = function (_a) { | ||
var id = _a.id, data = _a.data; | ||
if (id) { | ||
cache.writeFragment({ | ||
fragment: fragmentFromPojo(data), | ||
data: __assign({}, data, { __typename: '__FakeType' }), | ||
id: id, | ||
}); | ||
} | ||
else { | ||
cache.writeQuery({ | ||
query: queryFromPojo(data), | ||
data: data, | ||
}); | ||
} | ||
}; | ||
} | ||
@@ -49,3 +145,8 @@ var capitalizeFirstLetter = function (str) { return str.charAt(0).toUpperCase() + str.slice(1); }; | ||
var data = _a.data, errors = _a.errors; | ||
graphqlAnywhere_lib_async.graphql(resolver, query, data, operation.getContext(), operation.variables).then(function (nextData) { | ||
var context = operation.getContext(); | ||
var cache = context.cache; | ||
if (cache && !cache.writeData) { | ||
addWriteDataToCache(cache); | ||
} | ||
graphqlAnywhere_lib_async.graphql(resolver, query, data, context, operation.variables).then(function (nextData) { | ||
observer.next({ data: nextData, errors: errors }); | ||
@@ -52,0 +153,0 @@ observer.complete(); |
import { ApolloLink } from 'apollo-link'; | ||
import { ApolloCache } from 'apollo-cache'; | ||
export declare type WriteDataArgs = { | ||
id?: string; | ||
data: any; | ||
}; | ||
export declare type WriteData = { | ||
writeData: ({id, data}: WriteDataArgs) => void; | ||
}; | ||
export declare type ApolloCacheClient = ApolloCache<any> & WriteData; | ||
export declare const withClientState: (resolvers: any) => ApolloLink; |
import { ApolloLink, Observable } from 'apollo-link'; | ||
import { hasDirectives, getMainDefinition } from 'apollo-utilities'; | ||
import { graphql } from 'graphql-anywhere/lib/async'; | ||
import { removeClientSetsFromDocument } from './utils'; | ||
import { removeClientSetsFromDocument, addWriteDataToCache } from './utils'; | ||
var capitalizeFirstLetter = function (str) { return str.charAt(0).toUpperCase() + str.slice(1); }; | ||
@@ -30,3 +30,8 @@ export var withClientState = function (resolvers) { | ||
var data = _a.data, errors = _a.errors; | ||
graphql(resolver, query, data, operation.getContext(), operation.variables).then(function (nextData) { | ||
var context = operation.getContext(); | ||
var cache = context.cache; | ||
if (cache && !cache.writeData) { | ||
addWriteDataToCache(cache); | ||
} | ||
graphql(resolver, query, data, context, operation.variables).then(function (nextData) { | ||
observer.next({ data: nextData, errors: errors }); | ||
@@ -33,0 +38,0 @@ observer.complete(); |
import { DocumentNode } from 'graphql'; | ||
import { ApolloCacheClient } from './'; | ||
export declare function removeClientSetsFromDocument(query: DocumentNode): DocumentNode; | ||
export declare function queryFromPojo(obj: any): DocumentNode; | ||
export declare function fragmentFromPojo(obj: any): DocumentNode; | ||
export declare function addWriteDataToCache(cache: ApolloCacheClient): void; |
@@ -0,1 +1,9 @@ | ||
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 { checkDocument, removeDirectivesFromDocument, } from 'apollo-utilities'; | ||
@@ -16,2 +24,90 @@ var connectionRemoveConfig = { | ||
} | ||
export 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; | ||
} | ||
export function fragmentFromPojo(obj) { | ||
var frag = { | ||
kind: 'FragmentDefinition', | ||
typeCondition: { | ||
kind: 'NamedType', | ||
name: { | ||
kind: 'Name', | ||
value: '__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; | ||
} | ||
export function addWriteDataToCache(cache) { | ||
cache.writeData = function (_a) { | ||
var id = _a.id, data = _a.data; | ||
if (id) { | ||
cache.writeFragment({ | ||
fragment: fragmentFromPojo(data), | ||
data: __assign({}, data, { __typename: '__FakeType' }), | ||
id: id, | ||
}); | ||
} | ||
else { | ||
cache.writeQuery({ | ||
query: queryFromPojo(data), | ||
data: data, | ||
}); | ||
} | ||
}; | ||
} | ||
//# sourceMappingURL=utils.js.map |
{ | ||
"name": "apollo-link-state", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "An easy way to manage local state with Apollo Link", | ||
@@ -5,0 +5,0 @@ "author": "James Baxley <james@meteor.com>", |
import { ApolloLink, Observable, Operation, NextLink } from 'apollo-link'; | ||
import { ApolloCache } from 'apollo-cache'; | ||
@@ -6,6 +7,17 @@ import { hasDirectives, getMainDefinition } from 'apollo-utilities'; | ||
import { removeClientSetsFromDocument } from './utils'; | ||
import { removeClientSetsFromDocument, addWriteDataToCache } from './utils'; | ||
const capitalizeFirstLetter = str => str.charAt(0).toUpperCase() + str.slice(1); | ||
export type WriteDataArgs = { | ||
id?: string; | ||
data: any; | ||
}; | ||
export type WriteData = { | ||
writeData: ({ id, data }: WriteDataArgs) => void; | ||
}; | ||
export type ApolloCacheClient = ApolloCache<any> & WriteData; | ||
export const withClientState = resolvers => { | ||
@@ -43,12 +55,17 @@ return new ApolloLink((operation: Operation, forward: NextLink) => { | ||
next: ({ data, errors }) => { | ||
graphql( | ||
resolver, | ||
query, | ||
data, | ||
operation.getContext(), | ||
operation.variables, | ||
).then(nextData => { | ||
observer.next({ data: nextData, errors }); | ||
observer.complete(); | ||
}); | ||
const context = operation.getContext(); | ||
// Add a writeData method to the cache | ||
const cache: ApolloCacheClient = context.cache; | ||
if (cache && !cache.writeData) { | ||
addWriteDataToCache(cache); | ||
} | ||
graphql(resolver, query, data, context, operation.variables).then( | ||
nextData => { | ||
observer.next({ data: nextData, errors }); | ||
observer.complete(); | ||
}, | ||
); | ||
}, | ||
@@ -55,0 +72,0 @@ error: observer.error.bind(observer), |
121
src/utils.ts
@@ -1,2 +0,9 @@ | ||
import { DocumentNode, DirectiveNode } from 'graphql'; | ||
import { | ||
DocumentNode, | ||
DirectiveNode, | ||
OperationDefinitionNode, | ||
SelectionSetNode, | ||
FieldNode, | ||
FragmentDefinitionNode, | ||
} from 'graphql'; | ||
@@ -9,2 +16,4 @@ import { | ||
import { ApolloCacheClient, WriteDataArgs } from './'; | ||
const connectionRemoveConfig = { | ||
@@ -34,1 +43,111 @@ test: (directive: DirectiveNode) => directive.name.value === 'client', | ||
} | ||
export function queryFromPojo(obj: any): DocumentNode { | ||
const op: OperationDefinitionNode = { | ||
kind: 'OperationDefinition', | ||
operation: 'query', | ||
name: { | ||
kind: 'Name', | ||
value: 'GeneratedClientQuery', | ||
}, | ||
selectionSet: selectionSetFromObj(obj), | ||
}; | ||
const out: DocumentNode = { | ||
kind: 'Document', | ||
definitions: [op], | ||
}; | ||
return out; | ||
} | ||
export function fragmentFromPojo(obj: any): DocumentNode { | ||
const frag: FragmentDefinitionNode = { | ||
kind: 'FragmentDefinition', | ||
typeCondition: { | ||
kind: 'NamedType', | ||
name: { | ||
kind: 'Name', | ||
value: '__FakeType', | ||
}, | ||
}, | ||
name: { | ||
kind: 'Name', | ||
value: 'GeneratedClientQuery', | ||
}, | ||
selectionSet: selectionSetFromObj(obj), | ||
}; | ||
const out: DocumentNode = { | ||
kind: 'Document', | ||
definitions: [frag], | ||
}; | ||
return out; | ||
} | ||
function selectionSetFromObj(obj) { | ||
if ( | ||
typeof obj === 'number' || | ||
typeof obj === 'boolean' || | ||
typeof obj === 'string' || | ||
typeof obj === 'undefined' || | ||
obj === null | ||
) { | ||
// No selection set here | ||
return null; | ||
} | ||
if (Array.isArray(obj)) { | ||
// GraphQL queries don't include arrays | ||
return selectionSetFromObj(obj[0]); | ||
} | ||
// Now we know it's an object | ||
const selections: FieldNode[] = []; | ||
Object.keys(obj).forEach(key => { | ||
const field: FieldNode = { | ||
kind: 'Field', | ||
name: { | ||
kind: 'Name', | ||
value: key, | ||
}, | ||
}; | ||
// Recurse | ||
const nestedSelSet: SelectionSetNode = selectionSetFromObj(obj[key]); | ||
if (nestedSelSet) { | ||
field.selectionSet = nestedSelSet; | ||
} | ||
selections.push(field); | ||
}); | ||
const selectionSet: SelectionSetNode = { | ||
kind: 'SelectionSet', | ||
selections, | ||
}; | ||
return selectionSet; | ||
} | ||
export function addWriteDataToCache(cache: ApolloCacheClient) { | ||
cache.writeData = ({ id, data }: WriteDataArgs) => { | ||
if (id) { | ||
cache.writeFragment({ | ||
fragment: fragmentFromPojo(data), | ||
// Add a type here to satisfy the inmemory cache | ||
data: { ...data, __typename: '__FakeType' }, | ||
id, | ||
}); | ||
} else { | ||
cache.writeQuery({ | ||
query: queryFromPojo(data), | ||
data, | ||
}); | ||
} | ||
}; | ||
} |
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
60061
20
1411
1