-
-
Notifications
You must be signed in to change notification settings - Fork 816
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
Add a way to delegate to a non-root field #543
Comments
@stubailo did you ever find a solution to this question? I have the same need but can't find any documentation. |
Having this same issue. Was anyone able to figure this out? |
I have the same issue, I can be wrong, but if you know how resolve this field, you can resolve inside the merging resolver.
|
Ah ok that makes sense. Thanks! |
I'm facing this issue as well. Would be great to be able to pass a full query + fragment to the delegate method that allows us to query a nested field. Maybe something like this (based on the example https://www.apollographql.com/docs/graphql-tools/schema-stitching.html#basic-example): Chirp_Chirp: {
author: {
fragment: `... on Chirp { authorId }`,
resolve(chirp, args, context, info, chirpAuthorFragment) {
return info.mergeInfo.delegateToSchema({
schema: authorSchema,
query: gql`
query User($token: String, $authorId: String) {
viewer(token: $token) {
userById(id: $authorId) {
...ChirpAuthor
}
}
}
${chirpAuthorFragment}
`,
variables: {
token: context.token,
authorId: chirp.authorId
},
context,
info,
});
},
},
}, |
You can totally do this already with Transforms. The documentation doesn't really make sense around it: https://www.apollographql.com/docs/graphql-tools/schema-transforms.html#Modifying-types But if you look at the tests, it may make a bit more sense: As far as your specific example, I haven't tried it yet, but it's something like this:
|
@gviligvili any chance you'd repost this to stack overflow? It'd be great to get the rep for this. Apparently a bunch of people have been having this particular issue |
@dncrews Thank you; this is a huge help. For others reading, the return value of the In the above example, the Here is a more complete example which is similar to what I'm doing in my application, using additional schema transforms too: https://gist.github.com/elliottsj/fa7042c0588ea6df1bc2f02f583d1100 |
New TransformQuery transform replaces WrapQuery and ExtractField and should do the job, folded into #1306 . |
This is a rudimentary working version of a nested field transform: /* eslint-disable @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-return */
import type { Transform, DelegationContext } from '@graphql-tools/delegate'
import type { ExecutionRequest, ExecutionResult } from '@graphql-tools/utils'
import type { DocumentNode, FieldNode } from 'graphql'
import { visit, Kind } from 'graphql'
/**
* Nested field delegation transform.
*
* i.e.:
* ```
* await delegateToSchema({
* schema: subschema,
* operation: 'query',
* fieldName: 'some.nested.field',
* context,
* info,
* transforms: [new NestedDelegationTransform()],
* })
* ```
*/
class NestedDelegationTransform implements Transform {
/**
* Transform document to open nested field (containing `.`).
*/
transformRequest(request: ExecutionRequest) {
const document: DocumentNode = visit(request.document, {
[Kind.FIELD]: {
enter: (node) => {
const path = node.name.value.split('.')
// Skip when not nested field name.
if (path.length === 1) {
return node
}
const first = path[0]
const rest = path.slice(1).join('.')
const child: FieldNode = { ...node, name: { ...node.name, value: rest } }
const parent: FieldNode = {
...node,
name: { ...node.name, value: first },
selectionSet: {
kind: Kind.SELECTION_SET,
selections: [child],
},
}
return parent
},
},
})
return { ...request, document }
}
/**
* Transform result data by reconstructing nested field (containing `.`).
*/
transformResult(result: ExecutionResult, delegation: DelegationContext) {
// Safeguard for query execution errors.
if (!result.data) {
return result
}
const path = delegation.fieldName.split('.')
const value = path.reduce((curr, name) => {
const nested = curr[name]
// No result, stop traversing.
if (typeof nested === 'undefined' || nested === null) return null
// If is an array, simply get first element (how to handle this?)
if (Array.isArray(nested)) return nested[0]
return nested
}, result.data)
return { ...result, data: { [delegation.fieldName]: value } }
}
}
export { NestedDelegationTransform } |
ver : 2.2.1
some micro service has this queries :
when I try to stitch him, in the resolver I can't use "getMeetingsByDate" because his not defined in the microservice root queries.
I get:
so I tried instead of :
to do :
yet still I can't find a way to reach a subquery, and couldn't find any documentation on it.
The text was updated successfully, but these errors were encountered: