如何解决在其他架构SDL上共享相同的graphql架构类型定义
在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 举报,一经查实,本站将立刻删除。