Thisprojectdemonstrateshowtobuildaproduction-readyapplicationwithPrismaandgraphql-yoga.TheAPIprovidedbytheGraphQLserveristhefoundationforanapplicationsimilartoAirBnB.
GetstartedNote:prismaislistedasadevelopmentdependencyandscriptinthisproject'spackage.json.ThismeansyoucaninvokethePrismaCLIwithouthavingitgloballyinstalledonyourmachine(byprefixingitwithyarn),e.g.yarnprismadeployoryarnprismaplayground.IfyouhavethePrismaCLIinstalledglobally(whichyoucandowithnpminstall-gprisma),youcanomittheyarnprefix.
1.Downloadtheexample&installdependenciesClonetherepositorywiththefollowingcommand:
gitclonegit@github.com:graphcool/graphql-server-example.gitNext,navigateintothedownloadedfolderandinstalltheNPMdependencies:
cdgraphql-server-exampleyarninstall2.DeploythePrismadatabaseserviceYoucannowdeploythePrismaservice(notethatthisrequiresyoutohaveDockerinstalledonyourmachine-ifthat'snotthecase,followthecollapsedinstructionsbelowthecodeblock):
cdprismadocker-composeup-dcd..yarnprismadeployIdon'thaveDockerinstalledonmymachineTodeployyourservicetoapubliccluster(ratherthanlocallywithDocker),youneedtoperformthefollowingsteps:
Removetheclusterpropertyfromprisma.yml.Runyarnprismadeploy.WhenpromptedbytheCLI,selectapubliccluster(e.g.prisma-eu1orprisma-us1).Replacetheendpointinindex.tswiththeHTTPendpointthatwasprintedafterthepreviouscommand.NoticethatwhendeployingthePrismaservicefortheveryfirsttime,theCLIwillexecutethemutationsfromprisma/seed.graphqltoseedsomeinitialdatainthedatabase.TheCLIisawareofthisfilebecauseit'slistedinprisma/prisma.ymlundertheseedproperty.
3.StarttheGraphQLserverThePrismadatabaseservicethat'sbackingyourGraphQLserverisnowavailable.Thismeansyoucannowstarttheserver:
yarndevThedevscriptstartstheserver(onhttps://localhost:4000)andopensaGraphQLPlaygroundwhereyougetaccestotheAPIofyourGraphQLserver(definedintheapplicationschema)aswellastheunderlyingPrismaAPI(definedintheauto-generatedPrismadatabaseschema)directly.
InsidethePlayground,youcanstartexploringtheavailableoperationsbybrowsingthebuilt-indocumentation.
TestingtheAPICheckqueries/booking.graphqlandqueries/queries.graphqltoseeseveralexampleoperationsyoucansendtotheAPI.Togetanunderstandingofthebookingflows,checkthemutationsinqueries/booking.graphql.
DeploymentAquickandeasywaytodeploytheGraphQLserverfromthisrepositoryiswithZeitNow.AfteryoudownloadedtheNowDesktopapp,youcandeploytheserverwiththefollowingcommand:
now--dotenv.env.prodNoticethatyouneedtocreatethe.env.prodfileyourselfbeforeinvokingthecommand.Itshouldlistthesameenvironmentvariablesas.envbutwithdifferentvalues.Inparticular,youneedtomakesurethatyourPrismaserviceisdeployedtoaclusterthataccessibleovertheweb.
Hereisanexampleforwhat.env.prodmightlooklike:
PRISMA_STAGE="prod"PRISMA_CLUSTER="public-tundrapiper-423/prisma-us1"PRISMA_ENDPOINT="https://us1.prisma.sh/public-tundrapiper-423/prisma-airbnb-example/dev"PRISMA_SECRET="mysecret123"APP_SECRET="appsecret321"TolearnmoreaboutdeployingGraphQLserverswithZeitNow,checkoutthistutorial.
TroubleshootingI'mgettingtheerrormessage[Networkerror]:FetchError:requesttohttps://localhost:4466/auth-example/devfailed,reason:connectECONNREFUSEDwhentryingtosendaqueryormutationThisisbecausetheendpointforthePrismaserviceishardcodedinindex.js.Theserviceisassumedtoberunningonthedefaultportforalocalcluster:https://localhost:4466.Apparently,yourlocalclusterisusingadifferentport.
Younowhavetwooptions:
Figureouttheportofyourlocalclusterandadjustitinindex.js.Youcanlookitupin~/.prisma/config.yml.Deploytheservicetoapubliccluster.ExpandtheIdon'thaveDockerinstalledonmymachine-sectioninstep2forinstructions.Eitherway,youneedtoadjusttheendpointthat'spassedtothePrismaconstructorinindex.jssoitreflectstheactualclusterdomainandserviceendpoint.
LicenseMIT
评论