Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with new {args.id} style #134

Closed
karensg opened this issue Jul 16, 2018 · 5 comments
Closed

Issue with new {args.id} style #134

karensg opened this issue Jul 16, 2018 · 5 comments
Assignees
Labels
blocking Prevents production or dev due to perf, bug, build error, etc.. bug 🐛 has-reproduction ❤ Has a reproduction in a codesandbox or single minimal repository

Comments

@karensg
Copy link

karensg commented Jul 16, 2018

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?

@haipengz
Copy link

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

@fbartho
Copy link
Collaborator

fbartho commented Jul 17, 2018

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.

@fbartho fbartho added bug 🐛 blocking Prevents production or dev due to perf, bug, build error, etc.. has-reproduction ❤ Has a reproduction in a codesandbox or single minimal repository labels Jul 17, 2018
@fbartho fbartho self-assigned this Jul 17, 2018
@fbartho
Copy link
Collaborator

fbartho commented Jul 18, 2018

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!

@haipengz
Copy link

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

@haipengz
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blocking Prevents production or dev due to perf, bug, build error, etc.. bug 🐛 has-reproduction ❤ Has a reproduction in a codesandbox or single minimal repository
Projects
None yet
Development

No branches or pull requests

3 participants