Initial Sample.

This commit is contained in:
2024-06-03 20:23:50 +05:30
parent ef2b65f673
commit 5269ec3c66
2575 changed files with 282312 additions and 0 deletions

View File

@@ -0,0 +1,11 @@
import type { DocumentNode } from "graphql";
import type * as ReactTypes from "react";
import type { OperationOption, DataProps, MutateProps } from "./types.js";
import type { OperationVariables } from "../../core/index.js";
/**
* @deprecated
* Official support for React Apollo higher order components ended in March 2020.
* This library is still included in the `@apollo/client` package, but it no longer receives feature updates or bug fixes.
*/
export declare function graphql<TProps extends TGraphQLVariables | {} = {}, TData extends object = {}, TGraphQLVariables extends OperationVariables = {}, TChildProps extends object = Partial<DataProps<TData, TGraphQLVariables>> & Partial<MutateProps<TData, TGraphQLVariables>>>(document: DocumentNode, operationOptions?: OperationOption<TProps, TData, TGraphQLVariables, TChildProps>): (WrappedComponent: ReactTypes.ComponentType<TProps & TChildProps>) => ReactTypes.ComponentClass<TProps>;
//# sourceMappingURL=graphql.d.ts.map

View File

@@ -0,0 +1,22 @@
import { parser, DocumentType } from "../parser/index.js";
import { withQuery } from "./query-hoc.js";
import { withMutation } from "./mutation-hoc.js";
import { withSubscription } from "./subscription-hoc.js";
/**
* @deprecated
* Official support for React Apollo higher order components ended in March 2020.
* This library is still included in the `@apollo/client` package, but it no longer receives feature updates or bug fixes.
*/
export function graphql(document, operationOptions) {
if (operationOptions === void 0) { operationOptions = {}; }
switch (parser(document).type) {
case DocumentType.Mutation:
return withMutation(document, operationOptions);
case DocumentType.Subscription:
return withSubscription(document, operationOptions);
case DocumentType.Query:
default:
return withQuery(document, operationOptions);
}
}
//# sourceMappingURL=graphql.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"graphql.js","sourceRoot":"","sources":["../../../src/react/hoc/graphql.tsx"],"names":[],"mappings":"AAGA,OAAO,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAC1D,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAC3C,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAIzD;;;;GAIG;AACH,MAAM,UAAU,OAAO,CAOrB,QAAsB,EACtB,gBAKM;IALN,iCAAA,EAAA,qBAKM;IAIN,QAAQ,MAAM,CAAC,QAAQ,CAAC,CAAC,IAAI,EAAE,CAAC;QAC9B,KAAK,YAAY,CAAC,QAAQ;YACxB,OAAO,YAAY,CAAC,QAAQ,EAAE,gBAAgB,CAAC,CAAC;QAClD,KAAK,YAAY,CAAC,YAAY;YAC5B,OAAO,gBAAgB,CAAC,QAAQ,EAAE,gBAAgB,CAAC,CAAC;QACtD,KAAK,YAAY,CAAC,KAAK,CAAC;QACxB;YACE,OAAO,SAAS,CAAC,QAAQ,EAAE,gBAAgB,CAAC,CAAC;IACjD,CAAC;AACH,CAAC","sourcesContent":["import type { DocumentNode } from \"graphql\";\nimport type * as ReactTypes from \"react\";\n\nimport { parser, DocumentType } from \"../parser/index.js\";\nimport { withQuery } from \"./query-hoc.js\";\nimport { withMutation } from \"./mutation-hoc.js\";\nimport { withSubscription } from \"./subscription-hoc.js\";\nimport type { OperationOption, DataProps, MutateProps } from \"./types.js\";\nimport type { OperationVariables } from \"../../core/index.js\";\n\n/**\n * @deprecated\n * Official support for React Apollo higher order components ended in March 2020.\n * This library is still included in the `@apollo/client` package, but it no longer receives feature updates or bug fixes.\n */\nexport function graphql<\n TProps extends TGraphQLVariables | {} = {},\n TData extends object = {},\n TGraphQLVariables extends OperationVariables = {},\n TChildProps extends object = Partial<DataProps<TData, TGraphQLVariables>> &\n Partial<MutateProps<TData, TGraphQLVariables>>,\n>(\n document: DocumentNode,\n operationOptions: OperationOption<\n TProps,\n TData,\n TGraphQLVariables,\n TChildProps\n > = {}\n): (\n WrappedComponent: ReactTypes.ComponentType<TProps & TChildProps>\n) => ReactTypes.ComponentClass<TProps> {\n switch (parser(document).type) {\n case DocumentType.Mutation:\n return withMutation(document, operationOptions);\n case DocumentType.Subscription:\n return withSubscription(document, operationOptions);\n case DocumentType.Query:\n default:\n return withQuery(document, operationOptions);\n }\n}\n"]}

View File

@@ -0,0 +1,18 @@
/// <reference types="react" />
import * as React from "rehackt";
import type { OperationVariables } from "../../core/index.js";
import type { IDocumentDefinition } from "../parser/index.js";
export declare const defaultMapPropsToOptions: () => {};
export declare const defaultMapResultToProps: <P>(props: P) => P;
export declare const defaultMapPropsToSkip: () => boolean;
export declare function getDisplayName<P>(WrappedComponent: React.ComponentType<P>): string;
export declare function calculateVariablesFromProps<TProps>(operation: IDocumentDefinition, props: TProps): OperationVariables;
export type RefSetter<TChildProps> = (ref: React.ComponentClass<TChildProps>) => void | void;
export declare class GraphQLBase<TProps, TChildProps, TState = any> extends React.Component<TProps, TState> {
withRef: boolean;
private wrappedInstance?;
constructor(props: TProps);
getWrappedInstance(): React.ComponentClass<TChildProps, any> | undefined;
setWrappedInstance(ref: React.ComponentClass<TChildProps>): void;
}
//# sourceMappingURL=hoc-utils.d.ts.map

View File

