Question

How to create custom Graphql Mutation ?


Userlevel 1
Badge

I want to create a simple custom graphql mutation but I am not sure about the steps. When I refer the internet, it is mentioned that we need to define the schema first then we can use the custom mutation. Also, it is mentioned, in merchant center, there is a Graphql settings where we can define the schema but i cannot see such settings in Merchant center.

Can you please help me with the steps ?

Thanks in Advance.


7 replies

Badge

Hi Siva!

 

The GraphQL schema is generally defined and publicly available here: https://github.com/commercetools/commercetools-api-reference/blob/main/api-specs/graphql/schema.sdl

 

If you want to play around and have a slightly more user friendly way of getting to know graphQL you may also want to check out the GraphQL IDE in Impex. (the schema can be found on the right hand side under Docs)

You can find the link to the respective regions here: https://docs.commercetools.com/docs/login

Europe GraphQL IDE can be found here for example: https://impex.europe-west1.gcp.commercetools.com/graphiql

 

Hope this helps. 

Userlevel 1
Badge

Thanks @Jenny for the details. I have already checked this page https://github.com/commercetools/commercetools-api-reference/blob/main/api-specs/graphql/schema.sdl but I would like to know how can I create one custom graphql mutation. Can you please guide me on this that would be really helpful.

Userlevel 1
Badge

@Jenny Could you please help me on my previous comment ?

Userlevel 2
Badge

Hi @Siva 

Below is an example of mutation to update a cart with multiple update actions.  You can build such queries on the GraphQL IDE as Jenny mentioned above.  Does this help? 

 

mutation UpdateMyCart{
updateCart(
id: "9eg76th*******"
version: 5
actions: [
{
addLineItem: {
productId : "79847hlktsn*****",
variantId : 1,
quantity : 1
}
},
{
setCustomerEmail: {
email: ""
}
},
{
setShippingMethod: {
shippingMethod: {
id: "*****a125",
typeId: "shipping-method"
}
}
}
]
)
{
id
customerEmail
lineItems{
productId
productKey
variant{
sku
}
}
}
}

 

Userlevel 1
Badge

Hi @hetvim,

Thanks. I understand that we can use multiple actions in graphql request but still I think my question is not answered. I will try to elaborate it here:
Actually, I have understanding of Graphql as I have developed projects using Magento by graphql but in commercetools, I am totally new and learning now. I installed the php sdk in my local machine and also created a project in Merchant center. I explored, we have multiple graphqls available readily in commercetools and I checked that here but the question I have is whether we can create a new graphql query and mutation or not. Is there a provision in commercetools to do this ? If yes, where I need to create those files (Schema and Resolver) ? Because I cannot see such details in documentation and I need some understanding of those. Can you please help on these questions?

Userlevel 1
Badge

@hetvim @Jenny When I say new or custom graphql mutation, it may not available in GraphQL schema which is publicly available, https://github.com/commercetools/commercetools-api-reference/blob/main/api-specs/graphql/schema.sdl.

Example: updateCart is one of the default or built in graphql but if I want to create a complete new graphql mutation which have certain business logic, what steps I have to follow?

Badge

Hey @Siva,

 

to explain.a bit how GraphQL works within commercetools:

The GraphQL APIs are built on top of our REST APIs which are documented here: https://docs.commercetools.com/api/ 

Once an endpoint/action is made available via the REST API we try to make it available on GraphQL as well (which then will be seen in the public schema). 

As our APIs need the mapping to be able to act on the calls made by GraphQL you can only use the calls that are part of the public schema, but they should cover pretty much everything that is available via the REST API as well. In case there is a difference feel free to reach out to Support (at support.commercetools.com) to log a suggestion to support the behaviour. 

 

Hope that helps to clarify. 

Reply


© 2024 commercetools GmbH - All rights reserved