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

Allow for custom queryTransformers #2

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,38 @@ console.log(query.documentId); // => 5eef6cd6a52ee0d67bfbb0fdc72bbbde4d70331834e

* `generateId`: `function (querySource: string) => string` Function that allows to generate a custom documentId from the query source. This source contains all the dependencies sources concatenated, so it's suitable for hashing. By default it generates the sha256 hash in hexadecimal format. The source is concatenated in the same way as you'd get it from the `persistgraphql` tool, so hashing the queries from the output of that tool should get you the same hash value.
* `addTypename`: `boolean` Apply a query transformation to the query documents, adding the __typename field at every level of the query. You must pass this option if your client code uses this query transformation.
* `queryTransformers`: `array[function(documentNode) => documentNode]` Directly override the query transformations list to provide additional transforms as needed. This is what a query transformer to strip the client fields looks like:

```js

function removeClientFieldsTransformer(doc: DocumentNode) => {
function removeClientFieldsFromSelectionSet(selectionSet) {
if (selectionSet.selections) {
// eslint-disable-next-line no-param-reassign
selectionSet.selections = selectionSet.selections.map((selection) => {
if (selection.directives && selection.directives.length) {
const hasClient = selection.directives.find(d => d.name.value === 'client');
if (hasClient) return false;
}
if (selection.kind === 'Field' || selection.kind === 'InlineFragment') {
if (selection.selectionSet) {
removeClientFieldsFromSelectionSet(selection.selectionSet);
}
}
return selection;
}).filter(s => !!s);
}
return selectionSet;
}

const docClone = JSON.parse(JSON.stringify(doc));

docClone.definitions.forEach((definition: DefinitionNode) => {
const isRoot = definition.kind === 'OperationDefinition';
removeClientFieldsFromSelectionSet(definition.selectionSet, isRoot);
});

return docClone;
};
```
Further reading on the DocumentNode object can be accomplished here: https://github.com/graphql/graphql-js/blob/master/src/language/ast.js
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ module.exports = function graphQLPersistedDocumentLoader(content) {

function tryAddDocumentId(options, content, querySource) {
const queryMap = new ExtractGQL({
queryTransformers: [options.addTypename && queryTransformers.addTypenameTransformer].filter(Boolean)
queryTransformers: options.queryTransformers || [options.addTypename && queryTransformers.addTypenameTransformer].filter(Boolean)
}).createOutputMapFromString(querySource);

const queries = Object.keys(queryMap);
Expand Down