Graphql 有没有一种方法可以从不同的gql文件声明类型?
我一直在努力使这一点起作用。我目前正在学习Apollo服务器和GraphQL。我看了不同的纪录片,但似乎找不到答案。这就是我要做的 我有usertypes.js,下面是代码片段:Graphql 有没有一种方法可以从不同的gql文件声明类型?,graphql,apollo-server,graphql-schema,Graphql,Apollo Server,Graphql Schema,我一直在努力使这一点起作用。我目前正在学习Apollo服务器和GraphQL。我看了不同的纪录片,但似乎找不到答案。这就是我要做的 我有usertypes.js,下面是代码片段: const { gql } = require('apollo-server'); const UserTypeDefs = gql` type User { _id: ID!, username: String!, password: String!, } `; module.ex
const { gql } = require('apollo-server');
const UserTypeDefs = gql`
type User {
_id: ID!,
username: String!,
password: String!,
}
`;
module.exports = UserTypeDefs;
在我的logintypes.js上:
const { gql } = require('apollo-server');
const { UserTypeDefs } = require('./usertypes.js')
const LoginTypeDefs = gql`
type LoginResponse {
user: UserTypeDefs.User //this is not working. :(
}
extend type Mutation {
login(username: String!, password: String!): LoginResponse!
}
`;
module.exports = LoginTypeDefs;
感谢您的帮助。我不想在我的logintypes.js中再次声明我的类型User
提前感谢。无需导入其他模块。只需按名称引用类型:
type LoginResponse {
user: User
}
然后,在创建ApolloServer
实例时,将typeDefs作为数组传入:
const userTypeDefs = require(...)
const loginTypeDefs = require(...)
const apollo = new ApolloServer({
typeDefs: [userTypeDefs, loginTypeDefs],
...
})
不需要导入其他模块。只需按名称引用类型:
type LoginResponse {
user: User
}
然后,在创建ApolloServer
实例时,将typeDefs作为数组传入:
const userTypeDefs = require(...)
const loginTypeDefs = require(...)
const apollo = new ApolloServer({
typeDefs: [userTypeDefs, loginTypeDefs],
...
})
你有什么错误吗?您如何定义“不工作”?只要您创建一个由
用户
和登录
组成的可执行模式
,就不必重新声明任何类型,据我所知,就不会有任何错误。这里有一篇文章可以帮助您理解如何做到这一点:我遵循了您提供的链接,我能够解决我的问题。我删除了const{gql}=require('apollo-server')然后将我的typedef声明为字符串,而不是gql``;谢谢很高兴知道你解决了。你有什么错误吗?您如何定义“不工作”
?只要您创建一个由用户
和登录
组成的可执行模式
,就不必重新声明任何类型,据我所知,就不会有任何错误。这里有一篇文章可以帮助您理解如何做到这一点:我遵循了您提供的链接,我能够解决我的问题。我删除了const{gql}=require('apollo-server')然后将我的typedef声明为字符串,而不是gql``;谢谢很高兴知道你解决了。玩得高兴