Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Relay/GraphQL with Loopback?

Any working solutions of using Relay/GraphQL with Loopback? I guess a few things I'm considering are how to access the database (since I'm assuming going through the ORM wouldn't be possible) and how to leverage the api generators when using Relay/GraphQL...

like image 712
Detuned Avatar asked May 22 '16 16:05

Detuned


2 Answers

I have created this npm library to generate GraphQL schema from loopback models: https://github.com/Tallyb/loopback-graphql

like image 117
Tally Barak Avatar answered Nov 18 '22 16:11

Tally Barak


Just for the other people out there, I came across this answer and I was still confused whether I could implement a GraphQL api in Loopback or not. I used Apollo's apollo-server package for Express. Since Loopback is based on Express, calling

app.use('/graphql', bodyParser.json(), graphqlExpress({schema}));
app.use('graphiql', graphiqlExpress({
  endpointURL: "/graphql"
}))

works perfectly out of the box. Just follow their tutorials for Express code and it should work. As far as the database stuff goes, it seems like you can use resolvers as the middle layer in place of Loopback's remote methods. For each resolver that points to a piece of data, you can call the contextual app method to fetch data from your database.

like image 38
Huy-Anh Hoang Avatar answered Nov 18 '22 15:11

Huy-Anh Hoang