-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add cache.writeData to base cache type & DataProxy (#2818)
* Add writeData to base cache type & DataProxy * Add writeData to ApolloClient * Added changelog, bumped up apollo-cache bundle size * Add all the tests from link-state * Fix snapshots
- Loading branch information
1 parent
54012c7
commit 3373141
Showing
10 changed files
with
505 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
packages/apollo-cache/src/__tests__/__snapshots__/utils.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[ | ||
`writing data with no query converts a JavaScript object to a query correctly arrays 1` | ||
] = ` | ||
"query GeneratedClientQuery { | ||
number | ||
bool | ||
nested { | ||
bool2 | ||
undef | ||
nullField | ||
str | ||
} | ||
} | ||
" | ||
`; | ||
|
||
exports[ | ||
`writing data with no query converts a JavaScript object to a query correctly basic 1` | ||
] = ` | ||
"query GeneratedClientQuery { | ||
number | ||
bool | ||
bool2 | ||
undef | ||
nullField | ||
str | ||
} | ||
" | ||
`; | ||
|
||
exports[ | ||
`writing data with no query converts a JavaScript object to a query correctly fragments 1` | ||
] = ` | ||
"fragment GeneratedClientQuery on __FakeType { | ||
number | ||
bool | ||
nested { | ||
bool2 | ||
undef | ||
nullField | ||
str | ||
} | ||
} | ||
" | ||
`; | ||
|
||
exports[ | ||
`writing data with no query converts a JavaScript object to a query correctly nested 1` | ||
] = ` | ||
"query GeneratedClientQuery { | ||
number | ||
bool | ||
nested { | ||
bool2 | ||
undef | ||
nullField | ||
str | ||
} | ||
} | ||
" | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { print } from 'graphql/language/printer'; | ||
import { queryFromPojo, fragmentFromPojo } from '../utils'; | ||
|
||
describe('writing data with no query', () => { | ||
describe('converts a JavaScript object to a query correctly', () => { | ||
it('basic', () => { | ||
expect( | ||
print( | ||
queryFromPojo({ | ||
number: 5, | ||
bool: true, | ||
bool2: false, | ||
undef: undefined, | ||
nullField: null, | ||
str: 'string', | ||
}), | ||
), | ||
).toMatchSnapshot(); | ||
}); | ||
|
||
it('nested', () => { | ||
expect( | ||
print( | ||
queryFromPojo({ | ||
number: 5, | ||
bool: true, | ||
nested: { | ||
bool2: false, | ||
undef: undefined, | ||
nullField: null, | ||
str: 'string', | ||
}, | ||
}), | ||
), | ||
).toMatchSnapshot(); | ||
}); | ||
|
||
it('arrays', () => { | ||
expect( | ||
print( | ||
queryFromPojo({ | ||
number: [5], | ||
bool: [[true]], | ||
nested: [ | ||
{ | ||
bool2: false, | ||
undef: undefined, | ||
nullField: null, | ||
str: 'string', | ||
}, | ||
], | ||
}), | ||
), | ||
).toMatchSnapshot(); | ||
}); | ||
|
||
it('fragments', () => { | ||
expect( | ||
print( | ||
fragmentFromPojo({ | ||
number: [5], | ||
bool: [[true]], | ||
nested: [ | ||
{ | ||
bool2: false, | ||
undef: undefined, | ||
nullField: null, | ||
str: 'string', | ||
}, | ||
], | ||
}), | ||
), | ||
).toMatchSnapshot(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import { | ||
DocumentNode, | ||
OperationDefinitionNode, | ||
SelectionSetNode, | ||
FieldNode, | ||
FragmentDefinitionNode, | ||
} from 'graphql'; | ||
|
||
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, typename?: string): DocumentNode { | ||
const frag: FragmentDefinitionNode = { | ||
kind: 'FragmentDefinition', | ||
typeCondition: { | ||
kind: 'NamedType', | ||
name: { | ||
kind: 'Name', | ||
value: typename || '__FakeType', | ||
}, | ||
}, | ||
name: { | ||
kind: 'Name', | ||
value: 'GeneratedClientQuery', | ||
}, | ||
selectionSet: selectionSetFromObj(obj), | ||
}; | ||
|
||
const out: DocumentNode = { | ||
kind: 'Document', | ||
definitions: [frag], | ||
}; | ||
|
||
return out; | ||
} | ||
|
||
function selectionSetFromObj(obj: any): SelectionSetNode { | ||
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 const justTypenameQuery: DocumentNode = { | ||
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, | ||
}, | ||
], | ||
}, | ||
}, | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.