Update API calls in your app
Many API client libraries that you might use to call the Shopify Admin API support both REST and GraphQL. However, you need to make updates to your code that uses these libraries. Some updates that you need to make might include the following:
- Switching from a REST client to a GraphQL client
- Changing the API endpoint
- Updating the method
- Adding your query or mutation and specifying the fields to return
- Destructuring or digging into the response
You don't need to update your authentication or session handling to update from REST to GraphQL.
Use the following examples to understand the changes that you need to make for each API library.
This guide doesn't explain all of the changes that you need to make to your app to use GraphQL instead of REST. For example, you need to update your error handling and pagination logic. Learn more about considerations when migrating from REST to GraphQL.
shopify-api-js
Anchor link to section titled "shopify-api-js"The following examples show you how to update your calls to GraphQL when reading or writing data using the shopify-api-js library.
Reading data
Anchor link to section titled "Reading data"The following requests retrieve information about a product. To convert the REST version of this request to GraphQL, you need to make the following changes in your code.
Update | Description |
---|---|
Change the client type |
Remix: Instead of creating an Non-Remix: Instead of creating a |
Change the method | Instead of using the get method on the REST client or the find , all , or count method on the resource class, use the query method on the GraphQL client. |
Add a query |
Pass a query to the client, requesting the information that you want to retrieve. Instead of specifying the path to the resource (like If you're retrieving a resource by its ID, then use the GraphQL API global ID (GID) for the resource (like |
Destructure the response |
GraphQL responses are typically nested within a data object. The structure of the data mirrors the structure of the query. To make the data easier to work with, destructure the object that's returned. You can also access properties of the object using dot notation ( |
Writing data
Anchor link to section titled "Writing data"The following requests create a new product. To convert the REST version of this request to GraphQL, you need to make the following changes in your code.
Update | Description |
---|---|
Change the client type |
Remix: Instead of creating an Non-Remix: Instead of creating a |
Change the method | Instead of using the post , put , or delete method on the REST client, or the save or delete method on the resource class, use the query method on the GraphQL client. |
Add a mutation |
Instead of specifying the path to the resource (like |
Destructure the response |
GraphQL responses are typically nested within a data object, and the structure of the data often mirrors the structure of the mutation. To make the data easier to work with, destructure the object that's returned. You can also access properties of the object using dot notation ( |
Read userErrors |
Extract the userErrors array from the response. If the mutation wasn't completed, then this information is returned in this array. Learn about how error handling differs in GraphQL. |
shopify-api-ruby
Anchor link to section titled "shopify-api-ruby"The following examples show you how to update your calls to GraphQL when reading or writing data using the shopify-api-ruby library.
Reading data
Anchor link to section titled "Reading data"The following requests retrieve information about a product. To convert the REST version of this request to GraphQL, you need to make the following changes in your code.
Update | Description |
---|---|
Change the client type | Instead of creating a shopify.clients.Rest client or using REST resource classes like shopify.rest.Product.find , create a shopify.clients.GraphQL client. |
Change the method | Instead of using the get method on the REST client or the find , all , or count method on the resource class, use the query method on the GraphQL client. |
Add a query |
Pass a query to the client, requesting the information that you want to retrieve. Instead of specifying the path to a resource (like If you're retrieving a resource by its ID, then use the GraphQL API global ID (GID) for the resource (like |
Destructure the response |
GraphQL responses are typically nested within a data object. The structure of the data mirrors the structure of the query. To make the data easier to work with, destructure the object that's returned. You can also access properties of the object using bracket notation. In the provided Ruby example, the product data is accessed with |
Writing data
Anchor link to section titled "Writing data"The following requests create a new product. To convert the REST version of this request to GraphQL, you need to make the following changes in your code.
Update | Description |
---|---|
Change the client type | Instead of creating a ShopifyAPI::Clients::Rest::Admin client or using REST resource classes like ShopifyAPI::Product , create a ShopifyAPI::Clients::Graphql::Admin client. |
Change the method | Instead of using the post , put , or delete method on the REST client, or the save or delete method on the resource class, use the query method on the GraphQL client. |
Add a mutation |
Instead of specifying the path to the resource (like |
Destructure the response |
GraphQL responses are typically nested within a data object, and the structure of the data often mirrors the structure of the mutation. To make the data easier to work with, destructure the object that's returned. You can also access properties of the object using dot notation ( |
Read userErrors |
Extract the userErrors array from the response. If the mutation wasn't completed, then this information is returned in this array. Learn about how error handling differs in GraphQL. |
Node Fetch / Axios
Anchor link to section titled "Node Fetch / Axios"The following examples show you how to update your calls to GraphQL when reading or writing data using the Node Fetch or Axios libraries.
Reading data
Anchor link to section titled "Reading data"The following requests retrieve information about a product. To convert the REST version of this request to GraphQL, you need to make the following changes in your code.
Writing data
Anchor link to section titled "Writing data"The following requests create a new product. To convert the REST version of this request to GraphQL, you need to make the following changes in your code.
Update | Description |
---|---|
Change the endpoint | Instead of a resource-specific endpoint (/products/1234.json ), use the generic GraphQL endpoint for the store. The details for the update that you want to make is now part of a GraphQL mutation that's passed in the request body. |
Change the method | If you're using a PUT or DELETE method, then update the method to POST . |
Add a query | Add a GraphQL mutation in the request body. In this example, we're creating a new product. |
Add query variables |
Pass the details for the resource that you want to create or update as query variable. If you're updating or deleting a resource, then instead of using a simple ID ( |
Destructure the response |
GraphQL responses are typically nested within a data object. The structure of the data mirrors the structure of the query. To make the data easier to work with, destructure the object that's returned. You can also access properties of the object using dot notation ( |
Read userErrors |
Extract the userErrors array from the response. If the mutation wasn't completed, then this information is returned in this array. Learn about how error handling differs in GraphQL. |