问题描述
在GraphQL(阿波罗服务器)中,我们可以为schema
构造函数使用typedefs
或resolvers
&ApolloServer
。因此,在代码拆分的类型定义中,我们可以使用模板文字
const sampleStr = `
type User {
_id: ID!}`
然后我们只在typedef中使用它
const typeDefs = gql`
${smapleStr}
type Query{
user: User!
}
`
这很好用,但不推荐。或者使用graphql-tools
,我们可以在服务器中使用makeExecutableSchema
和mergeSchema
函数进行 schema-stiching ,这可以完美地模块化我们的服务器。 但是,假设我有一个名为type Post
的{/ {1}} typeDefs声明的postType
,如下所示...
post.js
现在在我的//post.js
const postType= gql`
type Query{
...queries
}
type Mutation{
addPost(...other args): Flash!
}
# //the type I wanna share to other schema
type Flash{
success: Boolean!
}
`
模块中,我要使用other.js
定义的Flash
,如下所示。
postType
是的,它不起作用。我的第一种方法是,我可以将//other.js
const otherTypeDefs = gql`
type Query{
test: Flash!
}
`
保留在字符串中,并从Flash
进行导出和导入,但这不是一个好方法。
现在,我该怎么做才能在
post.js
的{{1}}模式中使用Flash
或其他更大的type
?
我可能会通过扩展它来在其他架构中添加额外的字段,或者将其用作另一个模块/架构中另一个类型的嵌套类型。
注意:我使用的是other.js
,默认情况下包含post.js
。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)