How to Import Data via GraphQL API into Magento 2

- E-Commerce, Magento 2, Our extensions

Magento 2 graphql import export

The following article 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. Let’s take a closer look at the details.

Why GraphQL API?

Because it 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 type system for your data. Another stunning moment regarding GraphQL is that this API connection doesn’t require 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

Magento 2 mysql data import

To enable the Magento 2 GraphQL import and export right now, you need the Improved Import & Export Magento 2 extension. Install this module, and you will get the desired functionality along with tons of other crucial improvements and stunning features.

The Improved Import & Export Magento 2 extension not only supports GraphQL integrations, 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?

Magento 2 GraphQL 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 the information exchange.

How to Import Data via GraphQL API into 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 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 wand despite the schedule.

Note that all these features are fully compatible with every Magento 2 GraphQL data import and export processes.

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 attributes matching by introducing mapping presets. You no longer need to edit data files manually. Instead, the module allows implementing 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 one. 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 API, 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 systems. 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 the GraphQL support but also with 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:

Magento 2 Google Sheet import

Get Improved Import & Export Magento 2 Extension

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:

Functions for each field on each type have the following appearance:

When a GraphQL service is running, it can be sent 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:

And this is a possible JSON result:

Now, when you know the core principle behind GraphQL, we can proceed to the chapter that described 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: Introduction to GraphQL.

Magento 2 GraphQL Community Project

magento 2 graphql import export

Now, when you know all essential aspect 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.

GraphQL is an API style available as an open source project. Initially developed by Facebook, it is getting traction on other projects, including GitHub. Below, we shed light on why GraphQL is attractive for the Magento 2 community.

API Graphs

The name of the API came from the role of the project in the Facebook maintenance. The popular social networking service uses it as the API to query the social graph. GraphQL is part way 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:

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 no barriers in aligning with existing Magento 2 service contracts. Thus, the appearance of GraphQL support in addition to REST and SOAP is inevitable.

And you can leverage numerous several client-side JavaScript libraries for integrating GraphQL with JavaScript rendering frameworks: Relay for React, Apollo, etc.

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 retrieves and transfers:

The JSON format is used to return the data:

With this approach, fields map on to function calls. Thus, the “sku” field above may be a call to a simple getter function of a product record which 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.

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 make enabling query optimizations.
  • The schema evolution 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.

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 nature of the schema. Since it is necessary to parse the contents of a POST request instead of blocking particular REST URLs, the problem may occur.

But 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 the issues.

Conclusion

Find out why GraphQL is interesting for Magento 2 and discover the Magento 2 GraphQL community project. As for the Improved Import & Export Magento 2 extension, it already allows you to leverage the technology on your e-commerce website. For further information about the module, follow the link below and contact our support team:

Get Improved Import & Export Magento 2 Extension