By using a graph diagram named a magnetic graph diagram, we construct a polynomial invariant for knots and links. We show that it is a generalization of both the HOMFLY and the Kauffman polynomials.