-
I'd like to put GitHub's schema inside my own so I can make a call that looks like this: {
viewer {
accessToken
github {
# This part is from GitHub
viewer {
bio
}
}
}
} My strategy is to rename all the GitHub types with a I've tried both const parentSchema = makeExecutableSchema({
typeDefs: `
type User {
accessToken: String!
}
type Query {
viewer: User
}
`,
resolvers: {
Query: {
viewer: () => {
return {
accessToken: 'gho_YOURTOKENHERE'
}
}
}
}
})
const rawGitHubSchema = makeExecutableSchema({
typeDefs: schema.idl
})
const executor = async ({document, variables, context}) => {
const {accessToken} = context
const query = print(document)
const fetchResult = await fetch('https://api.github.com/graphql', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${accessToken}`,
Accept: 'application/json'
},
body: JSON.stringify({query, variables})
})
return fetchResult.json()
}
const prefixGitHub = (name: string) => `_GitHub${name}`
const transforms = [new RenameRootTypes(prefixGitHub), new RenameTypes(prefixGitHub)]
const githubSubSchemaConfig = {
schema: rawGitHubSchema,
executor,
transforms
}
const mergedSchema = stitchSchemas({
subschemas: [
githubSubSchemaConfig,
{
schema: parentSchema
}
],
mergeTypes: false,
typeDefs: `
extend type User {
github: _GitHubQuery
}
`,
resolvers: {
User: {
github: {
selectionSet: `{ accessToken }`,
resolve: async ({accessToken}, args, context, info) => {
return delegateToSchema({
schema: githubSubSchemaConfig,
operation: 'query',
fieldName: 'relay',
args,
context: {
...context,
accessToken
},
info
})
}
}
}
}
}) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 14 replies
-
Welcome! one thing, i don’t believe you want to rename the root types, all root types should be the same so things merge properly. That transform is for subschemas that don’t use Query. Not sure what GitHub uses. also not sure why you are getting that exact error message.... Note that the WrapType transform can be used on a remote schema to namespace a root type’s fields under a field rather than using manual delegateToSchema |
Beta Was this translation helpful? Give feedback.
-
Right, but when you are nesting, how do you ensure userId is present if was
not requested
…On Thu, 29 Apr 2021 at 20:43 Matt Krick ***@***.***> wrote:
Thank you!
I wasn't able to reuse the batching executor just because I did some
special error handling like path-based filtering.
For the access token, the DX is to accept a
GraphQLFieldResolver<ParentType> callback & let the dev decide how to get
it. For example, if I'm nesting the API on the User object, the callback
might be (source) => getTokenForUserId(source.userId)
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#2878 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AA7LAYCH2XOFG54G3E25LLLTLGLEJANCNFSM43WAAOEQ>
.
|
Beta Was this translation helpful? Give feedback.
Welcome!
one thing, i don’t believe you want to rename the root types, all root types should be the same so things merge properly.
That transform is for subschemas that don’t use Query. Not sure what GitHub uses.
also not sure why you are getting that exact error message....
Note that the WrapType transform can be used on a remote schema to namespace a root type’s fields under a field rather than using manual delegateToSchema