1

I want to use query by calling globalset in CraftCMS all child fields should be return, because the query that were used in prod are diffrent than queries we use in normal graphql explorer, after some research i was able to get the issue that adding fields manually in globalset interface fulfilling my requirenment , but i cant get object type fields can anyone have any idea how to achive it

this is my interface

<?php
/**
 * @link https://craftcms.com/
 * @copyright Copyright (c) Pixel & Tonic, Inc.
 * @license https://craftcms.github.io/license/
 */

namespace craft\gql\interfaces\elements;

use craft\gql\GqlEntityRegistry;
use craft\gql\interfaces\Element;
use craft\gql\TypeManager;
use craft\gql\types\generators\GlobalSetType;
use GraphQL\Type\Definition\InterfaceType;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Definition\ObjectType;
use craft\fieldlayoutelements\CustomField;

/**
 * Class GlobalSet
 *
 * @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
 * @since 3.3.0
 */
class GlobalSet extends Element
{
    /**
     * @inheritdoc
     */
    public static function getTypeGenerator(): string
    {
        return GlobalSetType::class;
    }

    /**
     * @inheritdoc
     */
    public static function getType($fields = null): Type
    {
        if ($type = GqlEntityRegistry::getEntity(self::getName())) {
            return $type;
        }

        $type = GqlEntityRegistry::createEntity(self::getName(), new InterfaceType([
            'name' => static::getName(),
            'fields' => self::class . '::getFieldDefinitions',
            'description' => 'This is the interface implemented by all global sets.',
            'resolveType' => self::class . '::resolveElementTypeName',
        ]));

        GlobalSetType::generateTypes();

        return $type;
    }

    /**
     * @inheritdoc
     */
    public static function getName(): string
    {
        return 'GlobalSetInterface';
    }

    /**
 * @inheritdoc
 */
public static function getFieldDefinitions(): array
{
    $fieldDefinitions = parent::getFieldDefinitions();

    $fieldDefinitions['handle'] = [
        'name' => 'handle',
        'type' => Type::string(),
        'description' => 'The handle of the global set.',
        'resolve' => function ($source) {
            return $source->handle;

        },
    ];


    $fieldDefinitions['fullWysiwyg'] = [
        'name' => 'fullWysiwyg',
        'type' => Type::string(),
        'description' => 'The fullWysiwyg field of the global set.',
        'resolve' => function ($source) {
            return $source->fullWysiwyg;
        },
    ];

    $fieldDefinitions['text4'] = [
        'name' => 'text4',
        'type' => Type::string(),
        'description' => 'The text4 field of the global set.',
        'resolve' => function ($source) {
            return $source->text4;
        },
    ];
    $fieldDefinitions['text'] = [
        'name' => 'text',
        'type' => Type::string(),
        'description' => 'The text field of the global set.',
        'resolve' => function ($source) {
            return $source->text4;
        },
    ];
    $fieldDefinitions['href'] = [
        'name' => 'href',
        'type' => Type::string(),
        'description' => 'The href field of the global set.',
        'resolve' => function ($source) {
            return $source->href;
        },
    ];
    $fieldDefinitions['image'] = [
        'name' => 'image',
        'type' => Asset::getType(), // Assuming it should be an Asset type
        'description' => 'The image field of the global set.',
        'resolve' => function ($source) {
            // Assuming $source is an instance of GlobalSetInterface
            return $source->getFieldV

type here

alue('image');
        },
    ];
    $fieldDefinitions['text3'] = [
        'name' => 'text3',
        'type' => Type::string(),
        'description' => 'The text3 field of the global set.',
        'resolve' => function ($source) {
            return $source->text3;
        },
    ];
    $fieldDefinitions['text2'] = [
        'name' => 'text2',
        'type' => Type::string(),
        'description' => 'The text2 field of the global set.',
        'resolve' => function ($source) {
            return $source->text3;
        },
    ];
    $fieldDefinitions['text5'] = [
        'name' => 'text5',
        'type' => Type::string(),
        'description' => 'The text5 field of the global set.',
        'resolve' => function ($source) {
            return $source->text5;
        },
    ];

    $fieldDefinitions['simpleWysiwyg'] = [
        'name' => 'simpleWysiwyg',
        'type' => Type::string(),
        'description' => 'The simpleWysiwyg field of the global set.',
        'resolve' => function ($source) {
            return $source->simpleWysiwyg;
        },
    ];
   


    $fieldDefinitions['fullWysiwyg2'] = [
        'name' => 'fullWysiwyg2',
        'type' => Type::string(),
        'description' => 'The fullWysiwyg2 field of the global set.',
        'resolve' => function ($source) {
            return $source->fullWysiwyg2;
        },
    ];
    $ProductColorType = new ObjectType([
        'name' => 'ProductColor',
        'fields' => [
            'colorName' => [
                'type' => Type::string(),
                'description' => 'The name of the color',
            ],
            'colorValue' => [
                'type' => Type::string(),
                'description' => 'The value of the color',
            ],
        ],
    ]);

    $fieldDefinitions['ProductColors'] = [
        'name' => 'ProductColors',
        'type' => Type::listOf(Type::string()),
        'description' => 'The fullWysiwyg2 field of the global set.',
        'resolve' => function ($source) {
            return $source->fullWysiwyg2;
        },
    ];



    

    return $fieldDefinitions;
}

}


#import "@bkwld/cloak/fragments/craft/default-seo.gql"
#import "@bkwld/cloak/fragments/craft/image.gql"
#import "@bkwld/cloak/fragments/craft/responsive-image.gql"
#import "./fragments/nav-menus.gql"

