I recently implemented a schema and some resolvers for my Express server. I tested them successfully through /graphql
and now I would like to call the queries I implemented when accessing from a REST API, like so:
//[...]
//schema and root correctly implemented and working
app.use('/graphql', graphqlHTTP({
schema: schema,
rootValue: root,
graphiql: true,
}));
//I start the server
app.listen(port, () => {
console.log('We are live on ' + port);
});
//one of many GET handlers
app.get("/mdc/all/:param", function(req, res) {
//call one of the (parametrized) queries here
//respond with the JSON result
});
How can I call the queries I defined with GraphQL inside my GET handlers? How do I pass parameters to them?
Thank you!
Basically you can just use http post method to fetch the data from a GraphQL API, but here very nice solution using node-fetch , to install it:
and the code to use it is:
this solution was taken from here
I used Apollo and performed queries directly on
/graphql
through POST from the front-end.