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

The deduplicator now accepts a query param #312

Closed
wants to merge 7 commits into from
Closed
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ The `options` object has the following fields:
| `uploads` | [UploadOptions](/src/types.ts#L39-L43) or `false` or `undefined` | `null` | Provides information about upload limits; the object can have any combination of the following three keys: `maxFieldSize`, `maxFileSize`, `maxFiles`; each of these have values of type Number; setting to `false` disables file uploading |
| `https` | [HttpsOptions](/src/types.ts#L62-L65) or `undefined` | `undefined` | Enables HTTPS support with a key/cert
| `getEndpoint` | String or Boolean | `false` | Adds a graphql HTTP GET endpoint to your server (defaults to `endpoint` if `true`). Used for leveraging CDN level caching. |
| `deduplicator` | Boolean | `true` | Enables [graphql-deduplicator](https://github.com/gajus/graphql-deduplicator). Once enabled sending the header `X-GraphQL-Deduplicate` will deduplicate the data. |
| `deduplicator` | Boolean | `true` | Enables [graphql-deduplicator](https://github.com/gajus/graphql-deduplicator). Once enabled, adding `?deduplicate` to the endpoint URL will deduplicate the data. |
| `bodyParserOptions` | BodyParserJSONOptions | [BodyParserJSONOptions Defaults](https://github.com/expressjs/body-parser#bodyparserjsonoptions) | Allows pass through of [body-parser options](https://github.com/expressjs/body-parser#bodyparserjsonoptions) |

Additionally, the `options` object exposes these `apollo-server` options:
Expand Down
17 changes: 12 additions & 5 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ test('Response data can be deduplicated with graphql-deduplicator', async t => {
}).promise()

const deduplicated = await request({
uri: uri + '?deduplicate',
method: 'POST',
json: true,
body: { query },
}).promise()

// The use of a header is deprecated but should work
const deduplicatedWithHeader = await request({
uri,
method: 'POST',
json: true,
Expand Down Expand Up @@ -155,10 +163,12 @@ test('Response data can be deduplicated with graphql-deduplicator', async t => {
},
})

t.deepEqual(deduplicated, deduplicatedWithHeader)

t.deepEqual(body.data, inflate(deduplicated.data))
Copy link
Contributor

Choose a reason for hiding this comment

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

If neither of deduplications worked, will the test pass?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, that one is just to check that both are the same, to check that data is properly deduplicated there's another check above

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, sure! I somehow missed t.deepEqual above 😅

})

test('graphql-deduplicated can be completely disabled', async t => {
test('graphql-deduplicator can be completely disabled', async t => {
const {
uri,
data: { book },
Expand All @@ -183,13 +193,10 @@ test('graphql-deduplicated can be completely disabled', async t => {
`

const body = await request({
uri,
uri: uri + '?deduplicate',
method: 'POST',
json: true,
body: { query },
headers: {
'X-GraphQL-Deduplicate': true,
},
}).promise()

t.deepEqual(body, {
Expand Down
12 changes: 11 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ export class GraphQLServer {
}
return (response, ...args) => {
if (
req.get('X-GraphQL-Deduplicate') &&
(isTrulyQueryParam(req.query.deduplicate) ||
req.get('X-GraphQL-Deduplicate')) &&
response.data &&
!response.data.__schema
) {
Expand Down Expand Up @@ -456,3 +457,12 @@ function mergeTypeDefs(typeDefs: ITypeDefinitions): string {
function isDocumentNode(node: any): node is DocumentNode {
return node.kind === 'Document'
}

/**
* Normalises GET parameter values into a boolean.
* Returns true in the following cases:
* ?value ?value= ?value=1 ?value=true
*/
function isTrulyQueryParam(value: string) {
return value === '' || value === '1' || value === 'true'
}