GraphQL 不允许在 NodeJS 中插入数组对象

Posted

技术标签:

【中文标题】GraphQL 不允许在 NodeJS 中插入数组对象【英文标题】:GraphQL does not allow to insert array objects in NodeJS 【发布时间】:2019-12-04 22:39:08 【问题描述】:

不幸的是,插入数组对象时出错。代码在这里

GraphQL 架构

type Member 
  _id: ID!
  member_id: Int!
  first_name: String!
  last_name: String
  username: String
  date: String


input MemberInput 
  member_id: Int!
  first_name: String!
  last_name: String
  username: String
  date: String


type RootQuery 
  groups: [Group!]!
  members: [Member!]!
  member(member_id: Int!): Member!

type RootMutation 
  createGroup(groupInput: GroupInput): Group
  createMember(memberInput: [MemberInput]): Member

schema 
  query: RootQuery
  mutation: RootMutation

制作数组对象

const mutatedAdminsDetails = groupAdminsFull.map(groupAdmin => 
  const rObj = 
    member_id: groupAdmin.id,
    first_name: groupAdmin.first_name,
    last_name: groupAdmin.last_name || '',
    username: groupAdmin.username || '',
    date: new Date().toISOString(),
  ;
  return rObj;
);

数组对象mutatedAdminsDetails

[
  
    member_id: 152356245,
    first_name: 'Subhash',
    last_name: 'Akbar',
    username: 'subhashbhai',
    date: '2019-07-26T14:39:01.314Z'
  ,
  
    member_id: 12312322,
    first_name: 'Shreef',
    last_name: '',
    username: '',
    date: '2019-07-26T14:39:01.314Z'
  
]

GraphQL 变异查询

const requestAdminBody = 
  query: `
      mutation 
        createMember(memberInput:
          [$mutatedAdminsDetails]
        ) 
          _id
          member_id
          first_name:
        
      
    `
;

API 请求

const insertAdmins = await fetch('http://localhost:3000/graphql', 
  method: 'POST',
  body: JSON.stringify(requestAdminBody),
  headers: 
    'Content-Type': 'application/json',
  
)
  .then(res => 
    return res.json();
  )
  .then(resData =>
    resData
  )
  .catch(err => err )

终端出错

【问题讨论】:

对于第二个错误group_id doesn't exist in the Member's schema definition @VladMamaev 已更改,现在出现此错误 errors: [ message: 'Syntax Error: Expected Name, found [', locations: [Array] ] @Fraction 修正了错字 【参考方案1】:

尝试将您的突变更新为:

const requestAdminBody = 
  query: `
      mutation CreateMember($memberInput: [MemberInput])
        createMember(memberInput: $memberInput) 
          _id
          first_name
        
      
    `,
  variables:  memberInput: mutatedAdminsDetails 
;

【讨论】:

收到此错误 errors: [ message: 'Must provide query string.' ] 不幸的是现在得到这个错误errors: [ message: 'Unknown type "memberInput". Did you mean "MemberInput" or "Member"?', locations: [Array] , message: 'Cannot query field "name" on type ' + '"Member". Did you mean "date" or ' + '"username"?', locations: [Array] ] 消息告诉你答案,我已经更新了我的答案^^ 收到此错误 errors: [ message: 'Cannot return null for non-nullable field Member._id.', locations: [Array], path: [Array] ], data: createMember: null 有什么想法吗? 我认为你没有从你的数据库中返回创建的成员,你可以尝试类似:const doc = await Member.create(member); return Member.findById(doc._id)

以上是关于GraphQL 不允许在 NodeJS 中插入数组对象的主要内容,如果未能解决你的问题,请参考以下文章

ReactJS - NodeJS - 在 AWS Lambda 上连接到 Graphql 的问题

如何使用 GraphQL 在 MERN 应用程序的 MongoDB 模式中插入数组字段?

如何使用GraphQL在MERN应用程序的MongoDB模式中插入数组字段?

如何在类型定义中为 GraphQL 字段定义数组数据类型

在数组 mongoose 中插入元素在 NodeJS 中创建 Object

Nodejs,mongodb在插入许多后更新数组