apollo-cache
Version:
Core abstract of Caching layer for Apollo Client
99 lines • 2.56 kB
JavaScript
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, 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;
}
export 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,
},
],
},
},
],
};
//# sourceMappingURL=utils.js.map