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,EAAE;wBAC1B,IAAM,SAAS;4BAKb,GAAC,IAAI,IAAG,MAAM;4BACd,WAAQ,GAAE,KAAe;+BAC1B,CAAC;wBACF,eAAe,GAAG,gBAAgB,CAAC,KAAK,CACtC,SAAS,EACT,eAAe,CAChB,CAAC;wBACF,UAAU,GAAG,eAAe,CAAC;qBAC9B;oBAED,OAAO,CACL,oBAAC,gBAAgB,eACV,KAAgB,EAChB,UAA0B,EAC/B,CACH,CAAC;gBACJ,CAAC,CACK,CACT,CAAC;YACJ,CAAC;YAtEM,mBAAW,GAAG,kBAAkB,CAAC;YACjC,wBAAgB,GAAG,gBAAgB,CAAC;YAsE7C,cAAC;SAAA,AAxED,CAAsB,WAAW,GAwEhC;QAGD,OAAO,oBAAoB,CAAC,OAAO,EAAE,gBAAgB,EAAE,EAAE,CAAC,CAAC;IAC7D,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import * as React from 'react';\nimport { DocumentNode } from 'graphql';\nimport hoistNonReactStatics from 'hoist-non-react-statics';\n\nimport { parser } from '../parser';\nimport { BaseQueryOptions } from '../types/types';\nimport { Query } from '../components';\nimport {\n getDisplayName,\n GraphQLBase,\n calculateVariablesFromProps,\n defaultMapPropsToOptions,\n defaultMapPropsToSkip\n} from './hoc-utils';\nimport { OperationOption, OptionProps, DataProps } from './types';\n\nexport function withQuery<\n TProps extends TGraphQLVariables | Record<string, any> = Record<string, any>,\n TData extends object = {},\n TGraphQLVariables extends object = {},\n TChildProps extends object = DataProps<TData, TGraphQLVariables>\n>(\n document: DocumentNode,\n operationOptions: OperationOption<\n TProps,\n TData,\n TGraphQLVariables,\n TChildProps\n > = {}\n) {\n // this is memoized so if coming from `graphql` there is nearly no extra cost\n const operation = parser(document);\n // extract options\n const {\n options = defaultMapPropsToOptions,\n skip = defaultMapPropsToSkip,\n alias = 'Apollo'\n } = operationOptions;\n\n let mapPropsToOptions = options as (props: any) => BaseQueryOptions;\n if (typeof mapPropsToOptions !== 'function') {\n mapPropsToOptions = () => options as BaseQueryOptions;\n }\n\n let mapPropsToSkip = skip as (props: any) => boolean;\n if (typeof mapPropsToSkip !== 'function') {\n mapPropsToSkip = () => skip as any;\n }\n\n // allow for advanced referential equality checks\n let lastResultProps: TChildProps | void;\n return (\n WrappedComponent: React.ComponentType<TProps & TChildProps>\n ): React.ComponentClass<TProps> => {\n const graphQLDisplayName = `${alias}(${getDisplayName(WrappedComponent)})`;\n class GraphQL extends GraphQLBase<TProps, TChildProps> {\n static displayName = graphQLDisplayName;\n static WrappedComponent = WrappedComponent;\n\n render() {\n let props = this.props;\n const shouldSkip = mapPropsToSkip(props);\n const opts = shouldSkip\n ? Object.create(null)\n : { ...mapPropsToOptions(props) };\n\n if (!shouldSkip && !opts.variables && operation.variables.length > 0) {\n opts.variables = calculateVariablesFromProps(operation, props);\n }\n\n return (\n <Query\n {...opts}\n displayName={graphQLDisplayName}\n skip={shouldSkip}\n query={document}\n >\n {({ client: _, data, ...r }: any) => {\n if (operationOptions.withRef) {\n this.withRef = true;\n props = Object.assign({}, props, {\n ref: this.setWrappedInstance\n });\n }\n\n // if we have skipped, no reason to manage any reshaping\n if (shouldSkip) {\n return (\n <WrappedComponent\n {...(props as TProps)}\n {...({} as TChildProps)}\n />\n );\n }\n\n // the HOC's historically hoisted the data from the execution result\n // up onto the result since it was passed as a nested prop\n // we massage the Query components shape here to replicate that\n const result = Object.assign(r, data || {});\n const name = operationOptions.name || 'data';\n let childProps = { [name]: result };\n if (operationOptions.props) {\n const newResult: OptionProps<\n TProps,\n TData,\n TGraphQLVariables\n > = {\n [name]: result,\n ownProps: props as TProps\n };\n lastResultProps = operationOptions.props(\n newResult,\n lastResultProps\n );\n childProps = lastResultProps;\n }\n\n return (\n <WrappedComponent\n {...(props as TProps)}\n {...(childProps as TChildProps)}\n />\n );\n }}\n </Query>\n );\n }\n }\n\n // Make sure we preserve any custom statics on the original component.\n return hoistNonReactStatics(GraphQL, WrappedComponent, {});\n };\n}\n"]}
\No newline at end of file