1

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

Anders Kitson
  • 1,413
  • 6
  • 38
  • 98
  • 2
    just pass array of ids, like in the query ... it's resolver role to adapt input data to DB requirements (hidden implementation) or 'extract' to required data shape – xadm Mar 25 '21 at 22:24
  • So you can pass an array of id's as a string value? that doesn't sound right too me, but I will try. I think it will complain to me that it is not a string value though. I'll report back – Anders Kitson Mar 26 '21 at 01:49
  • I got `Variable '$refIdArray' expected value of type 'String!' but got: ["293164663883956749","293526016787218952"]` Do I need to change the schema to be `refIdArray: [id]` – Anders Kitson Mar 26 '21 at 01:56
  • Ok that worked. Thanks – Anders Kitson Mar 26 '21 at 02:01
  • 1
    @AndersKitson You should probably write your own answer with the solution then, it'll definitely help other! :) – Vadorequest Mar 27 '21 at 11:45

1 Answers1

2

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
      }
    }
  `;
Anders Kitson
  • 1,413
  • 6
  • 38
  • 98