Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function process(): Promise | string {
// Always re-render from the rootElement, even though it might seem
// better to render the children of the component responsible for the
// promise, because it is not possible to reconstruct the full context
// of the original rendering (including all unknown context provider
// elements) for a subtree of the original component tree.
const ApolloContext = getApolloContext();
const html = renderFunction(
React.createElement(
ApolloContext.Provider,
{ value: { ...context, renderPromises } },
tree
)
);
return renderPromises.hasPromises()
? renderPromises.consumeAndAwaitPromises().then(process)
: html;
}
export function withQuery<
TProps extends TGraphQLVariables | {} = {},
TData = {},
TGraphQLVariables = {},
TChildProps = DataProps
>(
document: DocumentNode,
operationOptions: OperationOption<
TProps,
TData,
TGraphQLVariables,
TChildProps
> = {}
) {
// this is memoized so if coming from `graphql` there is nearly no extra cost
const operation = parser(document);
// extract options
const {
options = defaultMapPropsToOptions,
skip = defaultMapPropsToSkip,
alias = 'Apollo'
} = operationOptions;
let mapPropsToOptions = options as (props: any) => BaseQueryOptions;
if (typeof mapPropsToOptions !== 'function') {
mapPropsToOptions = () => options as BaseQueryOptions;
}
let mapPropsToSkip = skip as (props: any) => boolean;
if (typeof mapPropsToSkip !== 'function') {
mapPropsToSkip = () => skip as any;
}
export function withMutation<
TProps extends TGraphQLVariables | {} = {},
TData = {},
TGraphQLVariables = {},
TChildProps = MutateProps
>(
document: DocumentNode,
operationOptions: OperationOption<
TProps,
TData,
TGraphQLVariables,
TChildProps
> = {}
) {
// this is memoized so if coming from `graphql` there is nearly no extra cost
const operation = parser(document);
// extract options
const {
options = defaultMapPropsToOptions,
alias = 'Apollo'
} = operationOptions;
let mapPropsToOptions = options as (props: any) => BaseMutationOptions;
if (typeof mapPropsToOptions !== 'function')
mapPropsToOptions = () => options as BaseMutationOptions;
return (
WrappedComponent: React.ComponentType
): React.ComponentClass => {
const graphQLDisplayName = `${alias}(${getDisplayName(WrappedComponent)})`;
class GraphQL extends GraphQLBase {
export function withSubscription<
TProps extends TGraphQLVariables | {} = {},
TData = {},
TGraphQLVariables = {},
TChildProps = DataProps
>(
document: DocumentNode,
operationOptions: OperationOption<
TProps,
TData,
TGraphQLVariables,
TChildProps
> = {}
) {
// this is memoized so if coming from `graphql` there is nearly no extra cost
const operation = parser(document);
// extract options
const {
options = defaultMapPropsToOptions,
skip = defaultMapPropsToSkip,
alias = 'Apollo',
shouldResubscribe
} = operationOptions;
let mapPropsToOptions = options as (props: any) => BaseQueryOptions;
if (typeof mapPropsToOptions !== 'function')
mapPropsToOptions = () => options as BaseQueryOptions;
let mapPropsToSkip = skip as (props: any) => boolean;
if (typeof mapPropsToSkip !== 'function') mapPropsToSkip = () => skip as any;
// allow for advanced referential equality checks
private prepareObservableQueryOptions() {
const options = this.getOptions();
this.verifyDocumentType(options.query, DocumentType.Query);
const displayName = options.displayName || 'Query';
// Set the fetchPolicy to cache-first for network-only and cache-and-network
// fetches for server side renders.
if (
this.context &&
this.context.renderPromises &&
(options.fetchPolicy === 'network-only' ||
options.fetchPolicy === 'cache-and-network')
) {
options.fetchPolicy = 'cache-first';
}
return {
...options,
displayName,
export function getMarkupFromTree({
tree,
context = {},
// The rendering function is configurable! We use renderToStaticMarkup as
// the default, because it's a little less expensive than renderToString,
// and legacy usage of getDataFromTree ignores the return value anyway.
renderFunction = require('react-dom/server').renderToStaticMarkup
}: GetMarkupFromTreeOptions): Promise {
const renderPromises = new RenderPromises();
function process(): Promise | string {
// Always re-render from the rootElement, even though it might seem
// better to render the children of the component responsible for the
// promise, because it is not possible to reconstruct the full context
// of the original rendering (including all unknown context provider
// elements) for a subtree of the original component tree.
const ApolloContext = getApolloContext();
const html = renderFunction(
React.createElement(
ApolloContext.Provider,
{ value: { ...context, renderPromises } },
tree
)
);
Mutation.prototype.render = function () {
var children = this.props.children;
var _a = this.state, loading = _a.loading, data = _a.data, error = _a.error, called = _a.called;
var result = {
called: called,
loading: loading,
data: data,
error: error,
client: this.currentClient(),
};
return children(this.runMutation, result);
};
Mutation.prototype.currentClient = function () {
return getClient(this.props, this.context);
};
Mutation.contextType = reactCommon.getApolloContext();
Mutation.propTypes = {
mutation: PropTypes.object.isRequired,
variables: PropTypes.object,
optimisticResponse: PropTypes.object,
refetchQueries: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.string, PropTypes.object])),
PropTypes.func,
]),
awaitRefetchQueries: PropTypes.bool,
update: PropTypes.func,
children: PropTypes.func.isRequired,
onCompleted: PropTypes.func,
onError: PropTypes.func,
fetchPolicy: PropTypes.string,
};
return Mutation;
Query.prototype.render = function () {
var _this = this;
var ApolloContext = reactCommon.getApolloContext();
return (React.createElement(ApolloContext.Consumer, null, function (context) {
return _this.renderData(context);
}));
};
Query.prototype.fetchData = function (client, context) {
Query.prototype.render = function () {
var _this = this;
var ApolloContext = getApolloContext();
return (React.createElement(ApolloContext.Consumer, null, function (context) {
return _this.renderData(context);
}));
};
Query.prototype.fetchData = function (client, context) {
function process() {
var ApolloContext = getApolloContext();
var html = renderFunction(React.createElement(ApolloContext.Provider, { value: __assign({}, context, { renderPromises: renderPromises }) }, tree));
return renderPromises.hasPromises()
? renderPromises.consumeAndAwaitPromises().then(process)
: html;
}
return Promise.resolve().then(process);