{"version":3,"file":"query.mjs","sources":["../../../../../src/query/query.ts"],"sourcesContent":["import { type EventCallable, type Store, attach, createEvent, createStore, sample } from \"effector\"\n\nimport {\n  type ApolloClient,\n  type ApolloError,\n  type DefaultContext,\n  type DocumentNode,\n  type MaybeMasked,\n  type OperationVariables,\n  type QueryOptions,\n  type TypedDocumentNode,\n} from \"@apollo/client\"\n\nimport { operationName } from \"../lib/name\"\nimport { type Optional, optional } from \"../lib/optional\"\nimport { storify } from \"../lib/storify\"\nimport {\n  type ExecutionParams,\n  type RemoteOperation,\n  type RemoteOperationInternals,\n  createRemoteOperation,\n} from \"../remote_operation\"\n\nimport { type QueryMeta, createQueryController } from \"./controller\"\n\ninterface CreateQueryOptions<Data, Variables> {\n  /** Your {@link ApolloClient} instance that'll be used for making the query. */\n  client: ApolloClient<unknown> | Store<ApolloClient<unknown>>\n  /**\n   * A GraphQL Document with a single `query` for your operation.\n   * It's passed directly to Apollo with no modifications.\n   */\n  document: DocumentNode | TypedDocumentNode<Data, Variables>\n\n  /** Context passed to your Apollo Link. */\n  context?: DefaultContext | Store<DefaultContext>\n\n  /** The name of your query. Will be derived from `document` if abscent. */\n  name?: string\n}\n\nexport interface QueryInternals<Data, Variables>\n  extends RemoteOperationInternals<MaybeMasked<Data>, Variables, QueryMeta> {\n  push: EventCallable<MaybeMasked<Data> | null>\n}\n\nexport interface Query<Data, Variables extends OperationVariables>\n  extends RemoteOperation<MaybeMasked<Data>, Variables, QueryMeta> {\n  /** Start fetching data unconditionally. */\n  start: EventCallable<Optional<Variables>>\n  /** Start fetching data if it is absent or stale. */\n  refresh: EventCallable<Optional<Variables>>\n  /** Reset query state. Clears `$data`, `$error` and `$status` to their initial values.  */\n  reset: EventCallable<void>\n\n  /**\n   * Latest data received from your `Query`.\n   *\n   * If there was an error during fetching, or if there was no request yet,\n   * this store will be `null`.\n   */\n  $data: Store<MaybeMasked<Data> | null>\n  /**\n   * Latest {@link Query} error.\n   *\n   * If the data has been successfully fetched, or if there was no request yet,\n   * the store will be `null`.\n   */\n  $error: Store<ApolloError | null>\n\n  meta: {\n    /** The name of this query. */\n    name: string\n    /** The client this query will use to make requests. */\n    client: Store<ApolloClient<unknown>>\n    /** The document that contains your query and will be used to request data. */\n    document: TypedDocumentNode<Data, Variables>\n  }\n\n  /**\n   * Internal tools, useful for testing.\n   */\n  __: QueryInternals<Data, Variables>\n}\n\n/**\n * A factory to create a GraphQL Query\n *\n * @param config Query configuration\n * @returns Query instance\n */\nexport function createQuery<Data, Variables extends OperationVariables = OperationVariables>({\n  client,\n  document,\n  context,\n\n  name = operationName(document) || \"unknown\",\n}: CreateQueryOptions<Data, Variables>): Query<Data, Variables> {\n  type ResolvedData = MaybeMasked<Data>\n\n  const options: QueryOptions<Variables, Data> = {\n    query: document,\n    returnPartialData: false,\n    canonizeResults: true,\n  }\n\n  const push = createEvent<ResolvedData | null>({ name: `${name}.push` })\n\n  const $client = storify(client, { sid: `apollo.${name}.$client`, name: `${name}.client` })\n  const $context = storify(context, { sid: `apollo.${name}.$context`, name: `${name}.context` })\n\n  const $data = createStore<ResolvedData | null>(null, {\n    name: `${name}.data`,\n    sid: `apollo.${name}.$data`,\n    skipVoid: false,\n  })\n\n  const $error = createStore<ApolloError | null>(null, {\n    name: `${name}.error`,\n    sid: `apollo.${name}.$error`,\n    skipVoid: false,\n  })\n\n  const handler = attach({\n    source: { client: $client, context: $context },\n    effect: ({ client, context }, { variables, meta }: ExecutionParams<Variables, QueryMeta>) => {\n      const fetchPolicy = meta.force ? \"network-only\" : \"cache-first\"\n\n      return client.query({ ...options, context, variables, fetchPolicy }).then(({ data }) => data)\n    },\n  })\n\n  const operation = createRemoteOperation<ResolvedData, Variables, QueryMeta>({ name, handler })\n\n  const controller = createQueryController({ operation, name })\n\n  sample({\n    clock: operation.finished.success,\n    fn: ({ data }) => data,\n    target: [$data, $error.reinit],\n  })\n\n  sample({ clock: push, target: $data })\n\n  sample({\n    clock: operation.finished.failure,\n    fn: ({ error }) => error,\n    target: [$error, $data.reinit],\n  })\n\n  sample({\n    clock: operation.reset,\n    target: [$data.reinit, $error.reinit],\n  })\n\n  return {\n    ...operation,\n\n    start: optional(controller.start),\n    refresh: optional(controller.refresh),\n\n    $data,\n    $error,\n\n    meta: { name, client: $client, document },\n    __: { ...operation.__, push },\n  }\n}\n"],"names":["client","context"],"mappings":";;;;;;;AA2FO,SAAS,YAA6E;AAAA,EAC3F;AAAA,EACA;AAAA,EACA;AAAA,EAEA,OAAO,cAAc,QAAQ,KAAK;AACpC,GAAgE;AAG9D,QAAM,UAAyC;AAAA,IAC7C,OAAO;AAAA,IACP,mBAAmB;AAAA,IACnB,iBAAiB;AAAA,EACnB;AAEA,QAAM,OAAO,YAAiC,EAAE,MAAM,GAAG,IAAI,SAAS;AAEtE,QAAM,UAAU,QAAQ,QAAQ,EAAE,KAAK,UAAU,IAAI,YAAY,MAAM,GAAG,IAAI,UAAA,CAAW;AACzF,QAAM,WAAW,QAAQ,SAAS,EAAE,KAAK,UAAU,IAAI,aAAa,MAAM,GAAG,IAAI,WAAA,CAAY;AAEvF,QAAA,QAAQ,YAAiC,MAAM;AAAA,IACnD,MAAM,GAAG,IAAI;AAAA,IACb,KAAK,UAAU,IAAI;AAAA,IACnB,UAAU;AAAA,EAAA,CACX;AAEK,QAAA,SAAS,YAAgC,MAAM;AAAA,IACnD,MAAM,GAAG,IAAI;AAAA,IACb,KAAK,UAAU,IAAI;AAAA,IACnB,UAAU;AAAA,EAAA,CACX;AAED,QAAM,UAAU,OAAO;AAAA,IACrB,QAAQ,EAAE,QAAQ,SAAS,SAAS,SAAS;AAAA,IAC7C,QAAQ,CAAC,EAAE,QAAAA,SAAQ,SAAAC,YAAW,EAAE,WAAW,WAAkD;AACrF,YAAA,cAAc,KAAK,QAAQ,iBAAiB;AAElD,aAAOD,QAAO,MAAM,EAAE,GAAG,SAAS,SAAAC,UAAS,WAAW,YAAY,CAAC,EAAE,KAAK,CAAC,EAAE,KAAA,MAAW,IAAI;AAAA,IAAA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