How to use Relay/GraphQL with Loopback?
Asked Answered
C

2

8

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...

Cumuliform answered 22/5, 2016 at 16:13 Comment(2)
You should create a middle layer(GraphQL), which resolve data from Loopback, and provide data to front-end(Relay)Mcdowell
Any examples for this by chance? Thank you!Cumuliform
A
2

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.

Artur answered 26/9, 2017 at 5:48 Comment(0)
P
7

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

Platino answered 15/10, 2016 at 14:2 Comment(1)
Please elaborate. Link only answers are not enough, because they will become useless if the linked content goes down.Aswan
A
2

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.

Artur answered 26/9, 2017 at 5:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.