-
Notifications
You must be signed in to change notification settings - Fork 579
feat(apollo-usage-report): report referenced field by types #4020
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
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
3fedc8d
feat(apollo-usage-report): report referenced feild by types
EmrysMyrddin 511f447
add gzip compression
EmrysMyrddin 3500708
add batch + retry + timeout
EmrysMyrddin cfa4ef0
changeset
enisdenjo d2d6974
changeset
EmrysMyrddin 4fd20a8
fix after review
EmrysMyrddin d72a1e6
fix: handle failed parsing
EmrysMyrddin ebcef6c
chore(dependencies): updated changesets for modified dependencies
github-actions[bot] e00881f
fix last errors and add test
EmrysMyrddin b87c3ed
chore(dependencies): updated changesets for modified dependencies
github-actions[bot] 2364f4c
prettier
EmrysMyrddin b986c70
prettier
EmrysMyrddin 92fd753
add duplex option ?
EmrysMyrddin 03151f6
lint
EmrysMyrddin 61d91bd
fix compaitibility with old node versions
EmrysMyrddin a7a3d13
fix lint
EmrysMyrddin 47879ef
remove state
EmrysMyrddin 3e10948
feat: add control over unexecutable operation traces
EmrysMyrddin d225ad6
fix trace key for unexecutable operations
EmrysMyrddin 5d65f93
do not send unexecutable operations traces by default
EmrysMyrddin 1a35136
fix
EmrysMyrddin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
.changeset/@graphql-yoga_plugin-apollo-usage-report-4020-dependencies.md
This file contains hidden or 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,7 @@ | ||
--- | ||
"@graphql-yoga/plugin-apollo-usage-report": patch | ||
--- | ||
dependencies updates: | ||
- Added dependency [`@apollo/server-gateway-interface@^1.1.1` ↗︎](https://www.npmjs.com/package/@apollo/server-gateway-interface/v/1.1.1) (to `dependencies`) | ||
- Added dependency [`@apollo/utils.usagereporting@^2.1.0` ↗︎](https://www.npmjs.com/package/@apollo/utils.usagereporting/v/2.1.0) (to `dependencies`) | ||
- Added dependency [`@graphql-tools/utils@10.9.0-alpha-20250601230319-2f5f24f393b56915c1c913d3f057531f79991587` ↗︎](https://www.npmjs.com/package/@graphql-tools/utils/v/10.9.0) (to `dependencies`) |
This file contains hidden or 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 @@ | ||
--- | ||
'@graphql-yoga/plugin-apollo-usage-report': minor | ||
--- | ||
|
||
:construction: Improve report with per field request count, trace batching and report compression |
This file contains hidden or 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 @@ | ||
--- | ||
'@graphql-yoga/plugin-apollo-usage-report': minor | ||
--- | ||
|
||
Report referenced field by types |
This file contains hidden or 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,2 @@ | ||
[tools] | ||
node = "23" |
322 changes: 322 additions & 0 deletions
322
packages/plugins/apollo-usage-report/__tests__/apollo-usage-report.spec.ts
This file contains hidden or 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,322 @@ | ||
import { createSchema, createYoga, DisposableSymbols } from 'graphql-yoga'; | ||
import { Report } from '@apollo/usage-reporting-protobuf'; | ||
import { | ||
ApolloUsageReportOptions, | ||
useApolloUsageReport, | ||
} from '@graphql-yoga/plugin-apollo-usage-report'; | ||
import { createDeferredPromise } from '@whatwg-node/promise-helpers'; | ||
import { Reporter } from '../src/reporter'; | ||
|
||
describe('apollo-usage-report', () => { | ||
it('should send compressed traces', async () => { | ||
const testEnv = createTestEnv({ options: { alwaysSend: true } }); | ||
|
||
await testEnv.query(); | ||
|
||
const report = await testEnv.reportSent; | ||
const { ['# -\n{hello}']: traces } = report.tracesPerQuery; | ||
expect(traces).toBeDefined(); | ||
expect(traces?.referencedFieldsByType?.['Query']?.fieldNames).toEqual(['hello']); | ||
expect(traces?.trace).toHaveLength(1); | ||
|
||
await testEnv[DisposableSymbols.asyncDispose](); | ||
}); | ||
|
||
it('should handle operation name when defined', async () => { | ||
const testEnv = createTestEnv({ options: { alwaysSend: true } }); | ||
await testEnv.query('query test { hello }'); | ||
|
||
const report = await testEnv.reportSent; | ||
expect(report.tracesPerQuery['# test\nquery test{hello}']).toBeDefined(); | ||
|
||
await testEnv[DisposableSymbols.asyncDispose](); | ||
}); | ||
|
||
it('should trace unparsable requests', async () => { | ||
const testEnv = createTestEnv({ | ||
options: { alwaysSend: true, sendUnexecutableOperationDocuments: true }, | ||
}); | ||
await testEnv.query('this is an invalid request', 'test'); | ||
|
||
const report = await testEnv.reportSent; | ||
expect(Object.keys(report.tracesPerQuery).length).toBe(1); | ||
const [key, traces] = Object.entries(report.tracesPerQuery)[0]!; | ||
expect(key).toBe('## GraphQLParseFailure\n'); | ||
expect(traces).toBeDefined(); | ||
expect(traces?.trace).toHaveLength(1); | ||
expect(traces?.trace?.[0]).toMatchObject({ | ||
unexecutedOperationName: 'test', | ||
unexecutedOperationBody: 'this is an invalid request', | ||
}); | ||
|
||
await testEnv[DisposableSymbols.asyncDispose](); | ||
}); | ||
|
||
it('should trace invalid requests', async () => { | ||
const testEnv = createTestEnv({ | ||
options: { alwaysSend: true, sendUnexecutableOperationDocuments: true }, | ||
}); | ||
await testEnv.query('query test {unknown_field}', 'test'); | ||
|
||
const report = await testEnv.reportSent; | ||
expect(Object.keys(report.tracesPerQuery).length).toBe(1); | ||
const [key, traces] = Object.entries(report.tracesPerQuery)[0]!; | ||
expect(key).toBe('## GraphQLValidationFailure\n'); | ||
expect(traces).toBeDefined(); | ||
expect(traces?.trace).toHaveLength(1); | ||
expect(traces?.trace?.[0]).toMatchObject({ | ||
unexecutedOperationName: 'test', | ||
unexecutedOperationBody: 'query test {unknown_field}', | ||
}); | ||
|
||
await testEnv[DisposableSymbols.asyncDispose](); | ||
}); | ||
|
||
it('should trace unknown operation requests', async () => { | ||
const testEnv = createTestEnv({ | ||
options: { alwaysSend: true, sendUnexecutableOperationDocuments: true }, | ||
}); | ||
await testEnv.query('query test { hello }', 'unknown'); | ||
|
||
const report = await testEnv.reportSent; | ||
expect(Object.keys(report.tracesPerQuery).length).toBe(1); | ||
const [key, traces] = Object.entries(report.tracesPerQuery)[0]!; | ||
expect(key).toBe('## GraphQLUnknownOperationName\n'); | ||
expect(traces).toBeDefined(); | ||
expect(traces?.trace).toHaveLength(1); | ||
expect(traces?.trace?.[0]).toMatchObject({ | ||
unexecutedOperationName: 'unknown', | ||
unexecutedOperationBody: 'query test { hello }', | ||
}); | ||
|
||
await testEnv[DisposableSymbols.asyncDispose](); | ||
}); | ||
|
||
it('should not trace unparsable requests', async () => { | ||
const testEnv = createTestEnv({ | ||
options: { alwaysSend: true }, | ||
}); | ||
await testEnv.query('this is an invalid request'); | ||
await testEnv.query(); | ||
|
||
const report = await testEnv.reportSent; | ||
const { ['# -\n{hello}']: traces } = report.tracesPerQuery; | ||
expect(traces).toBeDefined(); | ||
expect(traces?.trace).toHaveLength(1); | ||
|
||
await testEnv[DisposableSymbols.asyncDispose](); | ||
}); | ||
|
||
it('should not trace invalid requests', async () => { | ||
const testEnv = createTestEnv({ | ||
options: { alwaysSend: true }, | ||
}); | ||
await testEnv.query('{unknown_field}'); | ||
await testEnv.query(); | ||
|
||
const report = await testEnv.reportSent; | ||
const { ['# -\n{hello}']: traces } = report.tracesPerQuery; | ||
expect(traces).toBeDefined(); | ||
expect(traces?.trace).toHaveLength(1); | ||
|
||
await testEnv[DisposableSymbols.asyncDispose](); | ||
}); | ||
|
||
it('should not trace unknown operation requests', async () => { | ||
const testEnv = createTestEnv({ | ||
options: { alwaysSend: true }, | ||
}); | ||
await testEnv.query('query test { hello }', 'unknown'); | ||
await testEnv.query(); | ||
|
||
const report = await testEnv.reportSent; | ||
const { ['# -\n{hello}']: traces } = report.tracesPerQuery; | ||
expect(traces).toBeDefined(); | ||
expect(traces?.trace).toHaveLength(1); | ||
|
||
await testEnv[DisposableSymbols.asyncDispose](); | ||
}); | ||
|
||
it('should batch traces and send once maxBatchDelay is reached', async () => { | ||
const testEnv = createTestEnv({ options: { maxBatchDelay: 500 } }); | ||
|
||
const start = performance.now(); | ||
await testEnv.query(); | ||
await testEnv.query(); | ||
const report = await testEnv.reportSent; | ||
const end = performance.now(); | ||
expect(report.tracesPerQuery['# -\n{hello}']?.trace).toHaveLength(2); | ||
|
||
const elapsed = end - start; | ||
expect(elapsed).toBeGreaterThan(500); | ||
expect(elapsed).toBeLessThan(550); | ||
|
||
await testEnv[DisposableSymbols.asyncDispose](); | ||
}); | ||
|
||
it('should send traces when size threshold is reached', async () => { | ||
const testEnv = createTestEnv({ | ||
options: { maxBatchUncompressedSize: 150, maxBatchDelay: 500 }, | ||
}); | ||
|
||
const start = performance.now(); | ||
await testEnv.query(); | ||
await testEnv.query(); | ||
const report = await testEnv.reportSent; | ||
const end = performance.now(); | ||
|
||
expect(report.tracesPerQuery['# -\n{hello}']?.trace).toHaveLength(2); | ||
const elapsed = end - start; | ||
expect(elapsed).toBeLessThan(500); | ||
|
||
await testEnv[DisposableSymbols.asyncDispose](); | ||
}); | ||
|
||
it('should wait for all traces to be sent on shutdown', async () => { | ||
const testEnv = createTestEnv({ options: { maxBatchDelay: 500 } }); | ||
|
||
const start = performance.now(); | ||
await testEnv.query(); | ||
|
||
await testEnv[DisposableSymbols.asyncDispose](); | ||
const report = await testEnv.reportSent; | ||
const end = performance.now(); | ||
|
||
expect(report.tracesPerQuery['# -\n{hello}']?.trace).toHaveLength(1); | ||
|
||
const elapsed = end - start; | ||
expect(elapsed).toBeLessThan(500); | ||
await testEnv[DisposableSymbols.asyncDispose](); | ||
}); | ||
|
||
it('should not leak trace sending promises', async () => { | ||
let reporter: Reporter; | ||
const testEnv = createTestEnv({ | ||
options: { | ||
alwaysSend: true, | ||
reporter: (...args) => { | ||
reporter = new Reporter(...args); | ||
return reporter; | ||
}, | ||
}, | ||
}); | ||
|
||
await testEnv.query(); | ||
await testEnv.reportSent; | ||
// @ts-expect-error Accessing a private field | ||
expect(reporter!.sending).toHaveLength(0); | ||
|
||
await testEnv[DisposableSymbols.asyncDispose](); | ||
}); | ||
}); | ||
|
||
function createTestEnv( | ||
options: { graphosFetch?: typeof fetch; options?: ApolloUsageReportOptions } = {}, | ||
) { | ||
const reportSent = createDeferredPromise<Report>(); | ||
const yoga = createYoga({ | ||
schema, | ||
plugins: [ | ||
useApolloUsageReport({ | ||
graphRef: 'graphref', | ||
apiKey: 'apikey', | ||
...options.options, | ||
}), | ||
], | ||
maskedErrors: false, | ||
fetchAPI: { | ||
fetch: async (url, init) => { | ||
if (url.toString().includes('usage-reporting.api.apollographql.com')) { | ||
try { | ||
const bodyStream = init?.body as ReadableStream; | ||
const body = await streamToUint8Array( | ||
bodyStream.pipeThrough(new DecompressionStream('gzip')), | ||
); | ||
reportSent.resolve(Report.decode(body)); | ||
|
||
return options.graphosFetch | ||
? options.graphosFetch(url, init) | ||
: new Response(null, { status: 200 }); | ||
} catch (err) { | ||
reportSent.reject(err); | ||
} | ||
} | ||
return fetch(url, init); | ||
}, | ||
}, | ||
}); | ||
|
||
return { | ||
yoga, | ||
reportSent: reportSent.promise, | ||
query: (query = '{ hello }', operationName?: string) => { | ||
return yoga.fetch('http://yoga/graphql', { | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'apollo-federation-include-trace': 'ftv1', | ||
}, | ||
method: 'POST', | ||
body: JSON.stringify({ query, operationName }), | ||
}); | ||
}, | ||
[DisposableSymbols.asyncDispose]: () => yoga.dispose() as Promise<void>, | ||
}; | ||
} | ||
|
||
const schema = createSchema({ | ||
typeDefs: /* GraphQL */ ` | ||
type Query { | ||
hello: String! | ||
boom: String! | ||
person: Person! | ||
people: [Person!]! | ||
} | ||
type Subscription { | ||
hello: String! | ||
} | ||
type Person { | ||
name: String! | ||
} | ||
`, | ||
resolvers: { | ||
Query: { | ||
async hello() { | ||
return 'world'; | ||
}, | ||
async boom() { | ||
throw new Error('bam'); | ||
}, | ||
async person() { | ||
return { name: 'John' }; | ||
}, | ||
async people() { | ||
return [{ name: 'John' }, { name: 'Jane' }]; | ||
}, | ||
}, | ||
Subscription: { | ||
hello: { | ||
async *subscribe() { | ||
yield { hello: 'world' }; | ||
}, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
async function streamToUint8Array(stream: ReadableStream): Promise<Uint8Array> { | ||
const chunks = []; | ||
let size = 0; | ||
for await (const chunk of stream) { | ||
size += chunk.length; | ||
chunks.push(chunk); | ||
} | ||
|
||
const result = new Uint8Array(size); | ||
let offset = 0; | ||
for (const chunk of chunks) { | ||
result.set(chunk, offset); | ||
offset += chunk.length; | ||
} | ||
|
||
return result; | ||
} |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@EmrysMyrddin FYI: I just noticed that this PR was merged with this alpha version in
package.json
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh yes you are right, I have to release this before merging. Thank you !