I wrote the whole paper, but did only a very small part of the research. PhD student wants to be first author, but didn't write. Is this okay?
Unfortunately, the time to decide these kinds of issues is well in advance of publication, and preferably in advance of doing the work and writing the paper! 

The answer to your question is "it depends". Authorship on academic papers isn't so much about writing text, but about scholarship. Writing text is a part of that, but scholarship is much bigger than simply writing up results. In the guidelines I link below, scholarship is defined as:

Scholarship: Contributes significantly to the conception, design, execution, and/or analysis and interpretation of data.

If your primary contribution is mainly writing the paper, then I would tend to think that you would not be the primary author. If you have substantial contributions above and beyond writing the text of the paper, for example if you were primarily responsible for designing the investigation, or primarily responsible executing the investigation, these put you on much stronger ground to claim primary authorship.

Beyond that, there are special considerations for lead authorship. Though all authors are responsible for verifying the accuracy and integrity of the work, the lead author is the person who is primarily responsible for this. They are the one responsible for obtaining publication approval from all co-authors for publication, and then responsible for certifying the accuracy and integrity to the publisher / journal / conference / etc. Practically, the lead author is the corresponding author who interacts with journal / conference editors and reviewers. They are also typically the person who is responsible for defending the work publicly if it is to be presented at a conference. 

The person primarily responsible for conducting investigations and experiments is typically the lead author for these reasons. They are the person who is most able to certify the accuracy and integrity of the work. They're also the person most familiar with the actual investigation that is being written up, and most able to discuss the details. 

I have a few practical suggestions for you:

1) Most schools have specific authorship guidelines that can help with these kinds of decisions, for example:

http://research.wustl.edu/PoliciesGuidelines/Pages/AuthorshipPolicy.aspx

2) Relevant publishers, professional organizations, journals, conferences, etc. may have their own authorship policies.

Again, if your primary contribution has been writing the paper, that would not seem to indicate to me that you would be the lead author. Unfortunately, in your situation, it sounds like this graduate student has been somewhat exploitative. It makes sense to involve undergraduates in the writing process for many reasons, but it's unreasonable for you to write the entire thing. The primary scholar on a project (the lead author) should be the one responsible for drafting most of the text.