In this paper, we present a systematic way based on the nonbinary graph state of constructing good nonbinary quantum codes, both additive and nonadditive, for systems with integer dimensions. With a computer search, which results in many interesting codes including some nonadditive codes meeting the Singleton bounds, we are able to construct explicitly four families of optimal codes, namely, , , , and for any odd dimension and a family of nonadditive codes for arbitrary . In the case of composite numbers as dimensions, we also construct a family of stabilizer codes for odd , whose coding subspace is not of a dimension that is a power of the dimension of the physical subsystem.