-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor to avoid import cycle between inMemoryCache.ts
and readFromStore.ts
#8850
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,12 @@ | ||
import { SelectionSetNode } from 'graphql'; | ||
|
||
import { NormalizedCache } from './types'; | ||
import { | ||
NormalizedCache, | ||
InMemoryCacheConfig, | ||
} from './types'; | ||
|
||
import { KeyFieldsContext } from './policies'; | ||
|
||
import { | ||
Reference, | ||
isReference, | ||
|
@@ -11,12 +17,55 @@ import { | |
resultKeyNameFromField, | ||
shouldInclude, | ||
isNonNullObject, | ||
compact, | ||
} from '../../utilities'; | ||
|
||
export const { | ||
hasOwnProperty: hasOwn, | ||
} = Object.prototype; | ||
|
||
export function defaultDataIdFromObject( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved |
||
{ __typename, id, _id }: Readonly<StoreObject>, | ||
context?: KeyFieldsContext, | ||
): string | undefined { | ||
if (typeof __typename === "string") { | ||
if (context) { | ||
context.keyObject = | ||
id !== void 0 ? { id } : | ||
_id !== void 0 ? { _id } : | ||
void 0; | ||
} | ||
// If there is no object.id, fall back to object._id. | ||
if (id === void 0) id = _id; | ||
if (id !== void 0) { | ||
return `${__typename}:${( | ||
typeof id === "number" || | ||
typeof id === "string" | ||
) ? id : JSON.stringify(id)}`; | ||
} | ||
} | ||
} | ||
|
||
const defaultConfig = { | ||
dataIdFromObject: defaultDataIdFromObject, | ||
addTypename: true, | ||
resultCaching: true, | ||
// Thanks to the shouldCanonizeResults helper, this should be the only line | ||
// you have to change to reenable canonization by default in the future. | ||
canonizeResults: false, | ||
}; | ||
|
||
export function normalizeConfig(config: InMemoryCacheConfig) { | ||
return compact(defaultConfig, config); | ||
} | ||
|
||
export function shouldCanonizeResults( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
config: Pick<InMemoryCacheConfig, "canonizeResults">, | ||
): boolean { | ||
const value = config.canonizeResults; | ||
return value === void 0 ? defaultConfig.canonizeResults : value; | ||
} | ||
|
||
export function getTypenameFromStoreObject( | ||
store: NormalizedCache, | ||
objectOrReference: StoreObject | Reference, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
InMemoryCacheConfig
is now exported by theline below.