在.graphql中封装突变

时间:2019-09-05 18:48:32

标签: node.js graphql apollo

Graphql只提供一种Main突变类型,就像我们有一种分解器一样。

我正在学习graphql并遇到这个问题

Graphql文件。

  1. root.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
}
  1. account.graphql
# 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
}

解析器文件。

  1. rootresolver.js
/*
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
    }
}
  1. account / resolver.js
/*
IMPORT'S.
*/
...


/*
RESOLVER
const Account = {
  WithMobileNumber: (__, __Context) => {...}
}


/*
EXPORT'S.
*/
module.exports = Account

Graphql查询

要运行以下查询,其中WithMobileNumber 将要处理变异。

mutation Mutation(
  $mobileNumber: String!
  $otp: String!
) {
 Account {
  Login {
   WithMobileNumber (
     mobileNumber: $mobileNumber 
     otp: $otp) {
      status
    }
   }
 }
}

结果:

{
  "data": {
    Account: null
  }
}

问题

  1. 如果我添加了帐户解析器,它将开始运行。
  2. 如果我将突变更改为如下所示的内容
type Mutation {
   WithMobileNumber(...): Status <--- Will going to work.
   UploadFile(...): Status <--- This Mutation work's
}

但是我不能像这样将所有项目变异都添加到一个地方,甚至我也可以嵌套变异,因为据我了解,graphql类型需要使用WithMobileNumber不能使用的解析器。该怎么办? 如何将每个项目块封装在不同的文件夹中,将其视为独立的graphl模块或如何构造.graphql文件,以便获得更好的体系结构?

0 个答案:

没有答案