<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def query(self, query, variables=None, timeout=None, metadata=None, credentials=None):
 """Adds a query operation to the transaction."""

        new_metadata = self._dg.add_login_metadata(metadata)
        req = self._common_query(query, variables=variables)
        try:
            res = self._dc.query(req, timeout=timeout,
                                 metadata=new_metadata,
                                 credentials=credentials)
        except Exception as error:
            if util.is_jwt_expired(error):
                self._dg.retry_login()
                new_metadata = self._dg.add_login_metadata(metadata)
                res = self._dc.query(req, timeout=timeout,
                                     metadata=new_metadata,
                                     credentials=credentials)
            else:
                raise error

        self.merge_context(res.txn)
        return res