@@ -0,0 +1,48 @@
import { __extends } from "tslib";
import { invariant } from "../../utilities/globals/index.js";
import * as React from "rehackt";
export var defaultMapPropsToOptions = function () { return ({}); };
export var defaultMapResultToProps = function (props) { return props; };
export var defaultMapPropsToSkip = function () { return false; };
export function getDisplayName(WrappedComponent) {
return WrappedComponent.displayName || WrappedComponent.name || "Component";
}
export function calculateVariablesFromProps(operation, props) {
var variables = {};
for (var _i = 0, _a = operation.variables; _i < _a.length; _i++) {
var _b = _a[_i], variable = _b.variable, type = _b.type;
if (!variable.name || !variable.name.value)
continue;
var variableName = variable.name.value;
var variableProp = props[variableName];
if (typeof variableProp !== "undefined") {
variables[variableName] = variableProp;
continue;
}
// Allow optional props
if (type.kind !== "NonNullType") {
variables[variableName] = undefined;
}
}
return variables;
}
// base class for hocs to easily manage refs
var GraphQLBase = /** @class */ (function (_super) {
__extends(GraphQLBase, _super);
function GraphQLBase(props) {
var _this = _super.call(this, props) || this;
_this.withRef = false;
_this.setWrappedInstance = _this.setWrappedInstance.bind(_this);
return _this;
}
GraphQLBase.prototype.getWrappedInstance = function () {
invariant(this.withRef, 47);
return this.wrappedInstance;
};
GraphQLBase.prototype.setWrappedInstance = function (ref) {
this.wrappedInstance = ref;
};
return GraphQLBase;
}(React.Component));
export { GraphQLBase };
//# sourceMappingURL=hoc-utils.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"hoc-utils.js","sourceRoot":"","sources":["../../../src/react/hoc/hoc-utils.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,kCAAkC,CAAC;AAC7D,OAAO,KAAK,KAAK,MAAM,SAAS,CAAC;AAIjC,MAAM,CAAC,IAAM,wBAAwB,GAAG,cAAM,OAAA,CAAC,EAAE,CAAC,EAAJ,CAAI,CAAC;AACnD,MAAM,CAAC,IAAM,uBAAuB,GAAuB,UAAC,KAAK,IAAK,OAAA,KAAK,EAAL,CAAK,CAAC;AAC5E,MAAM,CAAC,IAAM,qBAAqB,GAAG,cAAM,OAAA,KAAK,EAAL,CAAK,CAAC;AAEjD,MAAM,UAAU,cAAc,CAAI,gBAAwC;IACxE,OAAO,gBAAgB,CAAC,WAAW,IAAI,gBAAgB,CAAC,IAAI,IAAI,WAAW,CAAC;AAC9E,CAAC;AAED,MAAM,UAAU,2BAA2B,CACzC,SAA8B,EAC9B,KAAa;IAEb,IAAI,SAAS,GAAuB,EAAE,CAAC;IACvC,KAA+B,UAAmB,EAAnB,KAAA,SAAS,CAAC,SAAS,EAAnB,cAAmB,EAAnB,IAAmB,EAAE,CAAC;QAA5C,IAAA,WAAkB,EAAhB,QAAQ,cAAA,EAAE,IAAI,UAAA;QACvB,IAAI,CAAC,QAAQ,CAAC,IAAI,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK;YAAE,SAAS;QAErD,IAAM,YAAY,GAAG,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC;QACzC,IAAM,YAAY,GAAI,KAAa,CAAC,YAAY,CAAC,CAAC;QAElD,IAAI,OAAO,YAAY,KAAK,WAAW,EAAE,CAAC;YACxC,SAAS,CAAC,YAAY,CAAC,GAAG,YAAY,CAAC;YACvC,SAAS;QACX,CAAC;QAED,uBAAuB;QACvB,IAAI,IAAI,CAAC,IAAI,KAAK,aAAa,EAAE,CAAC;YAChC,SAAS,CAAC,YAAY,CAAC,GAAG,SAAS,CAAC;QACtC,CAAC;IACH,CAAC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC;AAMD,4CAA4C;AAC5C;IAIU,+BAA+B;IAKvC,qBAAY,KAAa;QACvB,YAAA,MAAK,YAAC,KAAK,CAAC,SAAC;QALR,aAAO,GAAY,KAAK,CAAC;QAM9B,KAAI,CAAC,kBAAkB,GAAG,KAAI,CAAC,kBAAkB,CAAC,IAAI,CAAC,KAAI,CAAC,CAAC;;IAC/D,CAAC;IAED,wCAAkB,GAAlB;QACE,SAAS,CACP,IAAI,CAAC,OAAO,EACZ,sDAAsD;YACpD,kCAAkC,CACrC,CAAC;QAEF,OAAO,IAAI,CAAC,eAAe,CAAC;IAC9B,CAAC;IAED,wCAAkB,GAAlB,UAAmB,GAAsC;QACvD,IAAI,CAAC,eAAe,GAAG,GAAG,CAAC;IAC7B,CAAC;IACH,kBAAC;AAAD,CAAC,AA3BD,CAIU,KAAK,CAAC,SAAS,GAuBxB","sourcesContent":["import { invariant } from \"../../utilities/globals/index.js\";\nimport * as React from \"rehackt\";\nimport type { OperationVariables } from \"../../core/index.js\";\nimport type { IDocumentDefinition } from \"../parser/index.js\";\n\nexport const defaultMapPropsToOptions = () => ({});\nexport const defaultMapResultToProps: <P>(props: P) => P = (props) => props;\nexport const defaultMapPropsToSkip = () => false;\n\nexport function getDisplayName<P>(WrappedComponent: React.ComponentType<P>) {\n return WrappedComponent.displayName || WrappedComponent.name || \"Component\";\n}\n\nexport function calculateVariablesFromProps<TProps>(\n operation: IDocumentDefinition,\n props: TProps\n) {\n let variables: OperationVariables = {};\n for (let { variable, type } of operation.variables) {\n if (!variable.name || !variable.name.value) continue;\n\n const variableName = variable.name.value;\n const variableProp = (props as any)[variableName];\n\n if (typeof variableProp !== \"undefined\") {\n variables[variableName] = variableProp;\n continue;\n }\n\n // Allow optional props\n if (type.kind !== \"NonNullType\") {\n variables[variableName] = undefined;\n }\n }\n return variables;\n}\n\nexport type RefSetter<TChildProps> = (\n ref: React.ComponentClass<TChildProps>\n) => void | void;\n\n// base class for hocs to easily manage refs\nexport class GraphQLBase<\n TProps,\n TChildProps,\n TState = any,\n> extends React.Component<TProps, TState> {\n public withRef: boolean = false;\n // wrapped instance\n private wrappedInstance?: React.ComponentClass<TChildProps>;\n\n constructor(props: TProps) {\n super(props);\n this.setWrappedInstance = this.setWrappedInstance.bind(this);\n }\n\n getWrappedInstance() {\n invariant(\n this.withRef,\n `To access the wrapped instance, you need to specify ` +\n `{ withRef: true } in the options`\n );\n\n return this.wrappedInstance;\n }\n\n setWrappedInstance(ref: React.ComponentClass<TChildProps>) {\n this.wrappedInstance = ref;\n }\n}\n"]}

View File

