imranhsayed / woo-next

:rocket: React WooCommerce theme, built with Next JS, Webpack, Babel, Node, Express, using GraphQL and Apollo Client
https://codeytek.com/course/woocommerce-with-react-course/
879 stars 260 forks source link

wooCountries, and wooStates, are those custom post types or plugins #91

Closed waynekhalifa closed 2 years ago

waynekhalifa commented 2 years ago

const GET_COUNTRIES = gqlquery GET_COUNTRIES{ wooCountries { billingCountries { countryCode countryName } shippingCountries { countryCode countryName } } }`

Server Error Error: Cannot query field "wooCountries" on type "RootQuery".

imranhsayed commented 2 years ago

These come from headless cms plugin. Please ensure the this plugin is active

space87 commented 2 years ago

i am having this issue after just pulling this down i have headless plugin activated

tedc commented 2 years ago

me too

siraht commented 2 years ago

Hi @imranhsayed I am also having this issue. I've deactivated all plugins and reactivated Headless CMS first. I've activated WPGraphQL then WooGraphQL then Headless CMS, and still, nothing works. Any ideas?

EDIT: The issue was that the latest Headless CMS plugin release in the Wordpress repository is v1.2.0 whereas the latest release on GitHub is v1.7.0. Updating to 1.7.0 downloaded from GitHub solved the issue!

waynekhalifa commented 2 years ago

Update wp graphql, and woographql both of them to the latest version

Sent from my iPhone

On 12 Oct 2021, at 10:10 PM, Travis @.***> wrote:

 Hi @imranhsayed I am also having this issue. I've deactivated all plugins and reactivated Headless CMS first. I've activated WPGraphQL then WooGraphQL then Headless CMS, and still, nothing works. Any ideas?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe. Triage notifications on the go with GitHub Mobile for iOS or Android.

spetrey commented 2 years ago

For anyone else who is continuing to see Cannot query field "wooCountries" on type "RootQuery". or similar error messages from the checkout page on the demo site — I found the following workaround helpful because it appears that the "headless-cms" plugin doesn't seem to work with WordPress 5.9. To resolve those query fields, just drop this at the end of your functions.php file and that should do the trick:

function get_formatted_countries($countries)
{

    $formatted_countries = [];

    if (empty($countries) && !is_array($countries)) {
        return $formatted_countries;
    }

    foreach ($countries as $countryCode => $countryName) {
        array_push($formatted_countries, [
            'countryCode' => $countryCode,
            'countryName' => $countryName,
        ]);
    }

    return $formatted_countries;
}

register_graphql_object_type('WooCountry', [
    'fields' => [
        'countryCode'  => ['type' => 'String'],
        'countryName' => ['type' => 'String'],
    ]
]);

register_graphql_object_type('WooCountries', [
    'description' => __('Countries Type', 'headless-cms'),
    'fields'      => [
        'billingCountries'   => [
            'type' => [
                'list_of' => 'WooCountry'
            ]
        ],
        'shippingCountries'   => [
            'type' => [
                'list_of' => 'WooCountry'
            ]
        ],
    ],
]);

register_graphql_field(
    'RootQuery',
    'wooCountries',
    [
        'description' => __('Countries', 'headless-cms'),
        'type'        => 'WooCountries',
        'resolve'     => function () {

            // All countries for billing.
            $all_countries     = class_exists('WooCommerce') ? WC()->countries : [];
            $billing_countries = !empty($all_countries->countries) ? $all_countries->countries : [];
            $billing_countries = get_formatted_countries($billing_countries);

            // All countries with states for shipping.
            $shipping_countries = class_exists('WooCommerce') ? WC()->countries->get_shipping_countries() : [];;
            $shipping_countries = !empty($shipping_countries) ? $shipping_countries : [];
            $shipping_countries = get_formatted_countries($shipping_countries);

            /**
             * Here you need to return data that matches the shape of the "WooCountries" type. You could get
             * the data from the WP Database, an external API, or static values.
             * For example in this case we are getting it from WordPress database.
             */
            return [
                'billingCountries'  => $billing_countries,
                'shippingCountries' => $shipping_countries,
            ];
        },
    ]
);
ghost commented 2 years ago

that's really great spetrey thanks a lot

there is also a query called wooStates that makes a problem, it appears after you select a country, for some states it tries to fetch an array of states... are you able to provide a function to repair this query?