Solved

How to create a collection Available online GraphQl

waliby
Shopify Partner
8 0 3

I've tried the mutation to create a collection, It seems there is no argument to make the new collection available online, I saw that with the REST API, we can use the arg "published": true

 

Can you please explain me the way to do this with GraphQl ? 

 

Accepted Solution (1)

Zameer
Shopify Staff
297 31 90

This is an accepted solution.

Hey Waliby,

 

This is definitely also possible in GraphQL, it just requires a separate mutation.

 

First, you create the collection as you mentioned, using the collectionCreate mutation.

 

Next, you get the publication id where you want to publish the collection, in your case the Online Store. This can be done using the publications QueryRoot:

query{
  publications(first:10){
    edges{
      node{
        id
        name
      }
    }
  }
}

 

Lastly, you publish the collection using the publishablePublish mutation (not the greatest name):

mutation publishablePublish($id: ID!, $input: [PublicationInput!]!) {
  publishablePublish(id: $id, input: $input) {
    userErrors {
      field
      message
    }
  }
}

 

Here you have to provide an input variable with both the id of the collection you want to publish as the "id" key and then the id of the publication (Online Store) as "publicationId" key, both base64 encoded:

{
  "id": "{collection_id_base_64}",
  "input": [
    {"publicationId": "publication_id_base_64"}
  ]
}

This should allow you to create a collection and publish it appropriately in the Online Store. Let me know if you run into any issues!

To learn more visit the Shopify Help Center or the Community Blog.

View solution in original post

Replies 5 (5)

Zameer
Shopify Staff
297 31 90

This is an accepted solution.

Hey Waliby,

 

This is definitely also possible in GraphQL, it just requires a separate mutation.

 

First, you create the collection as you mentioned, using the collectionCreate mutation.

 

Next, you get the publication id where you want to publish the collection, in your case the Online Store. This can be done using the publications QueryRoot:

query{
  publications(first:10){
    edges{
      node{
        id
        name
      }
    }
  }
}

 

Lastly, you publish the collection using the publishablePublish mutation (not the greatest name):

mutation publishablePublish($id: ID!, $input: [PublicationInput!]!) {
  publishablePublish(id: $id, input: $input) {
    userErrors {
      field
      message
    }
  }
}

 

Here you have to provide an input variable with both the id of the collection you want to publish as the "id" key and then the id of the publication (Online Store) as "publicationId" key, both base64 encoded:

{
  "id": "{collection_id_base_64}",
  "input": [
    {"publicationId": "publication_id_base_64"}
  ]
}

This should allow you to create a collection and publish it appropriately in the Online Store. Let me know if you run into any issues!

To learn more visit the Shopify Help Center or the Community Blog.

waliby
Shopify Partner
8 0 3

Thank you so much, it works perfectly.

Tejas-Ambalia
Tourist
4 0 4

I am not able to get the collections list. I am getting the below error. I am using a private app on the Shopify Plus store. but, I am not seeing a scope which name is read_publications into the admin area. how can I give this permission to my private app?

 

Access denied for publications field. Required access: `read_publications` access scope.  This scope is currently available only to private apps installed on Shopify Plus stores.

 

waliby
Shopify Partner
8 0 3

Have you tried to pull a list of collection from the GraphQl app ?
If it works with a simple structure like:

query{
				collections(first:1){
					edges{
						cursor,
						node{
							legacyResourceId,
              title
						}
					}
  }
  }

read_products should be enough just to get a list.

1080
Shopify Partner
282 8 39

here is sample graphql query to create a collection 

mutation {
  collectionCreate(
    input: {
      title: "GQL New Collection",
      descriptionHtml: "This is a new collection", 
      templateSuffix: null, 
      image: {src: "https://cdn.shopify.com/s/files/1/0594/7804/8868/products/8cd561824439482e3cea5ba8e3a6e2f6.jpg"}
    }
  ) {
    collection {
      id
      title
    }
  }
}