@@ -0,0 +1,316 @@
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
var globals = require('../../utilities/globals');
var parser = require('../parser');
var tslib = require('tslib');
var React = require('rehackt');
var hoistNonReactStatics = require('hoist-non-react-statics');
var components = require('../components');
var context = require('../context');
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e["default"] : e; }
function _interopNamespace(e) {
if (e && e.__esModule) return e;
var n = Object.create(null);
if (e) {
for (var k in e) {
n[k] = e[k];
}
}
n["default"] = e;
return Object.freeze(n);
}
var React__namespace = /*#__PURE__*/_interopNamespace(React);
var hoistNonReactStatics__default = /*#__PURE__*/_interopDefaultLegacy(hoistNonReactStatics);
var defaultMapPropsToOptions = function () { return ({}); };
var defaultMapPropsToSkip = function () { return false; };
function getDisplayName$1(WrappedComponent) {
return WrappedComponent.displayName || WrappedComponent.name || "Component";
}
function calculateVariablesFromProps(operation, props) {
var variables = {};
for (var _i = 0, _a = operation.variables; _i < _a.length; _i++) {
var _b = _a[_i], variable = _b.variable, type = _b.type;
if (!variable.name || !variable.name.value)
continue;
var variableName = variable.name.value;
var variableProp = props[variableName];
if (typeof variableProp !== "undefined") {
variables[variableName] = variableProp;
continue;
}
if (type.kind !== "NonNullType") {
variables[variableName] = undefined;
}
}
return variables;
}
var GraphQLBase = (function (_super) {
tslib.__extends(GraphQLBase, _super);
function GraphQLBase(props) {
var _this = _super.call(this, props) || this;
_this.withRef = false;
_this.setWrappedInstance = _this.setWrappedInstance.bind(_this);
return _this;
}
GraphQLBase.prototype.getWrappedInstance = function () {
globals.invariant(this.withRef, 47);
return this.wrappedInstance;
};
GraphQLBase.prototype.setWrappedInstance = function (ref) {
this.wrappedInstance = ref;
};
return GraphQLBase;
}(React__namespace.Component));
function withQuery(document, operationOptions) {
if (operationOptions === void 0) { operationOptions = {}; }
var operation = parser.parser(document);
var _a = operationOptions.options, options = _a === void 0 ? defaultMapPropsToOptions : _a, _b = operationOptions.skip, skip = _b === void 0 ? defaultMapPropsToSkip : _b, _c = operationOptions.alias, alias = _c === void 0 ? "Apollo" : _c;
var mapPropsToOptions = options;
if (typeof mapPropsToOptions !== "function") {
mapPropsToOptions = function () { return options; };
}
var mapPropsToSkip = skip;
if (typeof mapPropsToSkip !== "function") {
mapPropsToSkip = function () { return skip; };
}
var lastResultProps;
return function (WrappedComponent) {
var graphQLDisplayName = "".concat(alias, "(").concat(getDisplayName$1(WrappedComponent), ")");
var GraphQL = (function (_super) {
tslib.__extends(GraphQL, _super);
function GraphQL() {
return _super !== null && _super.apply(this, arguments) || this;
}
GraphQL.prototype.render = function () {
var _this = this;
var props = this.props;
var shouldSkip = mapPropsToSkip(props);
var opts = shouldSkip ? Object.create(null) : tslib.__assign({}, mapPropsToOptions(props));
if (!shouldSkip && !opts.variables && operation.variables.length > 0) {
opts.variables = calculateVariablesFromProps(operation, props);
}
return (React__namespace.createElement(components.Query, tslib.__assign({}, opts, { displayName: graphQLDisplayName, skip: shouldSkip, query: document }), function (_a) {
var _b, _c;
_a.client; var data = _a.data, r = tslib.__rest(_a, ["client", "data"]);
if (operationOptions.withRef) {
_this.withRef = true;
props = Object.assign({}, props, {
ref: _this.setWrappedInstance,
});
}
if (shouldSkip) {
return (React__namespace.createElement(WrappedComponent, tslib.__assign({}, props, {})));
}
var result = Object.assign(r, data || {});
var name = operationOptions.name || "data";
var childProps = (_b = {}, _b[name] = result, _b);
if (operationOptions.props) {
var newResult = (_c = {},
_c[name] = result,
_c.ownProps = props,
_c);
lastResultProps = operationOptions.props(newResult, lastResultProps);
childProps = lastResultProps;
}
return (React__namespace.createElement(WrappedComponent, tslib.__assign({}, props, childProps)));
}));
};
GraphQL.displayName = graphQLDisplayName;
GraphQL.WrappedComponent = WrappedComponent;
return GraphQL;
}(GraphQLBase));
return hoistNonReactStatics__default(GraphQL, WrappedComponent, {});
};
}
function withMutation(document, operationOptions) {
if (operationOptions === void 0) { operationOptions = {}; }
var operation = parser.parser(document);
var _a = operationOptions.options, options = _a === void 0 ? defaultMapPropsToOptions : _a, _b = operationOptions.alias, alias = _b === void 0 ? "Apollo" : _b;
var mapPropsToOptions = options;
if (typeof mapPropsToOptions !== "function")
mapPropsToOptions = function () {
return options;
};
return function (WrappedComponent) {
var graphQLDisplayName = "".concat(alias, "(").concat(getDisplayName$1(WrappedComponent), ")");
var GraphQL = (function (_super) {
tslib.__extends(GraphQL, _super);
function GraphQL() {
return _super !== null && _super.apply(this, arguments) || this;
}
GraphQL.prototype.render = function () {
var props = this.props;
var opts = mapPropsToOptions(props);
if (operationOptions.withRef) {
this.withRef = true;
props = Object.assign({}, props, {
ref: this.setWrappedInstance,
});
}
if (!opts.variables && operation.variables.length > 0) {
opts.variables = calculateVariablesFromProps(operation, props);
}
return (React__namespace.createElement(components.Mutation, tslib.__assign({ ignoreResults: true }, opts, { mutation: document }), function (mutate, _a) {
var _b, _c;
var data = _a.data, r = tslib.__rest(_a, ["data"]);
var result = Object.assign(r, data || {});
var name = operationOptions.name || "mutate";
var resultName = operationOptions.name ? "".concat(name, "Result") : "result";
var childProps = (_b = {},
_b[name] = mutate,
_b[resultName] = result,
_b);
if (operationOptions.props) {
var newResult = (_c = {},
_c[name] = mutate,
_c[resultName] = result,
_c.ownProps = props,
_c);
childProps = operationOptions.props(newResult);
}
return React__namespace.createElement(WrappedComponent, tslib.__assign({}, props, childProps));
}));
};
GraphQL.displayName = graphQLDisplayName;
GraphQL.WrappedComponent = WrappedComponent;
return GraphQL;
}(GraphQLBase));
return hoistNonReactStatics__default(GraphQL, WrappedComponent, {});
};
}
function withSubscription(document, operationOptions) {
if (operationOptions === void 0) { operationOptions = {}; }
var operation = parser.parser(document);
var _a = operationOptions.options, options = _a === void 0 ? defaultMapPropsToOptions : _a, _b = operationOptions.skip, skip = _b === void 0 ? defaultMapPropsToSkip : _b, _c = operationOptions.alias, alias = _c === void 0 ? "Apollo" : _c, shouldResubscribe = operationOptions.shouldResubscribe;
var mapPropsToOptions = options;
if (typeof mapPropsToOptions !== "function")
mapPropsToOptions = function () { return options; };
var mapPropsToSkip = skip;
if (typeof mapPropsToSkip !== "function")
mapPropsToSkip = function () { return skip; };
var lastResultProps;
return function (WrappedComponent) {
var graphQLDisplayName = "".concat(alias, "(").concat(getDisplayName$1(WrappedComponent), ")");
var GraphQL = (function (_super) {
tslib.__extends(GraphQL, _super);
function GraphQL(props) {
var _this = _super.call(this, props) || this;
_this.state = { resubscribe: false };
return _this;
}
GraphQL.prototype.updateResubscribe = function (resubscribe) {
this.setState({ resubscribe: resubscribe });
};
GraphQL.prototype.componentDidUpdate = function (prevProps) {
var resubscribe = !!(shouldResubscribe && shouldResubscribe(prevProps, this.props));
if (this.state.resubscribe !== resubscribe) {
this.updateResubscribe(resubscribe);
}
};
GraphQL.prototype.render = function () {
var _this = this;
var props = this.props;
var shouldSkip = mapPropsToSkip(props);
var opts = shouldSkip ? Object.create(null) : mapPropsToOptions(props);
if (!shouldSkip && !opts.variables && operation.variables.length > 0) {
opts.variables = calculateVariablesFromProps(operation, props);
}
return (React__namespace.createElement(components.Subscription, tslib.__assign({}, opts, { displayName: graphQLDisplayName, skip: shouldSkip, subscription: document, shouldResubscribe: this.state.resubscribe }), function (_a) {
var _b, _c;
var data = _a.data, r = tslib.__rest(_a, ["data"]);
if (operationOptions.withRef) {
_this.withRef = true;
props = Object.assign({}, props, {
ref: _this.setWrappedInstance,
});
}
if (shouldSkip) {
return (React__namespace.createElement(WrappedComponent, tslib.__assign({}, props, {})));
}
var result = Object.assign(r, data || {});
var name = operationOptions.name || "data";
var childProps = (_b = {}, _b[name] = result, _b);
if (operationOptions.props) {
var newResult = (_c = {},
_c[name] = result,
_c.ownProps = props,
_c);
lastResultProps = operationOptions.props(newResult, lastResultProps);
childProps = lastResultProps;
}
return (React__namespace.createElement(WrappedComponent, tslib.__assign({}, props, childProps)));
}));
};
GraphQL.displayName = graphQLDisplayName;
GraphQL.WrappedComponent = WrappedComponent;
return GraphQL;
}(GraphQLBase));
return hoistNonReactStatics__default(GraphQL, WrappedComponent, {});
};
}
function graphql(document, operationOptions) {
if (operationOptions === void 0) { operationOptions = {}; }
switch (parser.parser(document).type) {
case parser.DocumentType.Mutation:
return withMutation(document, operationOptions);
case parser.DocumentType.Subscription:
return withSubscription(document, operationOptions);
case parser.DocumentType.Query:
default:
return withQuery(document, operationOptions);
}
}
function getDisplayName(WrappedComponent) {
return WrappedComponent.displayName || WrappedComponent.name || "Component";
}
function withApollo(WrappedComponent, operationOptions) {
if (operationOptions === void 0) { operationOptions = {}; }
var withDisplayName = "withApollo(".concat(getDisplayName(WrappedComponent), ")");
var WithApollo = (function (_super) {
tslib.__extends(WithApollo, _super);
function WithApollo(props) {
var _this = _super.call(this, props) || this;
_this.setWrappedInstance = _this.setWrappedInstance.bind(_this);
return _this;
}
WithApollo.prototype.getWrappedInstance = function () {
globals.invariant(operationOptions.withRef, 48);
return this.wrappedInstance;
};
WithApollo.prototype.setWrappedInstance = function (ref) {
this.wrappedInstance = ref;
};
WithApollo.prototype.render = function () {
var _this = this;
return (React__namespace.createElement(context.ApolloConsumer, null, function (client) {
var props = Object.assign({}, _this.props, {
client: client,
ref: operationOptions.withRef ? _this.setWrappedInstance : undefined,
});
return React__namespace.createElement(WrappedComponent, tslib.__assign({}, props));
}));
};
WithApollo.displayName = withDisplayName;
WithApollo.WrappedComponent = WrappedComponent;
return WithApollo;
}(React__namespace.Component));
return hoistNonReactStatics__default(WithApollo, WrappedComponent, {});
}
exports.graphql = graphql;
exports.withApollo = withApollo;
exports.withMutation = withMutation;
exports.withQuery = withQuery;
exports.withSubscription = withSubscription;
//# sourceMappingURL=hoc.cjs.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,316 @@
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
var globals = require('../../utilities/globals');
var parser = require('../parser');
var tslib = require('tslib');
var React = require('rehackt');
var hoistNonReactStatics = require('hoist-non-react-statics');
var components = require('../components');
var context = require('../context');
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e["default"] : e; }
function _interopNamespace(e) {
if (e && e.__esModule) return e;
var n = Object.create(null);
if (e) {
for (var k in e) {
n[k] = e[k];
}
}
n["default"] = e;
return Object.freeze(n);
}
var React__namespace = /*#__PURE__*/_interopNamespace(React);
var hoistNonReactStatics__default = /*#__PURE__*/_interopDefaultLegacy(hoistNonReactStatics);
var defaultMapPropsToOptions = function () { return ({}); };
var defaultMapPropsToSkip = function () { return false; };
function getDisplayName$1(WrappedComponent) {
return WrappedComponent.displayName || WrappedComponent.name || "Component";
}
function calculateVariablesFromProps(operation, props) {
var variables = {};
for (var _i = 0, _a = operation.variables; _i < _a.length; _i++) {
var _b = _a[_i], variable = _b.variable, type = _b.type;
if (!variable.name || !variable.name.value)
continue;
var variableName = variable.name.value;
var variableProp = props[variableName];
if (typeof variableProp !== "undefined") {
variables[variableName] = variableProp;
continue;
}
if (type.kind !== "NonNullType") {
variables[variableName] = undefined;
}
}
return variables;
}
var GraphQLBase = (function (_super) {
tslib.__extends(GraphQLBase, _super);
function GraphQLBase(props) {
var _this = _super.call(this, props) || this;
_this.withRef = false;
_this.setWrappedInstance = _this.setWrappedInstance.bind(_this);
return _this;
}
GraphQLBase.prototype.getWrappedInstance = function () {
globals.invariant(this.withRef, 47);
return this.wrappedInstance;
};
GraphQLBase.prototype.setWrappedInstance = function (ref) {
this.wrappedInstance = ref;
};
return GraphQLBase;
}(React__namespace.Component));
function withQuery(document, operationOptions) {
if (operationOptions === void 0) { operationOptions = {}; }
var operation = parser.parser(document);
var _a = operationOptions.options, options = _a === void 0 ? defaultMapPropsToOptions : _a, _b = operationOptions.skip, skip = _b === void 0 ? defaultMapPropsToSkip : _b, _c = operationOptions.alias, alias = _c === void 0 ? "Apollo" : _c;
var mapPropsToOptions = options;
if (typeof mapPropsToOptions !== "function") {
mapPropsToOptions = function () { return options; };
}
var mapPropsToSkip = skip;
if (typeof mapPropsToSkip !== "function") {
mapPropsToSkip = function () { return skip; };
}
var lastResultProps;
return function (WrappedComponent) {
var graphQLDisplayName = "".concat(alias, "(").concat(getDisplayName$1(WrappedComponent), ")");
var GraphQL = (function (_super) {
tslib.__extends(GraphQL, _super);
function GraphQL() {
return _super !== null && _super.apply(this, arguments) || this;
}
GraphQL.prototype.render = function () {
var _this = this;
var props = this.props;
var shouldSkip = mapPropsToSkip(props);
var opts = shouldSkip ? Object.create(null) : tslib.__assign({}, mapPropsToOptions(props));
if (!shouldSkip && !opts.variables && operation.variables.length > 0) {
opts.variables = calculateVariablesFromProps(operation, props);
}
return (React__namespace.createElement(components.Query, tslib.__assign({}, opts, { displayName: graphQLDisplayName, skip: shouldSkip, query: document }), function (_a) {
var _b, _c;
_a.client; var data = _a.data, r = tslib.__rest(_a, ["client", "data"]);
if (operationOptions.withRef) {
_this.withRef = true;
props = Object.assign({}, props, {
ref: _this.setWrappedInstance,
});
}
if (shouldSkip) {
return (React__namespace.createElement(WrappedComponent, tslib.__assign({}, props, {})));
}
var result = Object.assign(r, data || {});
var name = operationOptions.name || "data";
var childProps = (_b = {}, _b[name] = result, _b);
if (operationOptions.props) {
var newResult = (_c = {},
_c[name] = result,
_c.ownProps = props,
_c);
lastResultProps = operationOptions.props(newResult, lastResultProps);
childProps = lastResultProps;
}
return (React__namespace.createElement(WrappedComponent, tslib.__assign({}, props, childProps)));
}));
};
GraphQL.displayName = graphQLDisplayName;
GraphQL.WrappedComponent = WrappedComponent;
return GraphQL;
}(GraphQLBase));
return hoistNonReactStatics__default(GraphQL, WrappedComponent, {});
};
}
function withMutation(document, operationOptions) {
if (operationOptions === void 0) { operationOptions = {}; }
var operation = parser.parser(document);
var _a = operationOptions.options, options = _a === void 0 ? defaultMapPropsToOptions : _a, _b = operationOptions.alias, alias = _b === void 0 ? "Apollo" : _b;
var mapPropsToOptions = options;
if (typeof mapPropsToOptions !== "function")
mapPropsToOptions = function () {
return options;
};
return function (WrappedComponent) {
var graphQLDisplayName = "".concat(alias, "(").concat(getDisplayName$1(WrappedComponent), ")");
var GraphQL = (function (_super) {
tslib.__extends(GraphQL, _super);
function GraphQL() {
return _super !== null && _super.apply(this, arguments) || this;
}
GraphQL.prototype.render = function () {
var props = this.props;
var opts = mapPropsToOptions(props);
if (operationOptions.withRef) {
this.withRef = true;
props = Object.assign({}, props, {
ref: this.setWrappedInstance,
});
}
if (!opts.variables && operation.variables.length > 0) {
opts.variables = calculateVariablesFromProps(operation, props);
}
return (React__namespace.createElement(components.Mutation, tslib.__assign({ ignoreResults: true }, opts, { mutation: document }), function (mutate, _a) {
var _b, _c;
var data = _a.data, r = tslib.__rest(_a, ["data"]);
var result = Object.assign(r, data || {});
var name = operationOptions.name || "mutate";
var resultName = operationOptions.name ? "".concat(name, "Result") : "result";
var childProps = (_b = {},
_b[name] = mutate,
_b[resultName] = result,
_b);
if (operationOptions.props) {
var newResult = (_c = {},
_c[name] = mutate,
_c[resultName] = result,
_c.ownProps = props,
_c);
childProps = operationOptions.props(newResult);
}
return React__namespace.createElement(WrappedComponent, tslib.__assign({}, props, childProps));
}));
};
GraphQL.displayName = graphQLDisplayName;
GraphQL.WrappedComponent = WrappedComponent;
return GraphQL;
}(GraphQLBase));
return hoistNonReactStatics__default(GraphQL, WrappedComponent, {});
};
}
function withSubscription(document, operationOptions) {
if (operationOptions === void 0) { operationOptions = {}; }
var operation = parser.parser(document);
var _a = operationOptions.options, options = _a === void 0 ? defaultMapPropsToOptions : _a, _b = operationOptions.skip, skip = _b === void 0 ? defaultMapPropsToSkip : _b, _c = operationOptions.alias, alias = _c === void 0 ? "Apollo" : _c, shouldResubscribe = operationOptions.shouldResubscribe;
var mapPropsToOptions = options;
if (typeof mapPropsToOptions !== "function")
mapPropsToOptions = function () { return options; };
var mapPropsToSkip = skip;
if (typeof mapPropsToSkip !== "function")
mapPropsToSkip = function () { return skip; };
var lastResultProps;
return function (WrappedComponent) {
var graphQLDisplayName = "".concat(alias, "(").concat(getDisplayName$1(WrappedComponent), ")");
var GraphQL = (function (_super) {
tslib.__extends(GraphQL, _super);
function GraphQL(props) {
var _this = _super.call(this, props) || this;
_this.state = { resubscribe: false };
return _this;
}
GraphQL.prototype.updateResubscribe = function (resubscribe) {
this.setState({ resubscribe: resubscribe });
};
GraphQL.prototype.componentDidUpdate = function (prevProps) {
var resubscribe = !!(shouldResubscribe && shouldResubscribe(prevProps, this.props));
if (this.state.resubscribe !== resubscribe) {
this.updateResubscribe(resubscribe);
}
};
GraphQL.prototype.render = function () {
var _this = this;
var props = this.props;
var shouldSkip = mapPropsToSkip(props);
var opts = shouldSkip ? Object.create(null) : mapPropsToOptions(props);
if (!shouldSkip && !opts.variables && operation.variables.length > 0) {
opts.variables = calculateVariablesFromProps(operation, props);
}
return (React__namespace.createElement(components.Subscription, tslib.__assign({}, opts, { displayName: graphQLDisplayName, skip: shouldSkip, subscription: document, shouldResubscribe: this.state.resubscribe }), function (_a) {
var _b, _c;
var data = _a.data, r = tslib.__rest(_a, ["data"]);
if (operationOptions.withRef) {
_this.withRef = true;
props = Object.assign({}, props, {
ref: _this.setWrappedInstance,
});
}
if (shouldSkip) {
return (React__namespace.createElement(WrappedComponent, tslib.__assign({}, props, {})));
}
var result = Object.assign(r, data || {});
var name = operationOptions.name || "data";
var childProps = (_b = {}, _b[name] = result, _b);
if (operationOptions.props) {
var newResult = (_c = {},
_c[name] = result,
_c.ownProps = props,
_c);
lastResultProps = operationOptions.props(newResult, lastResultProps);
childProps = lastResultProps;
}
return (React__namespace.createElement(WrappedComponent, tslib.__assign({}, props, childProps)));
}));
};
GraphQL.displayName = graphQLDisplayName;
GraphQL.WrappedComponent = WrappedComponent;
return GraphQL;
}(GraphQLBase));
return hoistNonReactStatics__default(GraphQL, WrappedComponent, {});
};
}
function graphql(document, operationOptions) {
if (operationOptions === void 0) { operationOptions = {}; }
switch (parser.parser(document).type) {
case parser.DocumentType.Mutation:
return withMutation(document, operationOptions);
case parser.DocumentType.Subscription:
return withSubscription(document, operationOptions);
case parser.DocumentType.Query:
default:
return withQuery(document, operationOptions);
}
}
function getDisplayName(WrappedComponent) {
return WrappedComponent.displayName || WrappedComponent.name || "Component";
}
function withApollo(WrappedComponent, operationOptions) {
if (operationOptions === void 0) { operationOptions = {}; }
var withDisplayName = "withApollo(".concat(getDisplayName(WrappedComponent), ")");
var WithApollo = (function (_super) {
tslib.__extends(WithApollo, _super);
function WithApollo(props) {
var _this = _super.call(this, props) || this;
_this.setWrappedInstance = _this.setWrappedInstance.bind(_this);
return _this;
}
WithApollo.prototype.getWrappedInstance = function () {
globals.invariant(operationOptions.withRef, 48);
return this.wrappedInstance;
};
WithApollo.prototype.setWrappedInstance = function (ref) {
this.wrappedInstance = ref;
};
WithApollo.prototype.render = function () {
var _this = this;
return (React__namespace.createElement(context.ApolloConsumer, null, function (client) {
var props = Object.assign({}, _this.props, {
client: client,
ref: operationOptions.withRef ? _this.setWrappedInstance : undefined,
});
return React__namespace.createElement(WrappedComponent, tslib.__assign({}, props));
}));
};
WithApollo.displayName = withDisplayName;
WithApollo.WrappedComponent = WrappedComponent;
return WithApollo;
}(React__namespace.Component));
return hoistNonReactStatics__default(WithApollo, WrappedComponent, {});
}
exports.graphql = graphql;
exports.withApollo = withApollo;
exports.withMutation = withMutation;
exports.withQuery = withQuery;
exports.withSubscription = withSubscription;
//# sourceMappingURL=hoc.cjs.map