# Query Craft globals data
query getGlobals($site:[String]) {

    # Default (fallback) SEO values
    defaultSeo: globalSet(
        handle: "defaultSeo"
        site:$site) { ...defaultSeo }

    # Notification bar
    notificationBar: globalSet(
        handle: "notificationBar"
        site:$site) {
        text: simpleWysiwyg
        url: href
    }

    # Contact Info
    contactInfo: globalSet(
        handle: "contactInfo"
        site:$site) {
        fullWysiwyg
    }

    # Social Links
    socialLinks: globalSet(
        handle: "socialLinks"
        site:$site) {
        ... on socialLinks_GlobalSet {
            socialLinks {
                ... on socialLinks_BlockType {
                    id
                    platform
                    to
                }
            }
        }
    }

    # Desktop Header Navigation
    desktopNavigation: entry(
        section: "navigation"
        type: "desktopHeaderNavigation"
        slug: "desktop-header"
        site: $site
    ) {
        title
        navigationMenus { ...NavMenus }
    }

    bannerAd:   globalSet(
        handle: "headerBannerAd"
        site:$site) {
        image { ...image }
        title: text
        body: text2
        ctaLabel: text3
        ctaLink: href
    }

    # Mobile Header Navigation
    mobileNavigation: entry(
        section: "navigation"
        type: "mobileHeaderNavigation"
        slug: "mobile-header"
        site:$site
    ) {
        title
        navigationMenus { ...NavMenus }
    }

    # Footer navigation links
    footerNavigation: entry(
        section: "navigation"
        type: "footerNavigation"
        slug: "footer-navigation"
        site:$site
        ) {
        title
        children(limit: 100) {
            title
            link: text
            slug

            children(limit: 100) {
                title
                link: text
                slug
            }
        }
    }

    # End Cap Navigation Links
    endCapNavigation: entry(
        section: "navigation"
        type: "footerNavigation"
        slug: "end-cap-navigation"
        site:$site
        ) {
        title
        children(limit: 10) {
            title
            slug
            link: text
        }
    }

    # Newsletter Signup
    newsletterSignup: globalSet(
        handle: "newsletterSignup"
        site:$site) {
        jotFormID: text3
        title: text
        formCopy: fullWysiwyg
        datePrompt: text4
        emailPrompt: text5
        termsCopy: simpleWysiwyg
        successMessage: fullWysiwyg2
        image: responsiveImage { ...responsiveImage }
    }

    #Affirm Modal
    affirmModal: globalSet(
        handle: "affirmModal"
        site:$site) {
        bodyCopy: fullWysiwyg
        image { ...image }
    }

    # 30-Night Satisfaction Guarantee Modal
        nightSatisfactionGuaranteeModal: globalSet(
        handle: "nightSatisfactionGuaranteeModal"
        site:$site) {
        bodyCopy: fullWysiwyg
        image { ...image }
    }

    # Free 2-Day Shipping Modal
    free2DayShippingModal: globalSet(
        handle: "free2DayShippingModal"
        site:$site){
            bodyCopy: fullWysiwyg
            image { ...image }
    }

    # HSA/FSA Approved Modal
    hsaFsaApprovedModal: globalSet(
        handle: "hsaFsaApprovedModal"
        site:$site) {
        bodyCopy: fullWysiwyg
        image { ...image }
    }

    cart: globalSet(
        handle: "cart"
        site:$site) {

        # Recommended Products
        products {
            slug
        }
    }

    productColors: globalSet(
        handle: "productColors"
        site:$site) {
        productColors {
            colorName
            colorValue
        }
    }

    # Free Gift Options
    freeGiftWithPurchase: globalSet(
        handle:"freeGiftWithPurchase"
        site:$site) {
        ... on freeGiftWithPurchase_GlobalSet {
            settings: freeGiftOptions {
                __typename
                ... on freeGiftOptions_userChoice_BlockType {
                    behavior: freeGiftBehavior
                    giftTriggers: products {
                        slug
                    }
                    products: productHandles {
                        handle
                    }
                }
            }
        }
    }

    # Search Page
    searchPage: globalSet(
        handle: "searchPage"
        site:$site) {
            seo {
                metaTitle
                metaDescription
                metaImage { path, url }
                canonicalUrl
                robots
            }
        }
}

 $fieldDefinitions['fullWysiwyg2'] = [
        'name' => 'fullWysiwyg2',
        'type' => Type::string(),
        'description' => 'The fullWysiwyg2 field of the global set.',
        'resolve' => function ($source) {
            return $source->fullWysiwyg2;
        },
    ];

i tried adding fields manually like that which is working fine with field types which are string,

$ProductColorType = new ObjectType([
        'name' => 'ProductColor',
        'fields' => [
            'colorName' => [
                'type' => Type::string(),
                'description' => 'The name of the color',
            ],
            'colorValue' => [
                'type' => Type::string(),
                'description' => 'The value of the color',
            ],
        ],
    ]);

    $fieldDefinitions['ProductColors'] = [
        'name' => 'ProductColors',
        'type' => Type::listOf($ProductColorType),
        'description' => 'The fullWysiwyg2 field of the global set.',
        'resolve' => function ($source) {
            return $source->fullWysiwyg2;
        },
    ];

then i did like this to do the same for object type but i got error

CraftError: Failed to validate the GQL Schema - Schema must contain unique named types but contains multiple types named

please help

Farman
  • 11
  • 1

0 Answers0