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 :


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?

1 Answer 1


You can register your field into GraphQL by TypeManager::EVENT_DEFINE_GQL_TYPE_FIELDS event here is an example:

            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

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.