View File

@@ -0,0 +1,8 @@
import "../../utilities/globals/index.js";
export { graphql } from "./graphql.js";
export { withQuery } from "./query-hoc.js";
export { withMutation } from "./mutation-hoc.js";
export { withSubscription } from "./subscription-hoc.js";
export { withApollo } from "./withApollo.js";
export * from "./types.js";
//# sourceMappingURL=index.d.ts.map

View File

@@ -0,0 +1,8 @@
import "../../utilities/globals/index.js";
export { graphql } from "./graphql.js";
export { withQuery } from "./query-hoc.js";
export { withMutation } from "./mutation-hoc.js";
export { withSubscription } from "./subscription-hoc.js";
export { withApollo } from "./withApollo.js";
export * from "./types.js";
//# sourceMappingURL=index.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/react/hoc/index.ts"],"names":[],"mappings":"AAAA,OAAO,kCAAkC,CAAC;AAE1C,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAEvC,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAC3C,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AACzD,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAE7C,cAAc,YAAY,CAAC","sourcesContent":["import \"../../utilities/globals/index.js\";\n\nexport { graphql } from \"./graphql.js\";\n\nexport { withQuery } from \"./query-hoc.js\";\nexport { withMutation } from \"./mutation-hoc.js\";\nexport { withSubscription } from \"./subscription-hoc.js\";\nexport { withApollo } from \"./withApollo.js\";\n\nexport * from \"./types.js\";\n"]}

