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

Added a New, Optional Apollo Link: HeadersLink #9

Merged
merged 12 commits into from
Dec 6, 2021

Conversation

joncursi
Copy link
Member

@joncursi joncursi commented Dec 2, 2021

This link allows you to access the REST API's response headers within the data object that comes back from Apollo Client.

NOTE: Since GraphQL only accepts field names written in camelCase, the headers should be requested in camel case format. I.e. if your REST API returns a total-count header, you'll want to ask for it using totalCount.

HeadersLink Setup

import { RestLink } from 'apollo-link-rest';
import { HeadersLink } from 'apollo-rest-utils';

const headersLink = new HeadersLink();
const restLink = new RestLink({ ... });

new ApolloClient({
  ...
  cache: new InMemoryCache({ ... }),
  link: ApolloLink.from([headersLink, restLink]),
  ...
});

HeadersLink Usage

const { data } = wrapRestQuery<'something'>()(
  gql`
    query SomethingQuery($id: String!) {
      something(id: $id) {
        _id
        fieldA
        fieldB
        fieldC
      }
      headers {
        nextOffset
        totalCount
        totalPages
      }
    }
  `,
  {
    endpoint: REST.GET.SOMETHING,
    variables: {
      id,
    },
  },
);

console.log(data?.something); // response data
console.log(data?.headers); // response headers

@joncursi joncursi requested a review from TheCleric December 2, 2021 17:16
Copy link
Collaborator

@TheCleric TheCleric left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Thanks for making this better!

We'll need to get some unit tests in here though to get the PR to pass. Let me know if I can help with that @joncursi

@joncursi
Copy link
Member Author

joncursi commented Dec 6, 2021

@TheCleric thanks and yes, would definitely appreciate some assistance on that! :)

@github-actions
Copy link

github-actions bot commented Dec 6, 2021

Total Coverage: 100.00%

Coverage Report
FileBranchesFuncsLinesUncovered Lines
lib/HeadersLink
   index.ts100%100%100%
lib/generateRoutes
   index.ts100%100%100%
lib/types
   index.ts100%100%100%
   invalidQueryError.ts100%100%100%
lib/types/restSchema
   index.ts100%100%100%
lib/useRestQuery
   index.ts100%100%100%

@github-actions
Copy link

github-actions bot commented Dec 6, 2021

Total Coverage: 100.00%

Coverage Report
FileBranchesFuncsLinesUncovered Lines
lib/HeadersLink
   index.ts100%100%100%
lib/generateRoutes
   index.ts100%100%100%
lib/types
   index.ts100%100%100%
   invalidQueryError.ts100%100%100%
lib/types/restSchema
   index.ts100%100%100%
lib/useRestQuery
   index.ts100%100%100%

@TheCleric TheCleric merged commit 239ebd2 into main Dec 6, 2021
@TheCleric TheCleric deleted the feature/headersLink branch December 6, 2021 16:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants