@apollo/client#QueryResult TypeScript Examples

The following examples show how to use @apollo/client#QueryResult. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Apollo.ts    From graphql-ts-client with MIT License 6 votes vote down vote up
export function useTypedQuery<
    TData extends object,
    TVariables extends object
>( 
    fetcher: Fetcher<"Query", TData, TVariables>,
    options?: QueryHookOptions<TData, TVariables> & {
        readonly operationName?: string,
		readonly registerDependencies?: boolean | { readonly fieldDependencies: readonly Fetcher<string, object, object>[] }
	}
) : QueryResult<TData, TVariables> {

    const body = requestBody(fetcher);

	const [operationName, request] = useMemo<[string, DocumentNode]>(() => {
		const operationName = options?.operationName ?? `query_${util.toMd5(body)}`;
		return [operationName, gql`query ${operationName}${body}`];
	}, [body, options?.operationName]);

	const [dependencyManager, config] = useContext(dependencyManagerContext);
	const register = options?.registerDependencies !== undefined ? !!options.registerDependencies : config?.defaultRegisterDependencies ?? false;
	if (register && dependencyManager === undefined) {
		throw new Error("The property 'registerDependencies' of options requires <DependencyManagerProvider/>");
	}
	useEffect(() => {
		if (register) {
			dependencyManager!.register(
				operationName, 
				fetcher, 
				typeof options?.registerDependencies === 'object' ? options?.registerDependencies?.fieldDependencies : undefined
			);
			return () => { dependencyManager!.unregister(operationName); };
		}// eslint-disable-next-line
	}, [register, dependencyManager, operationName, options?.registerDependencies, request]); // Eslint disable is required, becasue 'fetcher' is replaced by 'request' here.
	const response = useQuery<TData, TVariables>(request, options);
	const responseData = response.data;
	const newResponseData = useMemo(() => util.exceptNullValues(responseData), [responseData]);
	return newResponseData === responseData ? response : { ...response, data: newResponseData };
}
Example #2
Source File: useLesson.tsx    From nyxo-app with GNU General Public License v3.0 6 votes vote down vote up
useLesson = (
  slug?: string
): QueryResult<LessonData, LessonVariables> => {
  const locale = I18n.locale === 'en' ? 'en-US' : 'fi-FI'

  return useQuery<LessonData, LessonVariables>(GET_LESSON, {
    variables: { language: locale, slug }
  })
}
Example #3
Source File: useQuery.ts    From lexicon with MIT License 6 votes vote down vote up
export function useQuery<TData, TVariables = OperationVariables>(
  query: DocumentNode,
  options?: QueryHookOptions<TData, TVariables>,
  errorAlert: ErrorAlertOptionType = 'SHOW_ALERT',
): QueryResult<TData, TVariables> {
  const onErrorDefault = (error: ApolloError) => {
    errorHandlerAlert(error);
  };

  const { fetchPolicy = 'cache-and-network', ...others } = options ?? {};

  const {
    onError = errorAlert === 'SHOW_ALERT' ? onErrorDefault : undefined,
    nextFetchPolicy = fetchPolicy === 'cache-and-network'
      ? 'cache-first'
      : undefined,
    notifyOnNetworkStatusChange = fetchPolicy === 'network-only',
    ...otherOptions
  } = others;

  let queryResult = useQueryBase<TData, TVariables>(query, {
    fetchPolicy,
    nextFetchPolicy,
    notifyOnNetworkStatusChange,
    onError,
    ...otherOptions,
  });

  return queryResult;
}
Example #4
Source File: useWeek.ts    From nyxo-app with GNU General Public License v3.0 5 votes vote down vote up
useWeek = (slug: string): QueryResult<WeekData, WeekVariables> => {
  const locale = I18n.locale === 'en' ? 'en-US' : 'fi-FI'

  return useQuery<WeekData, WeekVariables>(GET_WEEK, {
    variables: { language: locale, slug }
  })
}
Example #5
Source File: useWeeks.ts    From nyxo-app with GNU General Public License v3.0 5 votes vote down vote up
useWeeks = (): QueryResult<WeeksData, WeeksVariables> => {
  const locale = I18n.locale === 'en' ? 'en-US' : 'fi-FI'

  return useQuery<WeeksData, WeeksVariables>(GET_WEEKS, {
    variables: { language: locale }
  })
}
Example #6
Source File: admin.profile.query.ts    From ui with GNU Affero General Public License v3.0 5 votes vote down vote up
useProfileQuery = (
  options?: QueryHookOptions<Data, unknown>
): QueryResult<Data, unknown> => useQuery<Data, unknown>(QUERY, options)
Example #7
Source File: form.pager.query.ts    From ui with GNU Affero General Public License v3.0 5 votes vote down vote up
useFormPagerQuery = (
  options?: QueryHookOptions<Data, Variables>
): QueryResult<Data, Variables> => useQuery<Data, Variables>(QUERY, options)
Example #8
Source File: form.public.query.ts    From ui with GNU Affero General Public License v3.0 5 votes vote down vote up
useFormPublicQuery = (
  options?: QueryHookOptions<Data, Variables>
): QueryResult<Data, Variables> => useQuery<Data, Variables>(QUERY, options)
Example #9
Source File: form.query.ts    From ui with GNU Affero General Public License v3.0 5 votes vote down vote up
useFormQuery = (
  options?: QueryHookOptions<Data, Variables>
): QueryResult<Data, Variables> => useQuery<Data, Variables>(QUERY, options)
Example #10
Source File: me.query.ts    From ui with GNU Affero General Public License v3.0 5 votes vote down vote up
useMeQuery = (options?: QueryHookOptions<Data, unknown>): QueryResult<Data, unknown> =>
  useQuery<Data, unknown>(QUERY, options)
Example #11
Source File: settings.query.ts    From ui with GNU Affero General Public License v3.0 5 votes vote down vote up
useSettingsQuery = (
  options?: QueryHookOptions<Data, unknown>
): QueryResult<Data, unknown> => useQuery<Data, unknown>(QUERY, options)
Example #12
Source File: status.query.ts    From ui with GNU Affero General Public License v3.0 5 votes vote down vote up
useStatusQuery = (
  options?: QueryHookOptions<Data, unknown>
): QueryResult<Data, unknown> => useQuery<Data, unknown>(QUERY, options)
Example #13
Source File: submission.pager.query.ts    From ui with GNU Affero General Public License v3.0 5 votes vote down vote up
useSubmissionPagerQuery = (
  options?: QueryHookOptions<Data, Variables>
): QueryResult<Data, Variables> => useQuery<Data, Variables>(QUERY, options)
Example #14
Source File: user.pager.query.ts    From ui with GNU Affero General Public License v3.0 5 votes vote down vote up
useUserPagerQuery = (
  options?: QueryHookOptions<Data, Variables>
): QueryResult<Data, Variables> => useQuery<Data, Variables>(QUERY, options)