Score:0

How to expose custom webform fields to GraphQL?

cn flag

I would like to use https://www.drupal.org/project/captcha with Webforms. For exposing Webforms via GraphQL I use https://github.com/duartegarin/graphql_webform.

As CAPTCHA is not a standard field https://github.com/duartegarin/graphql_webform does not expose it via GraphQL. As I did not find any documentation on how to expose custom webform fields to GraphQL I thought I ask about it here.

Thank you.

cn flag
Since your question is specific to a module that is not on drupal.org, I would ask in the module's issue queue instead: https://github.com/duartegarin/graphql_webform/issues
Score:1
us flag

The CAPTCHA module doesn't implement a webform component. It just adds a form element to the forms for which it has been configured to add its CAPTCHA.

If the CAPTCHA module implemented a webform component, then implementing a few classes (starting from a plugin like WebformElementPhone) would allow to integrate the modules together.

/**
 * A GraphQL type for textfield form item.
 *
 * @GraphQLType(
 *   id = "webform_element_phone",
 *   name = "WebformElementPhone",
 *   interfaces = {"WebformElementTextBase"},
 * )
 */
class WebformElementPhone extends TypePluginBase {

  /**
   * {@inheritdoc}
   */
  public function applies($object, ResolveContext $context, ResolveInfo $info) {
    return $object['plugin'] instanceof Telephone;
  }

}
HennyKo avatar
cn flag
Thank you, I wrote a WebformElementCaptcha, returning an instance of Drupal\webform\Plugin\WebformElement\Captcha. But I am unable to access any fields of the Captcha. I also tried to create a new interface. But the example WebformElementTextBase is empty, so I do not know how to expose the values.
HennyKo avatar
cn flag
Okay, I think I have to write a custom WebformElement. But I do not understand the structure of $value. `json_encode($value)` is a good return value in resolveValues for debugging. (But it looks like the capture itself is not returned, only the title, the description and what type of captcha is used).
apaderno avatar
us flag
The first step is implementing a webform component for the CAPTCHA, then a `GraphQLType` for that webform component. It's not a simple task and it requires to understand what the purpose of all the classes is.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.