-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Alessia Bellisario <alessia@apollographql.com> Co-authored-by: Jerel Miller <jerelmiller@gmail.com>
- Loading branch information
1 parent
e285dfd
commit 64b3048
Showing
8 changed files
with
91 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@apollo/client": patch | ||
--- | ||
|
||
Fix `includeUnusedVariables` option not working with `BatchHttpLink` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import gql from 'graphql-tag'; | ||
import { filterOperationVariables } from '../filterOperationVariables'; | ||
import { createOperation } from '../createOperation'; | ||
|
||
const sampleQueryWithVariables = gql` | ||
query MyQuery($a: Int!) { | ||
stub(a: $a) { | ||
id | ||
} | ||
} | ||
`; | ||
|
||
const sampleQueryWithoutVariables = gql` | ||
query MyQuery { | ||
stub { | ||
id | ||
} | ||
} | ||
`; | ||
|
||
describe('filterOperationVariables', () => { | ||
it('filters unused variables', () => { | ||
const variables = { a: 1, b: 2, c: 3 }; | ||
const result = filterOperationVariables( | ||
variables, | ||
createOperation({}, { query: sampleQueryWithoutVariables, variables }) | ||
); | ||
expect(result).toEqual({}); | ||
}); | ||
|
||
it('does not filter used variables', () => { | ||
const variables = { a: 1, b: 2, c: 3 }; | ||
const result = filterOperationVariables( | ||
variables, | ||
createOperation({}, { query: sampleQueryWithVariables, variables }) | ||
); | ||
expect(result).toEqual({ a: 1 }); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { VariableDefinitionNode, visit } from "graphql"; | ||
|
||
import { Operation } from "../core"; | ||
|
||
export function filterOperationVariables(variables: Record<string, any>, operation: Operation) { | ||
const result = { ...variables }; | ||
const unusedNames = new Set(Object.keys(variables)); | ||
visit(operation.query, { | ||
Variable(node, _key, parent) { | ||
// A variable type definition at the top level of a query is not | ||
// enough to silence server-side errors about the variable being | ||
// unused, so variable definitions do not count as usage. | ||
// https://spec.graphql.org/draft/#sec-All-Variables-Used | ||
if (parent && (parent as VariableDefinitionNode).kind !== 'VariableDefinition') { | ||
unusedNames.delete(node.name.value); | ||
} | ||
}, | ||
}); | ||
unusedNames.forEach(name => { | ||
delete result![name]; | ||
}); | ||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters