Magento 2 GraphQL Tutorial: How to Import Data via GraphQL API into Magento 2
The following Magento 2 GraphQL tutorial describes how to import and export data into/from Magento 2 with the help of GraphQL API. This connection type is yet impossible out of the box, but the community is working hard to introduce it as a replacement for REST API. At the same time, some tools enable Magento 2 GraphQL API import and export right now. For instance, you can leverage the Magento 2 GraphQL API import and export processes with the Improved Import & Export Magento 2 module.
The following article sheds light on how to import data into Magento 2 via GraphQL API with the help of our extension. You will find out how to enable the Magento 2 GraphQL API import and what extra benefits it is associated with. If you are not familiar with what GraphQL API is, we’ve also prepared a chapter that describes this standard and its benefits over REST and SOAP. Besides, an FAQ section provides answers to frequently asked questions about GraphQL Magento 2 import and export processes.
Please Note: Connection to complex 3rd-party API may require custom development on top of our Improved Import & Export. Our solution is 100% open source, so we would be happy to advise how to add the necessary functionality. We can also provide you with a quote for any integration –
Table of contents
- 1 Why Magento 2 GraphQL?
- 2 Magento 2 GraphQL API Import & Export
- 3 GraphQL Magento 2 API Connector Features
- 4 Magento 2 GraphQL API Characteristics
- 5 Magento 2 GraphQL Community Project
- 6 Magento 2 GraphQL API Import & Export FAQ
- 6.1 How to import products to Magento 2 via GraphQL API?
- 6.2 How to export products to Magento 2 via GraphQL API?
- 6.3 How to import categories to Magento 2 via GraphQL API?
- 6.4 How to export categories to Magento 2 via GraphQL API?
- 6.5 How to import orders to Magento 2 via GraphQL API?
- 6.6 How to export orders to Magento 2 via GraphQL API?
- 6.7 How to import customers to Magento 2 via GraphQL API?
- 6.8 How to export customers to Magento 2 via GraphQL API?
- 6.9 How to import B2B entities to Magento 2 via GraphQL API?
- 6.10 How to export B2B entities to Magento 2 via GraphQL API?
- 6.11 How to automate GraphQL API import and export processes in Magento 2?
- 6.12 How to import data with unsupported attributes to Magento 2 via GraphQL API?
- 7 Conclusion
Why Magento 2 GraphQL?
Because GraphQL in Magento 2 has multiple advantages over the standard Magento 2 API. Being a query language for API, GraphQL is at the same time a flexible server-side runtime where queries can be effortlessly executed. You only need to define a system type for your data. Another stunning moment regarding GraphQL is that this API connection doesn’t require it to be tied to a specific database or storage engine. Instead, it is backed by the existing code and data.
As a result, you can increase the performance of your store as well as achieve extra flexibility. That’s why the Magento community is going to implement GraphQL API as an alternative to REST API. And as mentioned above, we know something that already enables the technology on your e-commerce website.
Magento 2 GraphQL API Import & Export
To enable the Magento 2 GraphQL import and export, you need the Improved Import & Export Magento 2 extension. Install the module, and you will get the desired functionality along with tons of other crucial improvements and stunning features.
The Improved Import & Export extension not only supports GraphQL in Magento 2, but it is also already compatible with the platform’s improvement created by the Magento community. Thus, when GraphQL API support officially becomes a part of our favorite e-commerce platform, the Improved Import & Export extension will fully support the built-in functionality.
Our plugin already allows you to avoid the use of files in data transfers due to REST and SOAP APIs. In addition to these two standards, you can fully leverage GraphQL. The only requirement is related to a third-party platform you want to integrate your store with: it should support this API type. But how does the extension implement the Magento 2 GraphQL API import and export?
Everything is very straightforward. The Improved Import & Export module adds two sections to the admin section of your e-commerce store. The first one contains import profiles while the second one is designed to gather all export jobs. Depending on your needs, you should choose a section and create a new profile. While configuring the new import or export job, specify GraphQL API as a connection type and set other parameters of the integration. Enable/disable other options, making data transfers more flexible. What other features can be used with the GraphQL API integration?
GraphQL Magento 2 API Connector Features
Below, you will find a list of improvements that make every Magento 2 data import and export via GraphQL API fully automated. Besides, we shed light on alternative ways of information exchange.
How to Import Data via GraphQL in Magento 2 Automatically
There are two ways of automated data import into Magento 2 via GraphQL: with the help of schedules and after specific events. Let’s take a look at each one individually.
Cron
The first robust feature of the Improved Import & Export Magento 2 extension that enables the automation of default processes is scheduling. Our extension fully supports cron allowing you to create any custom schedule of updates. You only need to know a standard cron syntax to make import and export profiles run at a specified time automatically.
At the same time, you can select one of the predefined intervals to achieve the same goal. Note that even the predefined periods are fully customizable. You need the same knowledge as we’ve just mentioned above.
If you need a one-time data transfer, the Improved Import & Export Magento 2 extension provides the ability to create a job that has no schedule. You launch it once manually and can disable it after the data is transferred.
It is also necessary to mention that every scheduled profile supports asynchronous runs. If a significant update is provided right now, but the corresponding import job is planned for the next week, you can effortlessly start it whenever you want despite the schedule.
Note that all these features are fully compatible with every Magento 2 GraphQL data import and export process.
Events
Alternatively, to crone schedules, you can launch GraphQL import into Magento 2 right after a specific process. Select one or multiple trigger actions creating flexible conditions, and let the Improved Import & Export extension run all the corresponding operations automatically. The same behavior is possible in the case of export processes.
For instance, the extension can automatically update stocks – import the appropriate data file – when the number of product items is less than 10. Or it can export orders after a new purchase is made. For further information, follow this link: How to Run Magento 2 Import or Export After Specific System Event or Process.
How to Import Data via GraphQL API into Magento 2 and Map Attributes
It is not a secret that you always have to match third-party attributes to ones used in Magento 2 before running the update. And if in the case of data files the resolution is apparent, how to achieve the same result with the GraphQL API integration?
The Improved Import & Export Magento 2 extension offers all the necessary tools. Furthermore, there is a universal interface for all types of integration and file formats. And you have to slightly different options there: mapping presets and manual matching.
Presets
The Improved Import & Export extension dramatically improves the process of attribute matching by introducing mapping presets. You no longer need to edit data files manually. Instead, the module allows the implementation of the corresponding mapping scheme within just a few clicks. Select a preset that corresponds to the external platform you’d like to synchronize Magento 2 with and apply the scheme. Our plugin will match attributes automatically.
The following gif illustrates how to apply a preset for the migration from Magento 1 to Magento 2:
Mapping Interface
In addition to presets, the Improved Import & Export Magento 2 extension provides the ability to do everything manually within a user-friendly mapping interface. It is only necessary to select a third-party attribute and match it to the corresponding one used in Magento 2, just like shown below:
Note that there is the third column where you can specify a hardcoded value that is added to all items regarding the corresponding attribute.
Attribute Values Mapping
With the help of Improved Import & Export, you can not only match attributes but also apply the same improvements to their values. Everything takes place in the same interface where you can map multiple external values to the one used in Magento 2. For instance, the data file includes two attribute values: lime-green and avocado-green. The corresponding attribute value in your database is green. To avoid new values, you can match them to the existing ones. For further information, check this article: Attribute Values Mapping.
Attribute Values Editing
Alternatively, the Improved Import & Export extension allows you to import data into Magento 2 via GraphQL API and edit attribute values in bulk. You can specify a prefix that will be added to them. At the same time, it is possible to add a suffix. Note that it is possible to create specific conditions to make the improvement more precise. Other supported actions include the ability to split or merge data. Attribute values editing is described here: How to Modify Attribute Values During Import and Export in Magento 2.
Category Mapping
Since we are talking about the mapping capabilities of the Improved Import & Export Magento 2 extension, it is also necessary to mention that the module lets you map categories. Thus, products from any external catalog can be seamlessly added to yours. Furthermore, you can even create new categories during the import process. Both opportunities are discussed here: Category Mapping.
Additional Ways of Import/Export
In addition to GraphQL in Magento 2, the Improved Import & Export Magento 2 module supports several more ways to exchange data. Let’s take a look at each one.
Data File Import/Export
Our extension significantly extends the default capabilities of the platform when it comes to supported file formats. While Magento works with CSV files only, the Improved Import & Export plugin adds support for XML, JSON, ODS, and Excel. You can freely import and export files of these types into and from your e-commerce store.
Besides, there are several convenient file sources. First of all, you can freely leverage your local server to exchange data between Magento 2 and any external system. If this option is not suitable for your particular integration, use an external server. Improved Import & Export supports FTP/SFTP connection so that both options can be leveraged.
Alternatively, it is possible to streamline a Dropbox account for the same purpose. You can even import data via a direct URL. Note that the extension will extract the content of all compressed files automatically.
Import/Export Without Files
As for the ability to import and export data without files, it is represented not only by GraphQL support but also by REST and SOAP API integration with external systems. All the aforementioned features are entirely suitable for both standards.
Besides, you can easily import data from Google Sheets. This process is illustrated below:
Get Improved Import & Export Magento 2 Extension
Magento 2 GraphQL API Characteristics
Now, let’s take a look at the core principles behind GraphQL. You can create a service by defining types and fields on those types. Besides, it is necessary to provide functions for each field for each type. Below, you can see an example of a GraphQL service that informs us about the logged-in user:
1 2 3 4 5 6 7 8 |
type Query { me: User } type User { id: ID name: String } |
Functions for each field on each type have the following appearance:
1 2 3 4 5 6 7 |
function Query_me(request) { return request.auth.user; } function User_name(user) { return user.getName(); } |
When a GraphQL service is running, it can send queries for validation and execution. A received query is first checked to ensure it refers to the types and fields defined. Next, the provided functions are run to produce a result.
Below, you can see a simple example of the query:
1 2 3 4 5 |
{ me { name } } |
And this is a possible JSON result:
1 2 3 4 5 |
{ "me": { "name": "Luke Skywalker" } } |
Now, that you know the core principle behind GraphQL, we can proceed to the chapter that describes the technology from the perspective of Magento and sheds light on the Magento 2 GraphQL Community Project. For further information on GraphQL basics, check this article:
Magento 2 GraphQL Community Project
Now, that you know all the essential aspects of the Magento 2 GraphQL import and export processes run with the help of our module, we’d like to draw your attention to the community project that tends to make GraphQL API a part of the platform.
API Graphs
The name of the API came from the role of the project in Facebook maintenance. The popular social networking service uses it as the API to query the social graph. GraphQL is partway between query languages such as SQL and REST or SOAP. Thus, a GraphQL request always specifies what data to be returned, but GraphQL doesn’t provide any query operators. And since there is no JOIN operator, GraphQL allows a client to navigate through a type graph defined by a server.
Types
Being strongly typed, GraphQL allows you to define the exact structure of what can be returned. The following code snippet contains the example:
1 2 3 4 5 6 7 |
type Product { id: Int sku: String title: String description: String . . . } |
Types are generated automatically from the set of available attributes for entities such as “Product”. Alternatively, the return type of service contracts may be used.
Interoperability
What else attracts the Magento community? Of course, it is the simplicity of the GraphQL type system. This characteristic makes it easier to integrate the system with a broader range of programming languages. Furthermore, there are no barriers to aligning with existing Magento 2 service contracts. Thus, the appearance of GraphQL support in addition to REST and SOAP is inevitable.
You can leverage numerous client-side JavaScript libraries for integrating GraphQL with JavaScript rendering frameworks:
Data Retrieval
Both read and write request concepts are supported! Thus, when a query is specified to retrieve data, determine what fields you want to return. This nature is fully compatible with the new proposed Magento query API and helps to minimize the amount of data necessary for retrieving and transfers:
1 2 3 4 5 6 |
{ product(sku: “MJ01”) { sku title } } |
The JSON format is used to return the data:
1 2 3 4 5 6 7 8 |
{ “data”: { “product”: { “sku”: “MJ01”, “title”: “Mens Jacket in leather” } } } |
With this approach, fields map onto function calls. Thus, the “SKU” field above may be a call to a simple getter function of a product record that is fetched from the database. At the same time, fields can be used to invoke any defined code. Imagine a “relatedProducts” field that returns an array of product information about related products. As a result, a query can fetch data from multiple nodes in a graph of information in a single query, making GraphQL more potent than REST.
1 2 3 4 5 6 7 8 9 |
{ product(sku: “MJ01”) { title relatedProducts { sku title } } } |
If you think that asking for specific data fields is implemented in a less efficient way than you’ve got used to, check the following benefits:
- Network transfers are reduced, improving the experience related to mobile devices.
- Multiple performance improvements are possible. The server knows what fields are wanted, so you can enable query optimizations.
- The evolution of schema is possible because new fields can be added without breaking existing applications. As for the old ones, they can be removed when no old clients request them.
Request Batching
With GraphQL, you get multiple resources and services available from a single endpoint – URL. In the case of REST, a separate URL per resource is used. Thus, one request can include multiple queries with an aliasing scheme dramatically increasing performance and allowing you to avoid name collisions. Thus, sharing the per HTTP request overhead with several requests becomes much more comfortable and efficient.
1 2 3 4 5 6 7 8 9 |
{ p1: product(sku: “MJ01”) { sku title } p2: product(sku: “MJ02”) { description } } |
And you avoid the HTTP and PHP startup overhead which is an issue that occurs per REST calls.
Introspection
There is one more benefit if you have a well-defined type system. GraphQL endpoints publish their schema allowing interactive clients to query what a site supports including the documentation of all data structures and available methods.
Furthermore, GraphIQL is available as a Chrome extension so that you can install and use it without any headaches. In the case of Magento, the GraphQL schema can be dynamically generated from the available service contracts published by loaded modules and entity attribute definitions. Thus, developers can determine what a specific site supports.
Drawbacks
It is also necessary to draw your attention to possible issues and disadvantages caused by the implementation of the native Magento 2 support for GraphQL. First of all, the performance improvements of HTTP/2 will decrease the benefits of merging HTTP requests. Secondly, GraphQL can make caching with Varnish and similar technologies harder especially for blending multiple requests or using POST for sending queries.
Thirdly, Web Application Firewall rules can be harder to protect against attacks. The reason is in the schema’s nature. Since it is necessary to parse the contents of a POST request instead of blocking particular REST URLs, the problem may occur.
However, we don’t think that these problems will prevent the implementation of GraphQL API in Magento 2. The community will generate multiple solutions to avoid issues.
Magento 2 GraphQL API Import & Export FAQ
How to import products to Magento 2 via GraphQL API?
You can import products to Magento 2 via GraphQL API with the help of the Improved Import & Export extension. Create a new import job that will connect your e-commerce website to the destination containing the data you want to transfer. Next, provide the credentials for the point of data exchange, create a schedule of updates, and apply a mapping scheme to replace unsupported attributes. Improved Import & Export works with all product types, including gift cards!
How to export products to Magento 2 via GraphQL API?
The Improved Import & Export plugin offers functionality similar to the one described above pointed towards export processes. Thus, transferring products from Magento 2 to a remote destination via GraphQL API is possible. The combination of two import and export jobs creates a bi-directional GraphQL API connection between Magento 2 and an external system.
How to import categories to Magento 2 via GraphQL API?
It is possible to import categories to Magento 2 via GraphQL API with the help of the Improved Import & Export extension. Create a new import job that will connect your e-commerce website to the destination containing the data you want to transfer. Next, provide the credentials for the point of data exchange, create a schedule of updates, and apply a mapping scheme to replace unsupported attributes. Note that the Improved Import & Export supports all category data and lets you map external categories to the ones available in the existing category tree.
How to export categories to Magento 2 via GraphQL API?
The Improved Import & Export plugin offers functionality similar to the one described above aimed at export processes. Thus, transferring categories from Magento 2 to a remote destination via GraphQL API is possible. The combination of two import and export jobs creates a bi-directional GraphQL API connection between Magento 2 and an external system.
How to import orders to Magento 2 via GraphQL API?
The Improved Import & Export extension introduces an opportunity to import orders to Magento 2 via GraphQL API. Create a new import job that will connect your e-commerce website to the destination containing the data you want to transfer. Next, provide the credentials for the point of data exchange, create a schedule of updates, and apply a mapping scheme to replace unsupported attributes. Note that the Improved Import & Export imports all order data, including documents and tracking information.
How to export orders to Magento 2 via GraphQL API?
The Improved Import & Export plugin offers functionality similar to the one described above pointed towards export processes. Thus, transferring orders from Magento 2 to a remote destination via GraphQL API is possible. The combination of two import and export jobs creates a bi-directional GraphQL API connection between Magento 2 and an external system.
How to import customers to Magento 2 via GraphQL API?
You can import customers to Magento 2 via GraphQL API with the help of the Improved Import & Export extension. Create a new import job that will connect your e-commerce website to the destination containing the data you want to transfer. Next, provide the credentials for the point of data exchange, create a schedule of updates, and apply a mapping scheme to replace unsupported attributes. Note that the plugin transfers customers with all the related information, including customer addresses, groups, billing/shipping details, etc.
How to export customers to Magento 2 via GraphQL API?
The Improved Import & Export plugin offers functionality similar to the one described above aimed at export processes. Thus, transferring customers from Magento 2 to a remote destination via GraphQL API is possible. The combination of two import and export jobs creates a bi-directional GraphQL API connection between Magento 2 and an external system.
How to import B2B entities to Magento 2 via GraphQL API?
Although the Improved Import & Export extension is not enough for transferring B2B entities, you can easily use its B2B add-on to enable the missing functionality. The add-on lets you create a new import job that connects your e-commerce website to the destination that contains B2B entities. You can import companies, requisitions lists, shared catalogs, quotes, and negotiation quotes.
How to export B2B entities to Magento 2 via GraphQL API?
The Improved Import & Export plugin combined with the B2B add-on offers functionality similar to the one described above aimed at export processes. Thus, transferring products from Magento 2 to a remote destination via GraphQL API is possible. The combination of two import and export jobs creates a bi-directional GraphQL API connection between Magento 2 and an external system.
How to automate GraphQL API import and export processes in Magento 2?
The Improved Import & Export Magento 2 module offers a cron scheduler to let you automate GraphQL API import and export processes. Thus, you can specify intervals to launch the corresponding data transfers automatically.
How to import data with unsupported attributes to Magento 2 via GraphQL API?
The Improved Import & Export plugin enables GraphQL API import and export processes and offers a robust mapping interface, where you can match unsupported attributes to ones used in Magento 2. Consequently, the module replaces all the unsuitable data with the required designations, enabling import from external systems.
Conclusion
To learn more about the Magento 2 GraphQL tandem, learn
Get Improved Import & Export Magento 2 Extension
Please Note: Connection to complex 3rd-party API may require custom development on top of our Improved Import & Export. Our solution is 100% open source, so we would be happy to advise how to add the necessary functionality. We can also provide you with a quote for any integration –