[英]How to get GraphQL subschema to delegateToSchema
无法确定如何/在何处获取subschema
的子delegateToSchema
。
我在单独的文件中有 2 个模式,我正在使用mergeTypeDefs
合并它们:
# booking schema
type Booking {
id
name
}
type Query {
booking(id: Int!): Booking
}
和另一个模式:
# user schema
type User {
id: Int!
name: String!
bookingId: Int!
booking: Booking
}
type Query {
users: [User]
}
我想使用 delegateToSchema 所以我不必为我的User
类型手动解析booking
字段,所以我在我的解析器中尝试这样做:
export const resolvers = {
Query: { ... }
User: {
booking: (parent, args, context, info) => delegateToSchema({
schema: subschema // <<< how do I get this?
operation: 'query',
fieldName: 'booking',
args: { id: parent.booking_id },
context,
info
})
}
}
我曾尝试使用loadSchema
但没有成功。 注意:所有 3 个都在单独的文件中
您一定是在某处创建模式,不是吗? 您要么直接定义它(如此处):
let postSchema = makeExecutableSchema({
typeDefs: /* GraphQL */ `
type Post {
id: ID!
text: String
userId: ID!
}
type Query {
postById(id: ID!): Post
postsByUserId(userId: ID!): [Post!]!
}
`
})
或者您从远程服务器加载它,如下所示:
const executor: AsyncExecutor = async ({ document, variables }) => {
const query = print(document)
const fetchResult = await fetch('http://example.com/graphql', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ query, variables })
})
return fetchResult.json()
}
export default async () => {
const schema = wrapSchema({
schema: await introspectSchema(executor),
executor
})
return schema
}
在这种情况下,返回模式的是await introspectSchema(executor)
。
如果你不做这些中的任何一个......好吧,你可能可以,对吧?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.