View File

@@ -0,0 +1,12 @@
import type * as ReactTypes from "react";
import type { DocumentNode } from "graphql";
import type { DefaultContext, OperationVariables } from "../../core/types.js";
import type { OperationOption, MutateProps } from "./types.js";
import type { ApolloCache } from "../../core/index.js";
/**
* @deprecated
* Official support for React Apollo higher order components ended in March 2020.
* This library is still included in the `@apollo/client` package, but it no longer receives feature updates or bug fixes.
*/
export declare function withMutation<TProps extends TGraphQLVariables | {} = {}, TData extends Record<string, any> = {}, TGraphQLVariables extends OperationVariables = {}, TChildProps = MutateProps<TData, TGraphQLVariables>, TContext extends Record<string, any> = DefaultContext, TCache extends ApolloCache<any> = ApolloCache<any>>(document: DocumentNode, operationOptions?: OperationOption<TProps, TData, TGraphQLVariables, TChildProps>): (WrappedComponent: ReactTypes.ComponentType<TProps & TChildProps>) => ReactTypes.ComponentClass<TProps>;
//# sourceMappingURL=mutation-hoc.d.ts.map

View File

@@ -0,0 +1,75 @@
import { __assign, __extends, __rest } from "tslib";
import * as React from "rehackt";
import hoistNonReactStatics from "hoist-non-react-statics";
import { parser } from "../parser/index.js";
import { Mutation } from "../components/index.js";
import { defaultMapPropsToOptions, getDisplayName, calculateVariablesFromProps, GraphQLBase, } from "./hoc-utils.js";
/**
* @deprecated
* Official support for React Apollo higher order components ended in March 2020.
* This library is still included in the `@apollo/client` package, but it no longer receives feature updates or bug fixes.
*/
export function withMutation(document, operationOptions) {
if (operationOptions === void 0) { operationOptions = {}; }
// this is memoized so if coming from `graphql` there is nearly no extra cost
var operation = parser(document);
// extract options
var _a = operationOptions.options, options = _a === void 0 ? defaultMapPropsToOptions : _a, _b = operationOptions.alias, alias = _b === void 0 ? "Apollo" : _b;
var mapPropsToOptions = options;
if (typeof mapPropsToOptions !== "function")
mapPropsToOptions = function () {
return options;
};
return function (WrappedComponent) {
var graphQLDisplayName = "".concat(alias, "(").concat(getDisplayName(WrappedComponent), ")");
var GraphQL = /** @class */ (function (_super) {
__extends(GraphQL, _super);
function GraphQL() {
return _super !== null && _super.apply(this, arguments) || this;
}
GraphQL.prototype.render = function () {
var props = this.props;
var opts = mapPropsToOptions(props);
if (operationOptions.withRef) {
this.withRef = true;
props = Object.assign({}, props, {
ref: this.setWrappedInstance,
});
}
if (!opts.variables && operation.variables.length > 0) {
opts.variables = calculateVariablesFromProps(operation, props);
}
return (React.createElement(Mutation, __assign({ ignoreResults: true }, opts, { mutation: document }), function (mutate, _a) {
var _b, _c;
var data = _a.data, r = __rest(_a, ["data"]);
// the HOC's historically hoisted the data from the execution result
// up onto the result since it was passed as a nested prop
// we massage the Mutation component's shape here to replicate that
// this matches the query HoC
var result = Object.assign(r, data || {});
var name = operationOptions.name || "mutate";
var resultName = operationOptions.name ? "".concat(name, "Result") : "result";
var childProps = (_b = {},
_b[name] = mutate,
_b[resultName] = result,
_b);
if (operationOptions.props) {
var newResult = (_c = {},
_c[name] = mutate,
_c[resultName] = result,
_c.ownProps = props,
_c);
childProps = operationOptions.props(newResult);
}
return React.createElement(WrappedComponent, __assign({}, props, childProps));
}));
};
GraphQL.displayName = graphQLDisplayName;
GraphQL.WrappedComponent = WrappedComponent;
return GraphQL;
}(GraphQLBase));
// Make sure we preserve any custom statics on the original component.
return hoistNonReactStatics(GraphQL, WrappedComponent, {});
};
}
//# sourceMappingURL=mutation-hoc.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,8 @@
{
"name": "@apollo/client/react/hoc",
"type": "module",
"main": "hoc.cjs",
"module": "index.js",
"types": "index.d.ts",
"sideEffects": false
}

