Question

What is the use of schema.graphqls file?

vendor/magento/module-catalog-graph-ql/etc/schema.graphqls

Was it helpful?

Solution

The schema.graphqls file provides the information about generally two types of queries supported by GraphQL language. They are: 1. type Query to get/read data. 2. type Mutation to perform CRUD operations.

Now lets proceed with an example and try to understand that how actually GraphQL works, enter any module which ends with graph-ql and navigate to etc > schema.graphqls. Now search for the block type Query {}. As here want to get the cms page of id = 3, so will use module-cms-graph-ql, and pass the id to cmsPage graphQL query.

Figure 1. GraphQL modules present OOTB

Fig.2 Schema.graphql file

Now if we follow the above diagram i.e. Figure 2, our resulting graphQL query for fetching the details of cms page with id = 3 will be something like this:

  query cmsPage{
    cmsPage(id: 3){
    url_key
    title
    content
    content_heading
    page_layout
   }
}

@Sanjay Shiyal your question was where to run this query and check the output, so for this you can use ChromeiQL or Altair GraphQL , special Chrome extension.

To the above mentioned extension you need to set endpoint which will be having structure like: magentorooturl/graphql, like in my case its http://127.0.0.1/dev.local/graphql

So, for your reference I will be executing the above query in ChromeiQL extension as shown below: Figure 3. GraphQl query and result

Will also cross-check the above produced result of our graphQL query by diving directly into our admin CMS page section.

Figure 4. Enable Cookies CMS page(id =3) in Admin panel

It became a bit lenghty post, but I think it will give a crystal clear picture on how to have some hands on with GraphQL, which is an alternative of Rest and Soap API. If agreed, then kindly mark it as an accepted answer.

Thanks.

OTHER TIPS

schema.graphqls file defines the structure of queries and mutations and describe which data can be used for input and output.

For ex :

# Copyright © Magento, Inc. All rights reserved.
# See COPYING.txt for license details.

type Query {
    cmsPage (
        id: Int @doc(description: "Id of the CMS page")
    ): CmsPage @resolver(class: "Magento\\CmsGraphQl\\Model\\Resolver\\Page") @doc(description: "The CMS page query returns information about a CMS page")
}

type CmsPage @doc(description: "CMS page defines all CMS page information") {
    url_key: String @doc(description: "URL key of CMS page")
    title: String @doc(description: "CMS page title")
    content: String @doc(description: "CMS page content")
    content_heading: String @doc(description: "CMS page content heading")
    page_layout: String @doc(description: "CMS page content heading")
    meta_title: String @doc(description: "CMS page meta title")
    meta_description: String @doc(description: "CMS page meta description")
    meta_keywords: String @doc(description: "CMS page meta keywords")
}

It describes that which type of input you want to add and which type of output will be display.


input : cmsPage id

output : url_key, title, content, content_heading, page_layout, meta_title, meta_description, meta_keywords.

Hope, it will useful for you.

schema.graphqls is the file located in etc directory of the every grapql module. It is used to create Query and Mutation in graphql modules.

We used type Query to get the data from database and Mutation to submit the data to database. Native magento have create Customer graphql as Mutation and most of the others are query related.For example products, cms page etc.

We can use native graphQl modules and we can write our own custom graphql queries for our custom modules.

For further explanation you may visit What is GraphQl in magento 2.3 and how to access it?

For custom grapQl related stuff visit How to create a graphql schema for magento 2 custom module with custom table?

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top