André Jarboe II
André Jarboe II

Reputation: 848

How do I turn a sanity io document field that is an array of objects to an array of strings

I have a recipes.js document that has a tag field:

    {
      name: 'tags',
      title: 'Tags',
      type: 'array',
      of: [{ type: 'reference', to: { type: 'tags' } }],
      options: {
        layout: 'tags',
      },
    },

it references another document called tags.js:

export default {
  name: 'tags',
  type: 'document',
  title: 'Tags',
  fields: [
    {
      name: 'title',
      title: 'Title',
      type: 'string',
    },
  ],
};

I am trying to add the tags to each recipe as an array of strings in graphql. such as:

"tags": ['dinner', 'lunch', 'ribs']

but instead, I get an array of objects:

          "tags": [
            {
              "title": "breakfast"
            },
            {
              "title": "pancakes"
            },
            {
              "title": "food"
            }
          ]

How can I tell sanity all I want is each one to be added in as a string so it is an array of strings and not objects.

Upvotes: 0

Views: 2328

Answers (1)

Alexander Staubo
Alexander Staubo

Reputation: 3365

Sanity's GraphQL API does not support transforming the data structure into something else. If you want to do that, you will have to use GROQ.

Upvotes: 1

Related Questions