Extending GraphQL Mutations for Optional Input Fields in Magento 2
In this post, we’ll explore how to extend the GraphQL schema in Magento 2 to make input fields optional. We’ll focus on the Braintree module as an example, but these principles apply to any custom extension you’re developing.
The Current Schema and Requirements
The current BraintreeInput schema in the schema.graphqls file defines fields like clientToken, paymentMethodNonce, etc. However, we need to make the paymentMethodNonce field optional based on your business requirements.
input BraintreeInput {
client_token: String!
payment_method_nonce: String!
}
Extending the Schema
To achieve this, we’ll create a new schema file in our custom module. Let’s name it etc/schema.graphqls.
First, make sure your module is enabled and the required dependencies are set up correctly. For more details on module setup, check out our guide to creating Magento 2 extensions.
Defining the New Input Type
In the schema.graphqls file of your custom module, define a new input type that extends the existing one. This new type will have the same fields as the original, but we’ll specify the paymentMethodNonce field with an optional type (String?).
input ExtendedBraintreeInput {
client_token: String!
payment_method_nonce: String?
}
extend query { braintreeClientToken(clientId: $client_id): String! @resolver("Magento\\BraintreeGraphQl\\Resolver\\BraintreeClientToken") }
mutate braintreeTransaction(
input: ExtendedBraintreeInput!
) returns BraintreeTransaction {
transaction_id
}
Resolving the New Schema
Next, we need to create a resolver for our new schema. In your module’s etc/di.xml, add the following configuration:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\BraintreeGraphQl\Model\Resolver\BraintreeClientToken">
<arguments>
<argument name="inputTypeName" xsi:type="string">ExtendedBraintreeInput</argument>
</arguments>
</type>
</config>
This configuration tells Magento to use our new ExtendedBraintreeInput type instead of the original one when resolving the braintreeClientToken field.
Implementing the Mutation Resolver
Finally, we need to create a resolver for our new mutation. In your module’s etc/di.xml, add the following:
<type name="Magento\BraintreeGraphQl\Model\Resolver\BraintreeTransaction">
<arguments>
<argument name="inputTypeName" xsi:type="string">ExtendedBraintreeInput</argument>
</arguments>
</type>
Now, when you call the braintreeTransaction mutation using our new input type, the paymentMethodNonce field is optional.
Conclusion
In this post, we demonstrated how to extend an existing GraphQL schema in Magento 2 to make a field optional. By creating a new input type and configuring it correctly, we were able to modify the behavior of the Braintree module’s input fields without impacting the core functionality.
Remember that extending schemas is just one aspect of customizing your Magento store’s API. For more advanced customizations, consider using Magento 2’s Headless Commerce capabilities, which provide a more flexible and modern approach to API-driven development.
Don’t forget to thoroughly test your changes before deploying them to production. Magento’s GraphQL schema can be complex, so it’s important to ensure that your extensions work correctly in all scenarios.
Happy coding!