jonask
jonask

Reputation: 748

Multiple endpoints in same query

It works perfectly, with a single endpoint. With apollo-link-rest, I have made a client that looks like this

const restLink = new RestLink({ uri: "https://example.com/" })

And export the client with a new ApolloClient({...})

Now to the question

On the same server https://example.com/, there are multiple endpoints, all with same fields but different data in each

The first query that works look like this

export const GET_PRODUCTS = gql`
   query firstQuery {
    products @rest(type: "data" path: "first/feed") { // the path could be second/feed and it will work with different data
     id
     title
   }
 }
`

I want all these different path into one and same json feed, because they all have the same fields, but with different data

Upvotes: 0

Views: 1315

Answers (1)

xadm
xadm

Reputation: 8418

Using aliases

You can (should be possible) use standard method to make similar queries - get many data (result) nurmally available as the same shape (node name). This is described here.

{ 
  "1": products(....
  "2": products(....
  ...
}

Paths can be created using variables

Results can be easy combined by iterating over data object. Problem? Only for fixed amount (not many) endpoints as query shouldn't be generated by strings manipulations.

Multiple graphql queries

You can create queries in a loop - parametrized - using Promise.all() and apollo-client client.query(. Results needs to be combined into one, too.

Custom fetch

Using custom fetch you can create a query taking an array of paths. In this case resolver should use Promise.all() on parametrized fetch requests. Combined results can be returned as single node (as required).

Bads

All these methods needs making multiple requests. Problem can be resolved by making server side REST wrapper (docs or blog).

Upvotes: 1

Related Questions