View File

@@ -0,0 +1,10 @@
import type * as ReactTypes from "react";
import type { DocumentNode } from "graphql";
import type { OperationOption, DataProps } from "./types.js";
/**
* @deprecated
* Official support for React Apollo higher order components ended in March 2020.
* This library is still included in the `@apollo/client` package, but it no longer receives feature updates or bug fixes.
*/
export declare function withQuery<TProps extends TGraphQLVariables | Record<string, any> = Record<string, any>, TData extends object = {}, TGraphQLVariables extends object = {}, TChildProps extends object = DataProps<TData, TGraphQLVariables>>(document: DocumentNode, operationOptions?: OperationOption<TProps, TData, TGraphQLVariables, TChildProps>): (WrappedComponent: ReactTypes.ComponentType<TProps & TChildProps>) => ReactTypes.ComponentClass<TProps>;
//# sourceMappingURL=query-hoc.d.ts.map

View File

@@ -0,0 +1,81 @@
import { __assign, __extends, __rest } from "tslib";
import * as React from "rehackt";
import hoistNonReactStatics from "hoist-non-react-statics";
import { parser } from "../parser/index.js";
import { Query } from "../components/index.js";
import { getDisplayName, GraphQLBase, calculateVariablesFromProps, defaultMapPropsToOptions, defaultMapPropsToSkip, } from "./hoc-utils.js";
/**
* @deprecated
* Official support for React Apollo higher order components ended in March 2020.
* This library is still included in the `@apollo/client` package, but it no longer receives feature updates or bug fixes.
*/
export function withQuery(document, operationOptions) {
if (operationOptions === void 0) { operationOptions = {}; }
// this is memoized so if coming from `graphql` there is nearly no extra cost
var operation = parser(document);
// extract options
var _a = operationOptions.options, options = _a === void 0 ? defaultMapPropsToOptions : _a, _b = operationOptions.skip, skip = _b === void 0 ? defaultMapPropsToSkip : _b, _c = operationOptions.alias, alias = _c === void 0 ? "Apollo" : _c;
var mapPropsToOptions = options;
if (typeof mapPropsToOptions !== "function") {
mapPropsToOptions = function () { return options; };
}
var mapPropsToSkip = skip;
if (typeof mapPropsToSkip !== "function") {
mapPropsToSkip = function () { return skip; };
}
// allow for advanced referential equality checks
var lastResultProps;
return function (WrappedComponent) {
var graphQLDisplayName = "".concat(alias, "(").concat(getDisplayName(WrappedComponent), ")");
var GraphQL = /** @class */ (function (_super) {
__extends(GraphQL, _super);
function GraphQL() {
return _super !== null && _super.apply(this, arguments) || this;
}
GraphQL.prototype.render = function () {
var _this = this;
var props = this.props;
var shouldSkip = mapPropsToSkip(props);
var opts = shouldSkip ? Object.create(null) : __assign({}, mapPropsToOptions(props));
if (!shouldSkip && !opts.variables && operation.variables.length > 0) {
opts.variables = calculateVariablesFromProps(operation, props);
}
return (React.createElement(Query, __assign({}, opts, { displayName: graphQLDisplayName, skip: shouldSkip, query: document }), function (_a) {
var _b, _c;
var _ = _a.client, data = _a.data, r = __rest(_a, ["client", "data"]);
if (operationOptions.withRef) {
_this.withRef = true;
props = Object.assign({}, props, {
ref: _this.setWrappedInstance,
});
}
// if we have skipped, no reason to manage any reshaping
if (shouldSkip) {
return (React.createElement(WrappedComponent, __assign({}, props, {})));
}
// the HOC's historically hoisted the data from the execution result
// up onto the result since it was passed as a nested prop
// we massage the Query components shape here to replicate that
var result = Object.assign(r, data || {});
var name = operationOptions.name || "data";
var childProps = (_b = {}, _b[name] = result, _b);
if (operationOptions.props) {
var newResult = (_c = {},
_c[name] = result,
_c.ownProps = props,
_c);
lastResultProps = operationOptions.props(newResult, lastResultProps);
childProps = lastResultProps;
}
return (React.createElement(WrappedComponent, __assign({}, props, childProps)));
}));
};
GraphQL.displayName = graphQLDisplayName;
GraphQL.WrappedComponent = WrappedComponent;
return GraphQL;
}(GraphQLBase));
// Make sure we preserve any custom statics on the original component.
return hoistNonReactStatics(GraphQL, WrappedComponent, {});
};
}
//# sourceMappingURL=query-hoc.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,10 @@
import type * as ReactTypes from "react";
import type { DocumentNode } from "graphql";
import type { OperationOption, DataProps } from "./types.js";
/**
* @deprecated
* Official support for React Apollo higher order components ended in March 2020.
* This library is still included in the `@apollo/client` package, but it no longer receives feature updates or bug fixes.
*/
export declare function withSubscription<TProps extends TGraphQLVariables | {} = {}, TData extends object = {}, TGraphQLVariables extends object = {}, TChildProps extends object = DataProps<TData, TGraphQLVariables>>(document: DocumentNode, operationOptions?: OperationOption<TProps, TData, TGraphQLVariables, TChildProps>): (WrappedComponent: ReactTypes.ComponentType<TProps & TChildProps>) => ReactTypes.ComponentClass<TProps>;
//# sourceMappingURL=subscription-hoc.d.ts.map

View File

