Closed
Description
The article Wrapping a REST API in GraphQL describes how to use a client-side schema to resolve queries against a REST endpoint.
The schema is inserted as a network layer in Relay – can something similar be achieved with Apollo Client?
Activity
dalgard commentedon Jul 11, 2016
Seems easier than I expected: Custom network interface
stubailo commentedon Jul 11, 2016
Yep, that's it! Very similar to the Relay approach.
linonetwo commentedon Aug 19, 2016
Great, I'm currently seeking for this. @dalgard Have you tried it yet?
stubailo commentedon Aug 19, 2016
By the way, if someone wants to write a post about this it would be awesome!
dalgard commentedon Aug 23, 2016
@linonetwo Not yet. Don't know whether I'm going to need it within the next couple of months. Still interested, though!
linonetwo commentedon Aug 24, 2016
OK I'm trying it...
what can be inferred from apollo's docs is just:
I need to refer to relay's doc for further implementation examples...
linonetwo commentedon Aug 24, 2016
So I don't need a 「graphql」 package, instead a 「graphql-tool」 at the client side?
linonetwo commentedon Aug 24, 2016
Well, I think writing REST2GraphQL on the client side is much similar to what we usually wrote on the server side.
The only difference is that we use the connector to get data from database usually, now we use the connector to get data from REST endpoint.
I'm not sure whether I'm on the right path or not.
stubailo commentedon Aug 24, 2016
@linonetwo you're definitely on the right path. What you are basically doing is writing a "GraphQL server", but on the client, so any of the GraphQL server docs will apply.
You don't need
apollo-server
orexpress-graphql
for this - those packages are for attaching GraphQL to a real web server, which you don't have or need on the client. You just need to get aGraphQLSchema
instance and call it yourself via thegraphql
function from thegraphql
package.linonetwo commentedon Aug 25, 2016
Thanks for guiding.
Another question: One REST endpoint would give me tons of data. Typically if I want to fill in blanks for this type;
I will write resolver functions, each will just getting small piece of data.
though /api/account/whoami will return
which covers many of fields of User type.
should I still write resolving functions for every field?
And addQueryMerging(REST2GraphQLInterface) will do some magic for this?
I think I can cache data inside connector.
if
/api/account/whoami
returnsand I only need
departmentName
andcompanyName
for this time, I can cache the whole json inside User model, and set an expire time or so. When next query aboutrole
goto User model will hit this cache .But it looks redundantly with apollo-client 's client side cache.
So my opinion is to simulate the batching again here. If two queries come together within 10ms, second one will hit the cache.
Another option is caching at Model level and use graphQL decorator or so, to pass an 'force fetch' argument to this level. In this way, "client side server" 's caching won't be obscure to user.
Which way is better still need experiment.
linonetwo commentedon Aug 25, 2016
I will write a post after this...
I'm currently using「graphql」package to parse executeableSchema like this:
And it doesn't work, throwing
Error: Must provide a schema definition
from\node_modules\graphql\utilities\buildASTSchema.js
Maybe executableSchema is not what it wants?
stubailo commentedon Aug 26, 2016
What is
schema
? perhaps it's not the right kind of object?stubailo commentedon Aug 26, 2016
Also, you don't need to write resolvers like
({ field }) => field
- that's the default behavior. So you only need explicit resolvers for the case where you need to do some transformation on the data.linonetwo commentedon Aug 28, 2016
OK, My only reference is GitHunt... Learnt from it :
that
every field we are using should a resolver function, or we can't get data for it.
Are there any edge case learning material?
8 remaining items
Urigo commentedon Feb 7, 2017
@linonetwo I would love to see an English version of that post!
If you need help with that I would love too
linonetwo commentedon Feb 12, 2017
@Urigo Ok, I'm working on that, please wait for a while.
linonetwo commentedon Feb 12, 2017
@Urigo http://onetwo.ren/wrapping-restful-in-graphql/
Were there any bug or issue please point out. Any question, ask me and I will add some content. But what I want to say is that
lucasconstantino commentedon Mar 14, 2017
I've being working on a project with apollo-client/server both on the browser wrapping a rest api. I works smoothly, with File upload support, Apollo client dev tools working properly, etc, etc. I'm working on a post in portuguese, but I guess I can make a copy in english and put in on Medium or similar. Does anyone have a better or more contextualized place to publish it, in case it fits?
Urigo commentedon Mar 15, 2017
@lucasconstantino Medium sounds great.
If you want we can review it and in the case will fit and a lot of people will be interested, publish it also under our Apollo publication
lucasconstantino commentedon Mar 29, 2017
Here it goes: https://medium.com/@constantinoyo/graphql-today-using-apollo-for-applications-that-still-depend-on-rest-apis-839895ce20d0
SeanBannister commentedon Jul 7, 2017
For anyone else stumbling across this you might like to check out https://github.com/mstn/apollo-local-network-interface
jozanza commentedon Nov 22, 2017
Looks like the approach with Apollo 2.0 is different with the "link" approach replacing network interfaces. Here's how I personally got my plain old
GraphQLSchema
instance working:Vanuan commentedon Jan 3, 2018
Here's an example for Apollo client 2.0:
https://stackoverflow.com/a/48082522/99024