@swan-io/graphql-client
Advanced tools
Comparing version 0.1.7 to 0.2.0
@@ -24,3 +24,3 @@ import { DocumentNode, OperationDefinitionNode } from "@0no-co/graphql.web"; | ||
cacheIfEligible<T>(value: T, requestedKeys: Set<symbol>): symbol | T; | ||
updateFieldInClosestCachedAncestor({ originalFieldName, fieldNameWithArguments, value, path, ancestors, variables, rootTypename, selectedKeys, }: { | ||
updateFieldInClosestCachedAncestor({ originalFieldName, fieldNameWithArguments, value, path, jsonPath, ancestors, variables, queryVariables, rootTypename, selectedKeys, documentNode, }: { | ||
originalFieldName: string; | ||
@@ -30,6 +30,9 @@ fieldNameWithArguments: symbol | string; | ||
path: PropertyKey[]; | ||
jsonPath: PropertyKey[]; | ||
ancestors: unknown[]; | ||
variables: Record<string, unknown>; | ||
queryVariables: Record<string, unknown>; | ||
rootTypename: string; | ||
selectedKeys: Set<symbol>; | ||
documentNode: DocumentNode; | ||
}): void; | ||
@@ -36,0 +39,0 @@ unsafe__update<A>(cacheKey: symbol, path: (symbol | string)[], updater: (value: A) => A): void; |
@@ -189,6 +189,9 @@ 'use strict'; | ||
path, | ||
jsonPath, | ||
ancestors, | ||
variables, | ||
queryVariables, | ||
rootTypename, | ||
selectedKeys | ||
selectedKeys, | ||
documentNode | ||
}) { | ||
@@ -215,2 +218,5 @@ const ancestorsCopy = ancestors.concat(); | ||
value.__connectionArguments = variables; | ||
value.__connectionQueryArguments = queryVariables; | ||
value.__connectionDocumentNode = documentNode; | ||
value.__connectionJsonPath = [...jsonPath, originalFieldName]; | ||
} | ||
@@ -851,3 +857,3 @@ value[REQUESTED_KEYS] = selectedKeys; | ||
var writeOperationToCache = (cache, document, response, variables) => { | ||
const traverse = (selections, data, path = [], rootTypename) => { | ||
const traverse = (selections, data, path = [], jsonPath = [], rootTypename) => { | ||
selections.selections.forEach((selection) => { | ||
@@ -871,6 +877,9 @@ tsPattern.match(selection).with({ kind: graphql_web.Kind.FIELD }, (fieldNode) => { | ||
path, | ||
jsonPath, | ||
ancestors: data, | ||
variables: fieldArguments, | ||
queryVariables: variables, | ||
rootTypename, | ||
selectedKeys | ||
selectedKeys, | ||
documentNode: document | ||
}); | ||
@@ -883,6 +892,9 @@ const nextValue = Array(fieldValue.length); | ||
path: [...path, fieldNameWithArguments], | ||
jsonPath: [...jsonPath, originalFieldName], | ||
ancestors: [...data, fieldValue], | ||
variables: fieldArguments, | ||
queryVariables: variables, | ||
rootTypename, | ||
selectedKeys | ||
selectedKeys, | ||
documentNode: document | ||
}); | ||
@@ -896,6 +908,9 @@ fieldValue.forEach((item, index) => { | ||
path: [...path, fieldNameWithArguments], | ||
jsonPath: [...jsonPath, originalFieldName], | ||
ancestors: [...data, fieldValue], | ||
variables: fieldArguments, | ||
queryVariables: variables, | ||
rootTypename, | ||
selectedKeys | ||
selectedKeys, | ||
documentNode: document | ||
}); | ||
@@ -907,2 +922,3 @@ if (isRecord(item)) { | ||
[...path, fieldNameWithArguments, index.toString()], | ||
[...jsonPath, originalFieldName, index.toString()], | ||
rootTypename | ||
@@ -919,6 +935,9 @@ ); | ||
path, | ||
jsonPath, | ||
ancestors: data, | ||
variables: fieldArguments, | ||
queryVariables: variables, | ||
rootTypename, | ||
selectedKeys | ||
selectedKeys, | ||
documentNode: document | ||
}); | ||
@@ -930,2 +949,3 @@ if (isRecord(fieldValue) && fieldNode.selectionSet != void 0) { | ||
[...path, fieldNameWithArguments], | ||
[...jsonPath, originalFieldName], | ||
rootTypename | ||
@@ -942,6 +962,9 @@ ); | ||
path, | ||
jsonPath, | ||
ancestors: data, | ||
variables: fieldArguments, | ||
queryVariables: variables, | ||
rootTypename, | ||
selectedKeys | ||
selectedKeys, | ||
documentNode: document | ||
}); | ||
@@ -951,3 +974,9 @@ } | ||
}).with({ kind: graphql_web.Kind.INLINE_FRAGMENT }, (inlineFragmentNode) => { | ||
traverse(inlineFragmentNode.selectionSet, data, path, rootTypename); | ||
traverse( | ||
inlineFragmentNode.selectionSet, | ||
data, | ||
path, | ||
jsonPath, | ||
rootTypename | ||
); | ||
}).with({ kind: graphql_web.Kind.FRAGMENT_SPREAD }, () => { | ||
@@ -967,3 +996,3 @@ }).exhaustive(); | ||
); | ||
traverse(definition.selectionSet, [response], [], rootTypename); | ||
traverse(definition.selectionSet, [response], [], [], rootTypename); | ||
} | ||
@@ -1447,9 +1476,58 @@ }); | ||
return (connection) => { | ||
const connectionRef = react.useRef(connection); | ||
connectionRef.current = mergeConnection( | ||
connectionRef.current, | ||
connection, | ||
direction | ||
const client = react.useContext(ClientContext); | ||
const connectionArgumentsRef = react.useRef( | ||
[] | ||
); | ||
return connectionRef.current; | ||
if (connection != null && "__connectionQueryArguments" in connection) { | ||
const arg = connection.__connectionQueryArguments; | ||
const serializedArg = serializeVariables(arg); | ||
if (!connectionArgumentsRef.current.find( | ||
([serialized]) => serializedArg === serialized | ||
)) { | ||
connectionArgumentsRef.current = [ | ||
...connectionArgumentsRef.current, | ||
[serializedArg, arg] | ||
]; | ||
} | ||
} | ||
const jsonPath = react.useRef( | ||
connection != null && "__connectionJsonPath" in connection ? connection.__connectionJsonPath : [] | ||
); | ||
const documentNode = connection != null && "__connectionDocumentNode" in connection ? connection.__connectionDocumentNode : void 0; | ||
const lastReturnedValueRef = react.useRef(boxed.Option.None()); | ||
const getSnapshot = react.useCallback(() => { | ||
if (documentNode == null) { | ||
return boxed.Option.None(); | ||
} | ||
const value = boxed.Option.all( | ||
connectionArgumentsRef.current.map( | ||
([, args]) => client.readFromCache(documentNode, args, {}) | ||
) | ||
).map(boxed.Result.all).flatMap((x) => x.toOption()).map( | ||
(queries) => queries.map( | ||
(query) => jsonPath.current.reduce( | ||
(acc, key) => acc != null && typeof acc === "object" && key in acc ? ( | ||
// @ts-expect-error indexable | ||
acc[key] | ||
) : null, | ||
query | ||
) | ||
) | ||
); | ||
if (!deepEqual(value, lastReturnedValueRef.current)) { | ||
lastReturnedValueRef.current = value; | ||
return value; | ||
} else { | ||
return lastReturnedValueRef.current; | ||
} | ||
}, [client, documentNode]); | ||
const data = react.useSyncExternalStore( | ||
(func) => client.subscribe(func), | ||
getSnapshot | ||
); | ||
return data.map( | ||
([first, ...rest]) => rest.reduce((acc, item) => { | ||
return mergeConnection(acc, item, direction); | ||
}, first) | ||
).getOr(connection); | ||
}; | ||
@@ -1456,0 +1534,0 @@ }; |
{ | ||
"name": "@swan-io/graphql-client", | ||
"version": "0.1.7", | ||
"version": "0.2.0", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "description": "A simple, typesafe GraphQL client for React", |
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
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
368176
3555