@@ -0,0 +1,90 @@
import { __assign, __extends, __rest } from "tslib";
import * as React from "rehackt";
import hoistNonReactStatics from "hoist-non-react-statics";
import { parser } from "../parser/index.js";
import { Subscription } from "../components/index.js";
import { getDisplayName, GraphQLBase, calculateVariablesFromProps, defaultMapPropsToOptions, defaultMapPropsToSkip, } from "./hoc-utils.js";
/**
* @deprecated
* Official support for React Apollo higher order components ended in March 2020.
* This library is still included in the `@apollo/client` package, but it no longer receives feature updates or bug fixes.
*/
export function withSubscription(document, operationOptions) {
if (operationOptions === void 0) { operationOptions = {}; }
// this is memoized so if coming from `graphql` there is nearly no extra cost
var operation = parser(document);
// extract options
var _a = operationOptions.options, options = _a === void 0 ? defaultMapPropsToOptions : _a, _b = operationOptions.skip, skip = _b === void 0 ? defaultMapPropsToSkip : _b, _c = operationOptions.alias, alias = _c === void 0 ? "Apollo" : _c, shouldResubscribe = operationOptions.shouldResubscribe;
var mapPropsToOptions = options;
if (typeof mapPropsToOptions !== "function")
mapPropsToOptions = function () { return options; };
var mapPropsToSkip = skip;
if (typeof mapPropsToSkip !== "function")
mapPropsToSkip = function () { return skip; };
// allow for advanced referential equality checks
var lastResultProps;
return function (WrappedComponent) {
var graphQLDisplayName = "".concat(alias, "(").concat(getDisplayName(WrappedComponent), ")");
var GraphQL = /** @class */ (function (_super) {
__extends(GraphQL, _super);
function GraphQL(props) {
var _this = _super.call(this, props) || this;
_this.state = { resubscribe: false };
return _this;
}
GraphQL.prototype.updateResubscribe = function (resubscribe) {
this.setState({ resubscribe: resubscribe });
};
GraphQL.prototype.componentDidUpdate = function (prevProps) {
var resubscribe = !!(shouldResubscribe && shouldResubscribe(prevProps, this.props));
if (this.state.resubscribe !== resubscribe) {
this.updateResubscribe(resubscribe);
}
};
GraphQL.prototype.render = function () {
var _this = this;
var props = this.props;
var shouldSkip = mapPropsToSkip(props);
var opts = shouldSkip ? Object.create(null) : mapPropsToOptions(props);
if (!shouldSkip && !opts.variables && operation.variables.length > 0) {
opts.variables = calculateVariablesFromProps(operation, props);
}
return (React.createElement(Subscription, __assign({}, opts, { displayName: graphQLDisplayName, skip: shouldSkip, subscription: document, shouldResubscribe: this.state.resubscribe }), function (_a) {
var _b, _c;
var data = _a.data, r = __rest(_a, ["data"]);
if (operationOptions.withRef) {
_this.withRef = true;
props = Object.assign({}, props, {
ref: _this.setWrappedInstance,
});
}
// if we have skipped, no reason to manage any reshaping
if (shouldSkip) {
return (React.createElement(WrappedComponent, __assign({}, props, {})));
}
// the HOC's historically hoisted the data from the execution result
// up onto the result since it was passed as a nested prop
// we massage the Query components shape here to replicate that
var result = Object.assign(r, data || {});
var name = operationOptions.name || "data";
var childProps = (_b = {}, _b[name] = result, _b);
if (operationOptions.props) {
var newResult = (_c = {},
_c[name] = result,
_c.ownProps = props,
_c);
lastResultProps = operationOptions.props(newResult, lastResultProps);
childProps = lastResultProps;
}
return (React.createElement(WrappedComponent, __assign({}, props, childProps)));
}));
};
GraphQL.displayName = graphQLDisplayName;
GraphQL.WrappedComponent = WrappedComponent;
return GraphQL;
}(GraphQLBase));
// Make sure we preserve any custom statics on the original component.
return hoistNonReactStatics(GraphQL, WrappedComponent, {});
};
}
//# sourceMappingURL=subscription-hoc.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,43 @@
import type { ApolloCache, ApolloClient } from "../../core/index.js";
import type { ApolloError } from "../../errors/index.js";
import type { ApolloQueryResult, OperationVariables, FetchMoreOptions, UpdateQueryOptions, FetchMoreQueryOptions, SubscribeToMoreOptions, DefaultContext } from "../../core/index.js";
import type { MutationFunction, BaseQueryOptions, BaseMutationOptions, MutationResult } from "../types/types.js";
export interface QueryControls<TData = any, TGraphQLVariables = OperationVariables> {
error?: ApolloError;
networkStatus: number;
loading: boolean;
variables: TGraphQLVariables;
fetchMore: (fetchMoreOptions: FetchMoreQueryOptions<TGraphQLVariables, TData> & FetchMoreOptions<TData, TGraphQLVariables>) => Promise<ApolloQueryResult<TData>>;
refetch: (variables?: TGraphQLVariables) => Promise<ApolloQueryResult<TData>>;
startPolling: (pollInterval: number) => void;
stopPolling: () => void;
subscribeToMore: (options: SubscribeToMoreOptions) => () => void;
updateQuery: (mapFn: (previousQueryResult: any, options: UpdateQueryOptions<any>) => any) => void;
}
export type DataValue<TData, TGraphQLVariables = OperationVariables> = QueryControls<TData, TGraphQLVariables> & Partial<TData>;
export interface DataProps<TData, TGraphQLVariables = OperationVariables> {
data: DataValue<TData, TGraphQLVariables>;
}
export interface MutateProps<TData = any, TGraphQLVariables = OperationVariables> {
mutate: MutationFunction<TData, TGraphQLVariables>;
result: MutationResult<TData>;
}
export type ChildProps<TProps = {}, TData = {}, TGraphQLVariables = OperationVariables> = TProps & Partial<DataProps<TData, TGraphQLVariables>> & Partial<MutateProps<TData, TGraphQLVariables>>;
export type ChildDataProps<TProps = {}, TData = {}, TGraphQLVariables = OperationVariables> = TProps & DataProps<TData, TGraphQLVariables>;
export type ChildMutateProps<TProps = {}, TData = {}, TGraphQLVariables = OperationVariables> = TProps & MutateProps<TData, TGraphQLVariables>;
export interface OptionProps<TProps = any, TData = any, TGraphQLVariables = OperationVariables> extends Partial<DataProps<TData, TGraphQLVariables>>, Partial<MutateProps<TData, TGraphQLVariables>> {
ownProps: TProps;
}
export interface OperationOption<TProps, TData, TGraphQLVariables extends OperationVariables = OperationVariables, TChildProps = ChildProps<TProps, TData, TGraphQLVariables>, TContext = DefaultContext, TCache extends ApolloCache<any> = ApolloCache<any>> {
options?: BaseQueryOptions<TGraphQLVariables> | BaseMutationOptions<TData, TGraphQLVariables, TContext, TCache> | ((props: TProps) => BaseQueryOptions<TGraphQLVariables> | BaseMutationOptions<TData, TGraphQLVariables, TContext, TCache>);
props?: (props: OptionProps<TProps, TData, TGraphQLVariables>, lastProps?: TChildProps | void) => TChildProps;
skip?: boolean | ((props: TProps) => boolean);
name?: string;
withRef?: boolean;
shouldResubscribe?: (props: TProps, nextProps: TProps) => boolean;
alias?: string;
}
export type WithApolloClient<P> = P & {
client?: ApolloClient<any>;
};
//# sourceMappingURL=types.d.ts.map

View File

@@ -0,0 +1,2 @@
export {};
//# sourceMappingURL=types.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/react/hoc/types.ts"],"names":[],"mappings":"","sourcesContent":["import type { ApolloCache, ApolloClient } from \"../../core/index.js\";\nimport type { ApolloError } from \"../../errors/index.js\";\nimport type {\n ApolloQueryResult,\n OperationVariables,\n FetchMoreOptions,\n UpdateQueryOptions,\n FetchMoreQueryOptions,\n SubscribeToMoreOptions,\n DefaultContext,\n} from \"../../core/index.js\";\nimport type {\n MutationFunction,\n BaseQueryOptions,\n BaseMutationOptions,\n MutationResult,\n} from \"../types/types.js\";\n\nexport interface QueryControls<\n TData = any,\n TGraphQLVariables = OperationVariables,\n> {\n error?: ApolloError;\n networkStatus: number;\n loading: boolean;\n variables: TGraphQLVariables;\n fetchMore: (\n fetchMoreOptions: FetchMoreQueryOptions<TGraphQLVariables, TData> &\n FetchMoreOptions<TData, TGraphQLVariables>\n ) => Promise<ApolloQueryResult<TData>>;\n refetch: (variables?: TGraphQLVariables) => Promise<ApolloQueryResult<TData>>;\n startPolling: (pollInterval: number) => void;\n stopPolling: () => void;\n subscribeToMore: (options: SubscribeToMoreOptions) => () => void;\n updateQuery: (\n mapFn: (previousQueryResult: any, options: UpdateQueryOptions<any>) => any\n ) => void;\n}\n\nexport type DataValue<\n TData,\n TGraphQLVariables = OperationVariables,\n> = QueryControls<TData, TGraphQLVariables> &\n // data may not yet be loaded\n Partial<TData>;\n\nexport interface DataProps<TData, TGraphQLVariables = OperationVariables> {\n data: DataValue<TData, TGraphQLVariables>;\n}\n\nexport interface MutateProps<\n TData = any,\n TGraphQLVariables = OperationVariables,\n> {\n mutate: MutationFunction<TData, TGraphQLVariables>;\n result: MutationResult<TData>;\n}\n\nexport type ChildProps<\n TProps = {},\n TData = {},\n TGraphQLVariables = OperationVariables,\n> = TProps &\n Partial<DataProps<TData, TGraphQLVariables>> &\n Partial<MutateProps<TData, TGraphQLVariables>>;\n\nexport type ChildDataProps<\n TProps = {},\n TData = {},\n TGraphQLVariables = OperationVariables,\n> = TProps & DataProps<TData, TGraphQLVariables>;\n\nexport type ChildMutateProps<\n TProps = {},\n TData = {},\n TGraphQLVariables = OperationVariables,\n> = TProps & MutateProps<TData, TGraphQLVariables>;\n\nexport interface OptionProps<\n TProps = any,\n TData = any,\n TGraphQLVariables = OperationVariables,\n> extends Partial<DataProps<TData, TGraphQLVariables>>,\n Partial<MutateProps<TData, TGraphQLVariables>> {\n ownProps: TProps;\n}\n\nexport interface OperationOption<\n TProps,\n TData,\n TGraphQLVariables extends OperationVariables = OperationVariables,\n TChildProps = ChildProps<TProps, TData, TGraphQLVariables>,\n TContext = DefaultContext,\n TCache extends ApolloCache<any> = ApolloCache<any>,\n> {\n options?:\n | BaseQueryOptions<TGraphQLVariables>\n | BaseMutationOptions<TData, TGraphQLVariables, TContext, TCache>\n | ((\n props: TProps\n ) =>\n | BaseQueryOptions<TGraphQLVariables>\n | BaseMutationOptions<TData, TGraphQLVariables, TContext, TCache>);\n props?: (\n props: OptionProps<TProps, TData, TGraphQLVariables>,\n lastProps?: TChildProps | void\n ) => TChildProps;\n skip?: boolean | ((props: TProps) => boolean);\n name?: string;\n withRef?: boolean;\n shouldResubscribe?: (props: TProps, nextProps: TProps) => boolean;\n alias?: string;\n}\n\nexport type WithApolloClient<P> = P & { client?: ApolloClient<any> };\n"]}

