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

Feature: Query String Building #39

Closed
fbartho opened this issue Dec 26, 2017 · 2 comments
Closed

Feature: Query String Building #39

fbartho opened this issue Dec 26, 2017 · 2 comments

Comments

@fbartho
Copy link
Collaborator

fbartho commented Dec 26, 2017

This was raised as an issue in the #apollo-link-rest Slack Channel: Currently, when you have a REST API query with multiple arguments, you end up repeating yourself, expressing the exported arguments in the URI: /path/to/resource?arg1=:arg1&arg2=:arg2&arg3=:arg3… -- Just like we built the bodyBuilder for Mutations, maybe we should extend its use to implicitly build the query parameters?

Not sure if this is a good generic way to build this API. For now it seems like a nice to have enhancement, but I don't know what pitfalls or problems it could cause in the API.

@fbartho
Copy link
Collaborator Author

fbartho commented Feb 20, 2018

Implemented via #69, #70

@fbartho fbartho closed this as completed Feb 20, 2018
@fbartho fbartho added this to the v0.2.0 milestone Feb 23, 2018
@zachdixon
Copy link

zachdixon commented May 31, 2018

I've looked through the pathBuilder PR and think a query string builder would still be beneficial. I'm getting into writing all my queries and ran across the thought of having to provide the pathBuilder function every time. It doesn't seem very DRY (which may not be your goal).

On the Apollo docs page, it mentions a params option for the @rest directive:

const QUERY = gql`
  query RestData($email: String!) {
    users @rest(path: '/users/email/:email', params: { email: $email }, method: 'GET', type: 'User')

I think this is outdated as I don't see anything in the code that would support this unless I'm missing it, but something similar for the query string would be nice.

query RestData($email: String!) {
    users @rest(type: "[User]", path: '/users/email', queryParams: {email: $email})
}

This would automatically create a query string based on the queryParams and append it to the path. I think I would actually prefer this in the case of multiple @rest in the same query that may not use every param such as:

query RestData($userId: ID!, $startDate: String!, $endDate: String!) {
    user @rest(type: "User", path: '/users/:userId') {
        id
        name
        reports @rest(type: "[UserReport]", path: '/users/reports', queryParams: {startDate: $startDate, endDate: $endDate})
    }
}

If I understand correctly, this would be the equivalent using pathBuilder

let getPathBuilder = url => params => `${url}?${queryString.stringify(params)}`

const query = gql`
  query RestDate($userId: ID!, $startDate: String!, $endDate: String!, $userPath: any, $reportsPath: any) {
    user (id: $id) @rest(type: "User", pathBuilder: $userPath) {
      id
      name
      reports (startDate: $startDate, endDate: $endDate) @rest(type: "[UserReport]", pathBuilder: $reportsPath)
  }
`

client.query({
  query,
  variables: {
    id: someId,
    startDate: "05-01-2018",
    endDate: "05-31-2018",
    userPath: getPathBuilder("/user"),
    reportsPath: getPathBuilder("/user/reports")
  }
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants