0

In Magento2 graphql i want to add a field to the root query of the Magento GraphQL API called isLoggedIn. This should return Boolean whether the customer is logged in or not.

2 Answers 2

2

I couldn't find any GraphQL API Query named with "isLoggedIn" on Magento2 devdocs. Hence, I would not able to provide you the exact code to achieve your requirements.

However, I can explain you the concept vise. First, you need to create one file named as "schema.graphqls" in etc folder in your custom module (please create custom module if not exist any).

In this file, suppose if you want to add an additional field at root level of "Cart Query" then you need to write as below:

type Cart {
    extra_fields: YourExtraFields @resolver(class: "\\Namespace\\Modulename\\Model\\Resolver\\YourExtraFieldsResolver") @doc(description:"Additional custom fields used in the project.")
}

type YourExtraFields {
    field_first: Int! @doc(description: "The first field number count")
    field_second: Boolean! @doc(description: "The second field flag")
}

Now, you need to create a resolver file at the given path in model folder. In that file you just need to write your business logic and need to return all field values added in "YourExtraFields" schema.

Once the code is done, you can check in Altair and your new field(s) would be displayed in the respective api schema. I have done this on number of times in my project.

Let me know if you need any further assistance.

Happy to help!

2
  • Thanks for answer. Does it check whether the customer is logged in or not? Can you send me this small magento module having above mentioned functionality. I am new to magento graphql, so I have little beginner technical knowledge about it. Nov 3, 2020 at 6:18
  • @SanaullahAhmad, if you are using any existing customer graphql api then you can access customer object and you can check/use its properties. However, if you are building a new custom GraphQL api then you will to write whole business logic by your own. I will surely share some piece of demo code once I get some free time. Nov 3, 2020 at 6:58
0

As @Pratik explained how to do it, For further info do look into Magento resolvers

Which goes into your

type Cart {
    extra_fields: YourExtraFields @resolver(class: "\\Namespace\\Modulename\\Model\\Resolver\\YourExtraFieldsResolver") @doc(description:"Additional custom fields used in the project.")
}

Hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.