View File

@@ -0,0 +1,9 @@
import type * as ReactTypes from "react";
import type { OperationOption, WithApolloClient } from "./types.js";
/**
* @deprecated
* Official support for React Apollo higher order components ended in March 2020.
* This library is still included in the `@apollo/client` package, but it no longer receives feature updates or bug fixes.
*/
export declare function withApollo<TProps, TResult = any>(WrappedComponent: ReactTypes.ComponentType<WithApolloClient<Omit<TProps, "client">>>, operationOptions?: OperationOption<TProps, TResult>): ReactTypes.ComponentClass<Omit<TProps, "client">>;
//# sourceMappingURL=withApollo.d.ts.map

View File

@@ -0,0 +1,48 @@
import { __assign, __extends } from "tslib";
import { invariant } from "../../utilities/globals/index.js";
import * as React from "rehackt";
import hoistNonReactStatics from "hoist-non-react-statics";
import { ApolloConsumer } from "../context/index.js";
function getDisplayName(WrappedComponent) {
return WrappedComponent.displayName || WrappedComponent.name || "Component";
}
/**
* @deprecated
* Official support for React Apollo higher order components ended in March 2020.
* This library is still included in the `@apollo/client` package, but it no longer receives feature updates or bug fixes.
*/
export function withApollo(WrappedComponent, operationOptions) {
if (operationOptions === void 0) { operationOptions = {}; }
var withDisplayName = "withApollo(".concat(getDisplayName(WrappedComponent), ")");
var WithApollo = /** @class */ (function (_super) {
__extends(WithApollo, _super);
function WithApollo(props) {
var _this = _super.call(this, props) || this;
_this.setWrappedInstance = _this.setWrappedInstance.bind(_this);
return _this;
}
WithApollo.prototype.getWrappedInstance = function () {
invariant(operationOptions.withRef, 48);
return this.wrappedInstance;
};
WithApollo.prototype.setWrappedInstance = function (ref) {
this.wrappedInstance = ref;
};
WithApollo.prototype.render = function () {
var _this = this;
return (React.createElement(ApolloConsumer, null, function (client) {
var props = Object.assign({}, _this.props, {
client: client,
ref: operationOptions.withRef ? _this.setWrappedInstance : undefined,
});
return React.createElement(WrappedComponent, __assign({}, props));
}));
};
WithApollo.displayName = withDisplayName;
WithApollo.WrappedComponent = WrappedComponent;
return WithApollo;
}(React.Component));
// Make sure we preserve any custom statics on the original component.
return hoistNonReactStatics(WithApollo, WrappedComponent, {});
}
//# sourceMappingURL=withApollo.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"withApollo.js","sourceRoot":"","sources":["../../../src/react/hoc/withApollo.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,kCAAkC,CAAC;AAC7D,OAAO,KAAK,KAAK,MAAM,SAAS,CAAC;AAEjC,OAAO,oBAAoB,MAAM,yBAAyB,CAAC;AAE3D,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAGrD,SAAS,cAAc,CAAI,gBAA6C;IACtE,OAAO,gBAAgB,CAAC,WAAW,IAAI,gBAAgB,CAAC,IAAI,IAAI,WAAW,CAAC;AAC9E,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,UAAU,CACxB,gBAEC,EACD,gBAAuD;IAAvD,iCAAA,EAAA,qBAAuD;IAEvD,IAAM,eAAe,GAAG,qBAAc,cAAc,CAAC,gBAAgB,CAAC,MAAG,CAAC;IAE1E;QAAyB,8BAAuC;QAO9D,oBAAY,KAA6B;YACvC,YAAA,MAAK,YAAC,KAAK,CAAC,SAAC;YACb,KAAI,CAAC,kBAAkB,GAAG,KAAI,CAAC,kBAAkB,CAAC,IAAI,CAAC,KAAI,CAAC,CAAC;;QAC/D,CAAC;QAED,uCAAkB,GAAlB;YACE,SAAS,CACP,gBAAgB,CAAC,OAAO,EACxB,sDAAsD;gBACpD,kCAAkC,CACrC,CAAC;YAEF,OAAO,IAAI,CAAC,eAAe,CAAC;QAC9B,CAAC;QAED,uCAAkB,GAAlB,UACE,GAAuD;YAEvD,IAAI,CAAC,eAAe,GAAG,GAAG,CAAC;QAC7B,CAAC;QAED,2BAAM,GAAN;YAAA,iBAaC;YAZC,OAAO,CACL,oBAAC,cAAc,QACZ,UAAC,MAAM;gBACN,IAAM,KAAK,GAAG,MAAM,CAAC,MAAM,CAAC,EAAE,EAAE,KAAI,CAAC,KAAK,EAAE;oBAC1C,MAAM,QAAA;oBACN,GAAG,EACD,gBAAgB,CAAC,OAAO,CAAC,CAAC,CAAC,KAAI,CAAC,kBAAkB,CAAC,CAAC,CAAC,SAAS;iBACjE,CAAC,CAAC;gBACH,OAAO,oBAAC,gBAAgB,eAAK,KAAK,EAAI,CAAC;YACzC,CAAC,CACc,CAClB,CAAC;QACJ,CAAC;QAxCM,sBAAW,GAAG,eAAe,CAAC;QAC9B,2BAAgB,GAAG,gBAAgB,CAAC;QAwC7C,iBAAC;KAAA,AA1CD,CAAyB,KAAK,CAAC,SAAS,GA0CvC;IAED,sEAAsE;IACtE,OAAO,oBAAoB,CAAC,UAAU,EAAE,gBAAgB,EAAE,EAAE,CAAC,CAAC;AAChE,CAAC","sourcesContent":["import { invariant } from \"../../utilities/globals/index.js\";\nimport * as React from \"rehackt\";\nimport type * as ReactTypes from \"react\";\nimport hoistNonReactStatics from \"hoist-non-react-statics\";\n\nimport { ApolloConsumer } from \"../context/index.js\";\nimport type { OperationOption, WithApolloClient } from \"./types.js\";\n\nfunction getDisplayName<P>(WrappedComponent: ReactTypes.ComponentType<P>) {\n return WrappedComponent.displayName || WrappedComponent.name || \"Component\";\n}\n\n/**\n * @deprecated\n * Official support for React Apollo higher order components ended in March 2020.\n * This library is still included in the `@apollo/client` package, but it no longer receives feature updates or bug fixes.\n */\nexport function withApollo<TProps, TResult = any>(\n WrappedComponent: ReactTypes.ComponentType<\n WithApolloClient<Omit<TProps, \"client\">>\n >,\n operationOptions: OperationOption<TProps, TResult> = {}\n): ReactTypes.ComponentClass<Omit<TProps, \"client\">> {\n const withDisplayName = `withApollo(${getDisplayName(WrappedComponent)})`;\n\n class WithApollo extends React.Component<Omit<TProps, \"client\">> {\n static displayName = withDisplayName;\n static WrappedComponent = WrappedComponent;\n\n // wrapped instance\n private wrappedInstance: any;\n\n constructor(props: Omit<TProps, \"client\">) {\n super(props);\n this.setWrappedInstance = this.setWrappedInstance.bind(this);\n }\n\n getWrappedInstance() {\n invariant(\n operationOptions.withRef,\n `To access the wrapped instance, you need to specify ` +\n `{ withRef: true } in the options`\n );\n\n return this.wrappedInstance;\n }\n\n setWrappedInstance(\n ref: ReactTypes.ComponentType<WithApolloClient<TProps>>\n ) {\n this.wrappedInstance = ref;\n }\n\n render() {\n return (\n <ApolloConsumer>\n {(client) => {\n const props = Object.assign({}, this.props, {\n client,\n ref:\n operationOptions.withRef ? this.setWrappedInstance : undefined,\n });\n return <WrappedComponent {...props} />;\n }}\n </ApolloConsumer>\n );\n }\n }\n\n // Make sure we preserve any custom statics on the original component.\n return hoistNonReactStatics(WithApollo, WrappedComponent, {});\n}\n"]}