-
Notifications
You must be signed in to change notification settings - Fork 349
Auto merge types from subscriptions in additional type defs #8736
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
Open
enisdenjo
wants to merge
21
commits into
master
Choose a base branch
from
automatch
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
a1c38f6
finds available resolver
enisdenjo 6e5869f
auto resolve
enisdenjo fa2b32b
unnecessary comment
enisdenjo 2425fda
thanks eslint
enisdenjo 2b8cd09
more tests
enisdenjo fd7fccf
missing quotes
enisdenjo 3f113c0
can be missing
enisdenjo 67e5d1d
remember
enisdenjo fb5ee47
maybe 2
enisdenjo 75b2e45
rename
enisdenjo 060f91b
undef ctx
enisdenjo 1a54be0
test
enisdenjo 16ff916
selsetofdata
enisdenjo 8d48608
await using
enisdenjo 8a20760
fragment and stuff
enisdenjo 87360c6
skip test
enisdenjo c12cf69
consider arrays
enisdenjo 57dbc89
consider arrays and test
enisdenjo c197ac2
typo
enisdenjo 68f0190
diff selection set
enisdenjo 2edabf4
Revert "diff selection set"
enisdenjo 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 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,32 @@ | ||
--- | ||
'@graphql-mesh/utils': patch | ||
--- | ||
|
||
Auto merge types from subscriptions in additional type defs | ||
|
||
This means that the `sourceName` directive does not need to be provided to the `@resolveTo` directive, instead the resolver will automatically find the subgraph that requires fields available in the subscription event. | ||
|
||
```diff | ||
import { defineConfig, loadGraphQLHTTPSubgraph } from '@graphql-mesh/compose-cli' | ||
|
||
export const composeConfig = defineConfig({ | ||
subgraphs: [ | ||
{ | ||
sourceHandler: loadGraphQLHTTPSubgraph('products', { | ||
endpoint: `http://localhost:3000/graphql` | ||
}) | ||
} | ||
], | ||
additionalTypeDefs: /* GraphQL */ ` | ||
extend schema { | ||
subscription: Subscription | ||
} | ||
type Subscription { | ||
newProduct: Product! @resolveTo( | ||
pubsubTopic: "new_product" | ||
- sourceName: "products" | ||
) | ||
} | ||
` | ||
}) | ||
``` |
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-mesh/utils': patch | ||
--- | ||
|
||
The context can be undefined while resolving additional resolvers |
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
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
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
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,109 @@ | ||
/* eslint-disable no-labels */ | ||
import { Kind, type SelectionNode, type SelectionSetNode } from 'graphql'; | ||
|
||
/** | ||
* Checks recursively whether all of the fields of {@link requiredSelSet} exist in {@link selSet} | ||
*/ | ||
export function containsSelectionSet( | ||
requiredSelSet: SelectionSetNode, | ||
selSet: SelectionSetNode, | ||
): boolean { | ||
ReqLoop: for (const reqSel of requiredSelSet.selections) { | ||
switch (reqSel.kind) { | ||
case Kind.FIELD: { | ||
for (const sel of selSet.selections) { | ||
// required field exists in the selection set | ||
if (sel.kind === Kind.FIELD && sel.name.value === reqSel.name.value) { | ||
if (!reqSel.selectionSet && !sel.selectionSet) { | ||
// they're both scalar fields, continue to next required field | ||
continue ReqLoop; | ||
} | ||
if (reqSel.selectionSet && !sel.selectionSet) { | ||
// required field is an object, but the selection under the same name is scalar | ||
return false; | ||
} | ||
if (!reqSel.selectionSet && sel.selectionSet) { | ||
// required field is a scalar, but the selection under the same name is object | ||
return false; | ||
} | ||
// they're both objects | ||
if (containsSelectionSet(reqSel.selectionSet!, sel.selectionSet!)) { | ||
// and they recursively contain all required fields | ||
continue ReqLoop; | ||
} | ||
} | ||
} | ||
// no matches found | ||
return false; | ||
} | ||
case Kind.INLINE_FRAGMENT: { | ||
for (const sel of selSet.selections) { | ||
if (sel.kind !== Kind.INLINE_FRAGMENT) { | ||
continue; | ||
} | ||
if ( | ||
sel.typeCondition?.name.value && | ||
reqSel.typeCondition?.name.value && | ||
sel.typeCondition.name.value === reqSel.typeCondition.name.value | ||
) { | ||
// both have matching type conditions | ||
if (containsSelectionSet(reqSel.selectionSet, sel.selectionSet)) { | ||
// and they recursively contain all required fields | ||
continue ReqLoop; | ||
} | ||
} | ||
if (!sel.typeCondition?.name.value && !reqSel.typeCondition?.name.value) { | ||
// neither have a type condition, just check the selection sets | ||
if (containsSelectionSet(reqSel.selectionSet, sel.selectionSet)) { | ||
// and they recursively contain all required fields | ||
continue ReqLoop; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
default: | ||
// no other field kind is supported, like fragment spreads or definitions | ||
return false; | ||
} | ||
} | ||
// all fields matched | ||
return true; | ||
} | ||
|
||
export function selectionSetOfData(data: Record<string, unknown>): SelectionSetNode { | ||
const selSet = { | ||
kind: Kind.SELECTION_SET, | ||
selections: [] as SelectionNode[], | ||
} as const; | ||
for (const fieldName of Object.keys(data)) { | ||
const fieldValue = data[fieldName]; | ||
const selNode: SelectionNode = { | ||
kind: Kind.FIELD, | ||
name: { kind: Kind.NAME, value: fieldName }, | ||
}; | ||
if (fieldValue && typeof fieldValue === 'object') { | ||
if (Array.isArray(fieldValue)) { | ||
// we assume that all items in the array are of the same shape, so we look at the first one | ||
const firstItem = fieldValue[0]; | ||
if (firstItem && typeof firstItem === 'object') { | ||
selSet.selections.push({ | ||
...selNode, | ||
selectionSet: selectionSetOfData(firstItem as Record<string, unknown>), | ||
}); | ||
} else { | ||
// is an array of scalars | ||
selSet.selections.push(selNode); | ||
} | ||
} else { | ||
selSet.selections.push({ | ||
...selNode, | ||
selectionSet: selectionSetOfData(fieldValue as Record<string, unknown>), | ||
}); | ||
} | ||
} else { | ||
selSet.selections.push(selNode); | ||
} | ||
} | ||
return selSet; | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.