Of the many possible motives behind the composition of early Christian texts concerning the life of Mary such as the Protevangelium Jacobi (PJ), apologetic considerations certainly emerge as some of the most compelling and immediately identifiable. Early Church members were desperate to defend the legitimacy of their faith to the skeptical (and often polemical) anti-Christian philosophers, Jews, and polytheists among whom they lived, and the gospels inconveniently left an immense dearth of background information and supporting details concerning the lives of Jesus Christ and his first followers. The mysterious development of the Christian movement in a backwater province of the Roman Empire necessitated the appropriation of an older theistic tradition in order for the Church to assert itself as relevant in its contemporary religious context, and this it found in Judaism; however, the obscure heritage of the movement's founding figure provided an easy target for writers seeking to undermine the upstart cult's increasing popularity.
Origen wrote his extensive and seminal apologetic treatise Against Celsus solely to defend the Christian faith against one such attack, the philosopher Celsus' True Doctrine, which has only survived to us in the fragments quoted by Origen. In one especially useful passage (Contra Celsum 1.28) Origen relates the charges presented by Celsus against Mary, who claims that she came from a poor family in a nondescript Jewish village and made her livelihood by weaving. After she married Joseph, a poor carpenter, she was convicted of adultery and driven out of the house, so she wandered down to Egypt where she gave birth to the illegitimate child Jesus, and when he grew up he learned magic and acquired miraculous powers from the Egyptians and returned to his homeland to be worshipped as a god. The outrage such accusations would have provoked among the early Christians is obvious, but Celsus apparently goes even further and claims that the perpetrator of Mary's infidelity was a soldier named Panthera, making Jesus the bastard son of a mixed union between a Jew and a Roman of the lowest classes (Contra Celsum 1.32). The Jewish Babylonian Talmud (Talmud Bavli) contains a similar account (in less explicitly vitriolic rhetoric), indicating some kind of common tradition among anti-Christian communities. Such attacks on the blessed Virgin could not be left unresolved if Jesus' ministry and message were to be believed, so the composition of such a text as PJ clearly became an excellent and necessary vessel for rebuttal.
We see in the PJ narrative that all of these counts against Mary's life and background are addressed in detail, so that no further doubts might surface. Instead of a poor family from a small rural village, Mary's parents are some of the richest Jews in Jerusalem (PJ 1.1-3) and also belong to the royal tribe of David (10.1); Mary does not work as a seamstress for wages, but was appointed only to sew a sacred curtain for the temple of God, presumably the same which is later torn during Jesus' crucifixion (10.1-2, 12.1); Joseph (an innocuous old man) is not merely a poor home carpenter but a successful building contractor (9.2, 13.1). Perhaps most importantly, Mary's virginity is safeguarded from her birth, first in the temple and then as a ward in the house of Joseph, until the time of divine conception; her purity is tested and proven by the Jewish high priest in a strange ritual, and finally Salome's postpartum inspection demonstrates the anomalous intactness of her virginal hymen, even after giving birth. Throughout the narrative, there is not a single moment of her life left unaddressed when she could have been defiled. This is arguably the most important doctrinal innovation of the entire PJ text, since it established the concept of Mary's perpetual virginity, and also provided a foundation for virginity to be viewed as a virtue in Christian women, leading to the development of ascetic continence and related movements in the early Church.
With this background established, it seems that critics of Jesus' parentage would have been satisfactorily quelled, and later texts regarding Mary would not need to present further apologetic reinforcement. However, comparison with later writings in the PJ tradition reveals that this was not the case; the "Gospel of Pseudo-Matthew" written several hundred years later even further emphasizes aspects of Marian veneration, and many details are embellished to make her conception and childhood even more glorified. Her father becomes even richer and more pious; Mary is not only protected from defilement, but willfully vows her perpetual virginity; she is constantly accompanied by a group of other dedicated virgins; the high priest's purity test is administered not once but seven times; and the postpartum virginity is confirmed not by only one but by both midwives. The later "Old English Gospel of Pseudo-Matthew" makes Joachim even richer (the wealthiest Jew alive!); Mary's order of avowed virgins in the Jewish temple becomes a standard fixture; she creates the first regimented monastic schedule; she becomes the foremost expert in Jewish law and worship; Joseph is grafted into the tribe of Judah so that Jesus would have both a royal and priestly lineage; and the high priests confirm Mary's purity not only by seven tests, but by countless signs of divine mysteries. Finally, Jacobus de Veragine's Golden Legend creates an entire family tree of apostles and early Christian saints around Mary's lineage, and she receives visitations from angels and the vision of God on a daily basis.
Therefore, many of the tenets of Marian doctrine that we find rooted in the narrative of PJ were apparently promulgated by the early Church for entirely apologetic purposes, addressing specific accusations and attacks against Jesus' background. However, we see the story of Mary's life and heritage become increasingly grandiose throughout the centuries, in spite of the fact that it was no longer under attack or even in question. In fact, after Christianity had become firmly established as the most prominent religion of the Mediterranean in the fourth and fifth centuries, there ceased to be any challenges or disparagements whatsoever of the tradition of Mary's background presented in PJ. So what precipitated this sensational enhancement of the Marian narrative evident throughout these later texts? The simplest answer of course would point to the increased elevation and veneration of the Virgin in the Late Antique and Early Medieval Church, but perhaps the question is unanswerable. In comparison with the original outline of PJ, these later traditions seem ludicrous and completely fantastic, and are likely as much a product of their contemporary cultural and literary environment as any indication of real piety towards Mary on a progressively greater scale. However, in order to defend such an argument, the examination would require an extensive analysis of an enormous and unwieldy corpus of literature, so for now the question must remain open for interpretation and theorization.