We consider a core language for graph queries. These queries, which may transform graphs to graphs, are seen as formulas to be solved with respect to graph databases. For this purpose, we first define a graph query algebra where some operations over graphs and sets of graph homomorphisms are specified. Then, the notion of pattern is introduced to represent a kind of recursively defined formula over graphs. The syntax and formal semantics of patterns are provided. Afterwards, we propose a new sound and complete procedure to solve patterns. This procedure, which is based on a set of rewriting rules, is terminating and develops only one needed derivation per pattern to be solved. Our procedure is generic in the sense that it can be adapted to different kinds of graph queries provided that the notions of graph and graph homomorphism are well defined.