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

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 )


终端错误

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

最佳答案

尝试将您的突变更新为:

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

07-28 09:10