-
Notifications
You must be signed in to change notification settings - Fork 344
[Feature] apollo-link-http GET method remove ignored characters #1079
Comments
I’ve tried to do the modification outside the import { ApolloLink } from 'apollo-link';
import gql from 'graphql-tag';
import { print } from 'graphql/language/printer';
import { stripIgnoredCharacters } from 'graphql/utilities/stripIgnoredCharacters';
class CreateShortQueryLink extends ApolloLink {
request(operation, forward) {
const strippedQuery = stripIgnoredCharacters(print(operation.query));
const request = {
...operation,
getContext: operation.getContext,
setContext: operation.setContext,
query: gql(strippedQuery),
};
return forward(request);
}
}
const link = ApolloLink.from([
new CreateShortQueryLink(),
createHttpLink({ uri: 'http://localhost:3000/graphql' }),
]); It seems that the print(gql('query Posts{posts(orderBy:id_DESC){id title __typename}}'))
// outputs
query Posts{posts(orderBy:id_DESC){id title __typename}} query Posts {
posts(orderBy: id_DESC) {
id
title
__typename
}
} This same method
|
Also tried the above with something like: export const createMinifyLink = () => {
return new ApolloLink((operation: Operation, forward: NextLink) => {
const stripped = stripIgnoredCharacters(operation.query.loc!.source);
return forward({
...operation,
getContext: operation.getContext,
setContext: operation.setContext,
query: gql(stripped),
});
});
}; But get a load of warnings about re-using fragment names and problems writing to store. I would use an HTTP Interceptor but then you've got issues with differing hashes when using APQ. I've got queries that are 44kb and full of You can't even pre-process it because ApolloClient just adds __typename throughout and re-prettifies it: export function stripIgnoredCharactersGql(literals: string[], ...placeholders: any[]) {
const strippedLiterals = literals.map(str => stripIgnoredCharacters(str));
return gql(strippedLiterals, ...placeholders);
}
export const configQuery = stripIgnoredCharactersGql`
query {
hello {
world
}
}
`; |
We've ended up patching apollo-link-http-common via https://www.npmjs.com/package/patch-package to use stripIgnoredCharacters right after printer was used: patches/apollo-link-http-common+0.2.13.patch
|
For a project that I'm working on we're making use of the GET method to make the call benefit of HTTP caching. This is working successfully but we do notice that the query parameter contains characters which are not necessary for executing the query.
Example
Below an example were we are using
graphql-tag
to parse the query. Newlines are included for readability of the query.The actual behaviour results in the following HTTP request:
Expected behaviour is a GET query without unnecessary characters:
I currently don't directly see a way/ability to implement a workaround to achieve the same result. If this request make sense I can provide a PR for this to do the necessary modifications.
apollo-link/packages/apollo-link-http/src/httpLink.ts
Lines 198 to 203 in 951217b
The body of
rewriteURIForGET
can be stripped before going through theencodeURIComponent
.It would be possible to reuse the logic that is used in
graphql
package where there is a utility method available stripIgnoredCharacters.The package
graphql
is already referenced inapollo-link-http
and can be reused or custom logic can be directly included to sanitize the query.The text was updated successfully, but these errors were encountered: