Graphql只提供一种Main突变类型,就像我们有一种分解器一样。
我正在学习graphql并遇到这个问题
# import * from './account.graphql'
# Main mutation handler consider it as
# global Mutation object containing Other
# Mutation as a child and Those methods independently
# which servers to the whole API instead of small working.
# Example: Account can be considered as one single type in Graphql
# so we get something like Login etc which can nested inside Account type and use Account type in Query or Mutation type for connecting it to root node and do similarly for Handlers.
# UploadingFile is global service so put it directly in Mutation.
type Mutation {
AccountMutation: AccountMutation <--- Nested Mutation doesn't work
UploadFile(...): Status <--- This Mutation work's
}
# Account type for handling account-related type's
type Account {
_id: ID
fullName: String!
email: String!
mobileNumber: String
}
# AccountLogin mutation for handling
# account-related mutation's.
type AccountLoginMutation {
WithMobileNumber(...): Account <--- Want to run this..
WithEmailId(...): Account
}
type AccountRegisterMutation {
WithMobileNumber(...): Account
}
# Custom Mutation type for handling account
type AccountMuatation {
Login: AccountLoginMutation
Register: AccountRegisterMutation
}
/*
IMPORT'S.
*/
import Account from './Account/'
import UploadFileMutation from '../UploadFile'
/*
GLOBAL'S.
const GloblMutation = {
UploadFile: UploadFileMutation
}
/*
EXPORT'S.
*/
module.exports = {
..._.omit(Account, 'Mutation'),
'Mutation': {
'Account': Account['Mutation'],
...GlobalMutation
}
}
/*
IMPORT'S.
*/
...
/*
RESOLVER
const Account = {
WithMobileNumber: (__, __Context) => {...}
}
/*
EXPORT'S.
*/
module.exports = Account
要运行以下查询,其中WithMobileNumber 将要处理变异。
mutation Mutation(
$mobileNumber: String!
$otp: String!
) {
Account {
Login {
WithMobileNumber (
mobileNumber: $mobileNumber
otp: $otp) {
status
}
}
}
}
结果:
{
"data": {
Account: null
}
}
type Mutation {
WithMobileNumber(...): Status <--- Will going to work.
UploadFile(...): Status <--- This Mutation work's
}
但是我不能像这样将所有项目变异都添加到一个地方,甚至我也可以嵌套变异,因为据我了解,graphql类型需要使用WithMobileNumber不能使用的解析器。该怎么办? 如何将每个项目块封装在不同的文件夹中,将其视为独立的graphl模块或如何构造.graphql文件,以便获得更好的体系结构?