import { gql, useQuery } from '@apollo/client';
import { useGraphQLData } from './helper';

export const QUERY_IS_AUTHORIZED = gql`
  query isAuthorized($action: String!, $object: AuthorizationObject!) {
    isAuthorized(action: $action, object: $object)
  }
`;

export function useIsAuthorizedQuery(options = {}) {
  return useQuery(QUERY_IS_AUTHORIZED, options);
}

export function useIsAuthorized(options = {}, defaultValue = false) {
  const { data, loading, error } = useGraphQLData<boolean>(
    QUERY_IS_AUTHORIZED, 'isAuthorized', defaultValue, options
  );
  return { isAuthorized: data, loading, error };
}