-
Notifications
You must be signed in to change notification settings - Fork 10
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
feat: APQ between subgraph and gateway #313
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
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,27 @@ | ||
--- | ||
'@graphql-tools/executor-http': minor | ||
'@graphql-mesh/transport-http': patch | ||
--- | ||
|
||
Automatic Persisted Queries support for upstream requests | ||
|
||
For HTTP Executor; | ||
```ts | ||
buildHTTPExecutor({ | ||
// ... | ||
apq: true, | ||
}) | ||
``` | ||
|
||
For Gateway Configuration; | ||
```ts | ||
export const gatewayConfig = defineConfig({ | ||
transportEntries: { | ||
'*': { | ||
options: { | ||
apq: true | ||
} | ||
} | ||
}, | ||
}) | ||
``` |
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,86 @@ | ||
import { createTenv } from '@internal/e2e'; | ||
import { stripIgnoredCharacters } from 'graphql'; | ||
import { describe, expect, it } from 'vitest'; | ||
import { hashSHA256 } from '../../packages/executors/http/src/utils'; | ||
|
||
const { service, gateway } = createTenv(__dirname); | ||
|
||
describe('APQ to the upstream', () => { | ||
it('works', async () => { | ||
await using gw = await gateway({ | ||
supergraph: { | ||
with: 'mesh', | ||
services: [await service('greetings')], | ||
}, | ||
}); | ||
const query = stripIgnoredCharacters(/* GraphQL */ ` | ||
{ | ||
__typename | ||
hello | ||
} | ||
`); | ||
const sha256Hash = await hashSHA256(query); | ||
await expect(gw.execute({ query })).resolves.toEqual({ | ||
data: { | ||
__typename: 'Query', | ||
hello: 'world', | ||
}, | ||
}); | ||
// First it sends the request with query | ||
expect(gw.getStd('both')).toContain( | ||
`fetch 1 ${JSON.stringify({ | ||
extensions: { | ||
persistedQuery: { | ||
version: 1, | ||
sha256Hash, | ||
}, | ||
}, | ||
})}`, | ||
); | ||
// Then it sends the query with the hash | ||
// In the following requests the query won't be needed | ||
expect(gw.getStd('both')).toContain( | ||
`fetch 2 ${JSON.stringify({ | ||
query, | ||
extensions: { | ||
persistedQuery: { | ||
version: 1, | ||
sha256Hash, | ||
}, | ||
}, | ||
})}`, | ||
); | ||
|
||
await expect(gw.execute({ query })).resolves.toEqual({ | ||
data: { | ||
__typename: 'Query', | ||
hello: 'world', | ||
}, | ||
}); | ||
|
||
// The query is not sent again | ||
expect(gw.getStd('both')).toContain( | ||
`fetch 3 ${JSON.stringify({ | ||
extensions: { | ||
persistedQuery: { | ||
version: 1, | ||
sha256Hash, | ||
}, | ||
}, | ||
})}`, | ||
); | ||
|
||
// The query is not sent again | ||
expect(gw.getStd('both')).not.toContain( | ||
`fetch 4 ${JSON.stringify({ | ||
query, | ||
extensions: { | ||
persistedQuery: { | ||
version: 1, | ||
sha256Hash, | ||
}, | ||
}, | ||
})}`, | ||
); | ||
}); | ||
}); |
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,20 @@ | ||
import { defineConfig } from '@graphql-hive/gateway'; | ||
|
||
let fetchCnt = 0; | ||
export const gatewayConfig = defineConfig({ | ||
transportEntries: { | ||
greetings: { | ||
options: { | ||
apq: true, | ||
}, | ||
}, | ||
}, | ||
plugins: () => [ | ||
{ | ||
onFetch({ options }) { | ||
fetchCnt++; | ||
process.stdout.write(`fetch ${fetchCnt} ${options.body}\n`); | ||
}, | ||
}, | ||
], | ||
}); |
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,17 @@ | ||
import { | ||
defineConfig, | ||
loadGraphQLHTTPSubgraph, | ||
} from '@graphql-mesh/compose-cli'; | ||
import { Opts } from '@internal/testing'; | ||
|
||
const opts = Opts(process.argv); | ||
|
||
export const composeConfig = defineConfig({ | ||
subgraphs: [ | ||
{ | ||
sourceHandler: loadGraphQLHTTPSubgraph('greetings', { | ||
endpoint: `http://localhost:${opts.getServicePort('greetings')}/graphql`, | ||
}), | ||
}, | ||
], | ||
}); |
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,10 @@ | ||
{ | ||
"name": "@e2e/apq-subgraphs", | ||
"private": true, | ||
"devDependencies": { | ||
"@apollo/server": "^4.11.2", | ||
"@graphql-mesh/compose-cli": "^1.2.13", | ||
"graphql": "^16.9.0", | ||
"tslib": "^2.8.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,25 @@ | ||
import { ApolloServer } from '@apollo/server'; | ||
import { startStandaloneServer } from '@apollo/server/standalone'; | ||
import { Opts } from '@internal/testing'; | ||
|
||
const opts = Opts(process.argv); | ||
|
||
const apolloServer = new ApolloServer({ | ||
typeDefs: /* GraphQL */ ` | ||
type Query { | ||
hello: String | ||
} | ||
`, | ||
resolvers: { | ||
Query: { | ||
hello: () => 'world', | ||
}, | ||
}, | ||
}); | ||
|
||
startStandaloneServer(apolloServer, { | ||
listen: { port: opts.getServicePort('greetings') }, | ||
}).catch((e) => { | ||
console.error(e); | ||
process.exit(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
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
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.
Transport entries is marked as an advanced feature today. Do we want to hide this behind this setting ?
Perhaps we can have a top level option instead ?
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.
They are not advanced. It is a way to configure the transport.
I don't think a top level option is a good idea because this feature is specific to HTTP transport.
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.
I don't think it's advanced, but that's what we are advertising in the documentation ^^'
And the way transport entries work is never actually explain. It's shortly explained in the Subscriptions page because it is needed to configure websocket/http callback subscription transports
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.
We should fix it then yes. I agree it is a bit low level but i think we should cover each feature individial rather than grouping them into one single transportEntries documentation. What do you think?