Answers.Body: <p>You can register your field into GraphQL by <code>TypeManager::EVENT_DEFINE_GQL_TYPE_FIELDS event</code> here is an example:</p>
  <pre><code>Event::on(
              TypeManager::class,
              TypeManager::EVENT_DEFINE_GQL_TYPE_FIELDS,
              static function(DefineGqlTypeFieldsEvent $event) {
                  if ($event-&gt;typeName === 'your_entry_type') {
                  $event-&gt;fields['totalResults'] = [
                      'name' =&gt; 'totalResults',
                      'type' =&gt; Type::string(,
                      'resolve' =&gt; function($source) {
                          /** @var Entry $event */
                          $entry = $source;
                          return $entry-&gt;totalResults;
                      }
                  ];
              }
               
              }
          );
  </code></pre>
  <p>Please note <code>your_entry_type</code> is the typeName, if I am not wrong the format is section_entryType_type</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-01-15T04:37:43.073Z
Answers.LastActivityDate: 2024-01-15T04:37:43.073Z
Answers.UserDisplayName: aodihis
Body: <p>I have an headless site that is using GraphQL and we have one query that need to do some calculation before returning the results.</p>
  <p>I was planing to create a behaviours in Craft with a property that return the result.</p>
  <p>If I'm querying the entry :</p>
  <pre><code>Entry::find()-&gt;section('xyz')-&gt;id('1')-&gt;one()-&gt;totalResult
  </code></pre>
  <p>The behaviours return what it is supposed.</p>
  <p>If i'm building a GraphQL query I've got an error saying that the field cannot be query.</p>
  <pre><code>query MyQuery {
    product {
      id, title, totalResult
    }
  }
  </code></pre>
  <p>Is there a way for GraphQL to have access to this property or is there a simple workaround to create a custom query without having to create a separate api?</p>
  
Tags: <graphql>
Title: GraphQL - Behaviours
ThreadText: # GraphQL - Behaviours
  
  (Asked by: username_0 on 2024-01-10)
  
  I have an headless site that is using GraphQL and we have one query that need to do some calculation before returning the results.
  
  I was planing to create a behaviours in Craft with a property that return the result.
  
  If I'm querying the entry :
  
  ```
  Entry::find()->section('xyz')->id('1')->one()->totalResult
  
  ```
  
  The behaviours return what it is supposed.
  
  If i'm building a GraphQL query I've got an error saying that the field cannot be query.
  
  ```
  query MyQuery {
    product {
      id, title, totalResult
    }
  }
  
  ```
  
  Is there a way for GraphQL to have access to this property or is there a simple workaround to create a custom query without having to create a separate api?
  
  **Tags:** graphql
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2024-01-15. Score: 3 | Accepted answer
  
  You can register your field into GraphQL by `TypeManager::EVENT_DEFINE_GQL_TYPE_FIELDS event` here is an example:
  
  ```
  Event::on(
              TypeManager::class,
              TypeManager::EVENT_DEFINE_GQL_TYPE_FIELDS,
              static function(DefineGqlTypeFieldsEvent $event) {
                  if ($event->typeName === 'your_entry_type') {
                  $event->fields['totalResults'] = [
                      'name' => 'totalResults',
                      'type' => Type::string(,
                      'resolve' => function($source) {
                          /** @var Entry $event */
                          $entry = $source;
                          return $entry->totalResults;
                      }
                  ];
              }
               
              }
          );
  
  ```
  
  Please note `your_entry_type` is the typeName, if I am not wrong the format is section\_entryType\_type
  
  
  ---
  
  