Education
February 20, 2023

What Is GraphQL And How Does It Work For Efinity?

What Is GraphQL And How Does It Work For Efinity?
In this blog, we'll discuss how Efinity utilizes GraphQL's efficient data querying and modifying abilities to empower developers to create robust and engaging blockchain applications.

When discussing the Enjin Platform API and its interaction with Efinity, it is critical to consider GraphQL. While our bi-weekly development updates and tweets mention the use of GraphQL with queries and mutations, we haven't gone into detail about its functionality, including why it is easy to understand, even for those without much coding experience.

First, what is GraphQL?

GraphQL is a query language (a language for specifying methods for retrieving and occasionally changing information from databases) built specifically for APIs. It allows you to retrieve information from a database of your choice, in our case, the database is the blockchain itself! Great, isn’t it?

GraphQL allows you to specify exactly what data you want to retrieve in a single API request. This is similar to how you would shop for specific items, like buying your favorite game, a new controller, and a pair of shoes, rather than simply purchasing "something."

This precise data retrieval makes GraphQL a more efficient and flexible method of requesting data than traditional REST APIs. With REST APIs, you often receive extraneous information that you don't need, leading to slow and resource-intensive network requests. By contrast, GraphQL minimizes network traffic by allowing you to request only the data that is necessary.

How does GraphQL work?

GraphQL provides two types of operations: Queries and Mutations. With Queries, you can retrieve information from a database without modifying any of the data you receive (READ), while Mutations enable you to MODIFY the data in the specific database you are interacting with.

Queries

To effectively use GraphQL, it's essential to understand how Object Types, Fields, and Arguments work. To demonstrate this below is a mock-up example of a query for the Efinity blockchain.

1. Objects & Fields

In GraphQL, an object is a fundamental component in defining the structure of the data you interact with. Fields are the individual data points within an object. Fields provide specific information about an object, and they are used to define the shape of the data that you can request from the server. For instance, suppose you want to track information about a specific NFT on the Efinity blockchain, such as the NFT name, minting date, owner, and the collection it belongs to.

type NFT {
 id: "1001"
 name: "Efinity_NFT"
 collection: "Cool_Game"
 owner: "Marian"
}

In this context, ‘id’, ‘name’, ‘collection’, and ‘owner’ are all fields of the ‘NFT’ object.

2. Arguments

Arguments allow you to pass variables to a field, which can be used to filter or modify the data returned by that field.

Now, let’s say we want to return information about a specific NFT; to do so, we start by defining a query with a field for each piece of data we would like to retrieve.

type Query {
 NFT(id: "1001")
 name
 collection 
 owner
}

The NFT field is the root of our query, and it takes the argument of 'id', which is the unique identifier of the NFT. The ‘name’, ‘collection’, and ‘owner’ fields are then used to specify which data we want to retrieve about the NFT.

When executing the query, the argument is used to filter the results, so only the data for the specified NFT is returned. This makes queries more efficient and reduces the amount of data transferred over the network.

In this context, here’s how a query would pass the Objects, Fields, and Arguments all correctly.

query GetNftInfo($id: ID!) {
 NFT(id: $id) {
  name
  collection
  owner
 }
}

Mutations

As for mutations, based on what we explained before, you can MODIFY data from this specific database that you are interacting with. Unlike queries, which are used to retrieve data, mutations are used to create, update, or delete data.

Here's an example of how a mutation can be used to create an NFT in the same context as before:(Please note that these queries and mutations do not in-fact exist, and were created to act as a visual representation of real-world scenarios)

mutation CreateNFT {
 createNFT(id: "1002", name: "This is our NFT", collection: "Cool_Game", owner: "Marian") {
  id
  name
  collection
  owner
 }
}

In this example, the createNFT mutation takes four arguments: 'id', 'name', 'collection', and 'owner'. The 'createNFT' field takes these arguments and creates a new NFT in the database with the specified values.

After executing this mutation, the server can respond with the created NFT data:

{
 "data": {
  "createNFT": {
   "id": "1002",
   "name": "This is our NFT",
   "collection": "Cool_Game",
   "owner": "Marian"
  }
 }
}

This response contains the ‘id’, ‘name’, ‘collection’, and ‘owner’ fields of the newly created NFT. By using mutations, you can modify data in the database with the same ease and flexibility as retrieving data with queries.

Tying all of that to Efinity and the Enjin Platform API

The Enjin Platform API is a package designed to accelerate blockchain integration for game developers. With the Enjin Platform API, developers can perform blockchain actions without worrying about interacting with the blockchain directly.

The Enjin Platform API includes a GraphQL API server that provides developers with access to all indexed data from the Efinity blockchain. With a full range of queries and mutations available, developers can retrieve any necessary information and send transactions directly to the blockchain.

The GraphQL API server is designed to be the primary source of data retrieval from the Efinity blockchain. By using GraphQL, developers can retrieve only the data they need, which reduces bandwidth usage and improves application performance. Instead of making multiple requests to retrieve different pieces of data, developers can fetch and filter data within a single request, making data retrieval more efficient and flexible. This approach reduces the load on the server and the client, resulting in faster and more responsive applications.

Here are a few examples of how you can use the GraphQL API to integrate your game to Efinity with some of the functions available (not all):

  • Creating FTs & NFTs Collections;
  • Minting, transferring, and burning tokens;
  • Setting Royalties to a collection or tokens;
  • Transferring FTs & NFTs;
  • Create Managed Wallets for your users;
  • Querying information about tokens, collections and users;

And a lot more! These are just a few examples of the many functions available through the GraphQL API that developers can leverage to create immersive blockchain-based games with Efinity.

Wrapping things up 🤓

The GraphQL API is an extremely powerful tool, making it easier for game developers to interact with the Efinity Blockchain through the Enjin Platform API. The API's easy-to-use interface abstracts away the complexities of blockchain technology, allowing developers to focus on creating great gaming experiences without worrying about the underlying technology.

By using GraphQL, developers are able to query and modify data with a single request, making it more efficient and flexible compared to other REST APIs. The Enjin Platform API does away with the difficulties of dealing directly with the blockchain and helps developers swiftly develop blockchain integrations. The Enjin Platform API's GraphQL API server makes it simple for developers to create blockchain-based games and applications by enabling them to send transactions and receive all indexed data from the Efinity blockchain.

Overall, the Enjin Platform API and GraphQL offer a simplified way for game developers to leverage blockchain technology and create engaging gaming experiences for their players.

If you want to learn more about the GraphQL API or would like to integrate your game into Efinity, don’t hesitate to get in touch with us here.

Want to know more?
Check out how to bring NFTs into Efinity!
Learn More

You might also enjoy

 Efinity Token Migration FAQs
August 15, 2023
Efinity Token Migration FAQs
Ecosystem
Education
Efinity Beam: Exclusive Features
March 17, 2023
Efinity Beam: Exclusive Features
Ecosystem
Education
How to create your Digital Collectibles on Efinity
February 2, 2023
How to create your Digital Collectibles on Efinity
Education
How to Use Fuel Tanks
January 18, 2023
How to Use Fuel Tanks
Education