1

Unfortunately getting error when insert array objects. Here the code

GraphQL Schema

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
}

Making array objects

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;
});

Array objects 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 mutation query

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

API request

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 )

Error in terminal

enter image description here

3
  • For the second error group_id doesn't exist in the Member's schema definition Commented Jul 26, 2019 at 15:10
  • @VladMamaev Changed, now getting this error { errors: [ { message: 'Syntax Error: Expected Name, found [', locations: [Array] } ] } Commented Jul 26, 2019 at 15:23
  • @Fraction fixed the typo Commented Jul 26, 2019 at 15:27

1 Answer 1

1

Try to update your mutation to:

const requestAdminBody = {
  query: `
      mutation CreateMember($memberInput: [MemberInput]){
        createMember(memberInput: $memberInput) {
          _id
          first_name
        }
      }
    `,
  variables: { memberInput: mutatedAdminsDetails }
};
Sign up to request clarification or add additional context in comments.

9 Comments

Getting this error { errors: [ { message: 'Must provide query string.' } ] }
unfortunately getting this error now 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] } ]
the message tells you the answer, I've updated my answer ^^
Getting this error{ errors: [ { message: 'Cannot return null for non-nullable field Member._id.', locations: [Array], path: [Array] } ], data: { createMember: null } } any idea about it ?
I think you don't return the created member from your db, you can try something like: const doc = await Member.create(member); return Member.findById(doc._id)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.