简体   繁体   中英

How to in GraphQL mutation pass an array to a field using FaunaDB

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM