Reputation: 1545
I don't think I am doing things the proper graphQL way, but I am not sure how to achieve it in graphQL.
I have the following schema, that is then uploaded to FaunaDB, and they do some magic to turn it into a usable graphQL api endpoint.
type Catalog {
decor: Boolean
clothing: Boolean
supplies: Boolean
furniture: Boolean
owner: User
}
type Decor {
description: String
pieces: Int
purchaser: String
alterations: Boolean
cost: Int
purchaseDate: Date
category: String
image: String
itemNum: Int
owner: User!
visible: Boolean
}
type DecorSheet {
sheetName: String
refIdArray: String
owner: User!
}
type User {
email: String! @unique
catalog: Catalog
decor: [Decor!] @relation
decorSheet: [DecorSheet!] @relation
}
type Query {
getMultipleDecors(DecorId: [ID!]): [Decor]
@resolver(name: "get_multiple_decors")
}
I have the need to Create a DecorSheet type, I need the refIdArray, to be an array of objects like so
[
{
"refId0": "293164663883956749"
},
{
"refId1": "293526016787218952"
}
]
I have the following mutation gql for apollo to trigger
const CREATE_DECOR_SHEET = gql`
mutation CreateDecorSheet(
$ownerID: ID!
# $description: String!
$sheetName: String
$refIdArray: String!
) {
createDecorSheet(data: { sheetName: $sheetName, refIdArray: $refIdArray, owner: { connect: $ownerID } }) {
refIdArray
sheetName
_id
}
}
`;
and I am calling it here
const res = await createDecorSheet({
variables: {
ownerID: user.id,
// ...dataMassage,
sheetName: dataForm.sheetName,
refIdArray: JSON.stringify(chosenValues), //A WAY TO DO THIS GRAPHQL STYLE
},
}).catch(console.error);
See currently I am stringifying the chosenValues which is the same values I listed above of the refId and Id objects in the array.
Is there a better way to do this, do I need to write a custom mutation that you can pass an array of objects to refIdArray.
If anyone could help with this that would be great.
Thanks
Upvotes: 1
Views: 1816
Reputation: 1545
I ended up updating the schema to
type DecorSheet {
sheetName: String
refIdArray: [ID]
owner: User!
}
Then I just passed my array as is
const res = await createDecorSheet({
variables: {
ownerID: user.id,
// ...dataMassage,
sheetName: dataForm.sheetName,
refIdArray: chosenValues, //array here
},
}).catch(console.error);
and the gql mutation looked as so
const CREATE_DECOR_SHEET = gql`
mutation CreateDecorSheet(
$ownerID: ID!
# $description: String!
$sheetName: String
$refIdArray: [ID]
) {
createDecorSheet(
data: {
sheetName: $sheetName
refIdArray: $refIdArray
owner: { connect: $ownerID }
}
) {
refIdArray
sheetName
_id
}
}
`;
Upvotes: 2