Apollo-link-rest: Issue with new {args.id} style

Created on 16 Jul 2018  ·  5Comments  ·  Source: apollographql/apollo-link-rest

Hi,

Just updated the library and have issues upgrading. Without any changes the functionality works as expected with a warning to migrate to the new style. When I make the needed changes like below I get that both {args} and {args.name} are undefined. Here is the code:

const restLink = new RestLink({
  uri: 'https://avoindata.prh.fi/bis/v1',
  typePatcher: {
    BusinessPayload: data => ({
      ...data,
      results: data.results.map(business => ({
        __typename: 'Business',
        ...business
      }))
    })
  }
});

const client = new ApolloClient({
  link: restLink,
  cache: new InMemoryCache()
});

const query = gql`
  query($name: String!) {
    companies(name: $name)
      @rest(
        type: "BusinessPayload"
        path: "?totalResults=false&maxResults=10&resultsFrom=0&name={args.name}"
      ) {
      results {
        name
        businessId
      }
    }
  }
`;

// And then call the query:
client.query({ query, variables: { name: value } });

Am i missing something?

blocking bug 🐛 has-reproduction

Most helpful comment

Fixed in v0.4.1: https://github.com/apollographql/apollo-link-rest/releases/tag/v0.4.1 Please let me know if you're still seeing it!

All 5 comments

Same issue. Just got undefined using the new {args.id} style.

Great catch @karensg @haipengz -- This is a key priority for me, but I got interrupted yesterday before being able to fully test these changes.

@karensg While your syntax should work (and it's a bug that it doesn't), I intended the api to be used slightly differently.
Instead of: path: "?totalResults=false&maxResults=10&resultsFrom=0&name={args.name}"
I intended it to be: path: "?totalResults=false&maxResults=10&resultsFrom=0&{args}"

{args} or {args.query.deeper} takes the object pointed to by the key path, and then turns it into a query string. So &{args} in your situation would be { name: "foo" } becomes &name=foo where foo automatically gets encodeURIComponent called on it.

I consider this a my second immediate top priority, but I'm fighting fires at work, so I won't get to it till the afternoon. In general, I appreciate any help you guys can provide, you can find me in the ApolloGraphQL slack in the #apollo-link-rest room.

One thing that would be really useful / speed up my fix would be some code for unit tests for each case that you're using, so I can make sure it works for you going forwards.

Fixed in v0.4.1: https://github.com/apollographql/apollo-link-rest/releases/tag/v0.4.1 Please let me know if you're still seeing it!

@fbartho Thanks for the fixes! It now works for my use case.

Thanks again!
After upgraded to the v0.4.1, the new {args.id} style now works for my case, no longer got undefined.

...
const GET_DOG_IMAGES_BY_BREED = gql`
  query breeds ($breed: String!) {
    imagesByBreed(breed: $breed) @rest(
      type: "imagesByBreedResult"
      path: "breed/{args.breed}/images"
      endpoint: "dogApi"
    ) {
      status
      message
    }
  }
`

...
<Query query={GET_DOG_IMAGES_BY_BREED}
    notifyOnNetworkStatusChange
    variables={{ breed: 'hound' }}
 >
  {({ refetch, error, data, networkStatus, }) => {
    expect(error).toBeUndefined()
    expect(data).toBeDefined()
  }}
</Query>
...

Was this page helpful?
0 / 5 - 0 ratings