Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: Type Crb_ThemeOptions must define one or more fields. #8

Closed
benada002 opened this issue Mar 10, 2021 · 5 comments
Closed

Error: Type Crb_ThemeOptions must define one or more fields. #8

benada002 opened this issue Mar 10, 2021 · 5 comments

Comments

@benada002
Copy link
Contributor

Hi,

If I try to run GraphQL Code Generator, I get the following error: Type Crb_ThemeOptions must define one or more fields.

I think one possible solution would be:

diff --git a/src/Container.php b/src/Container.php
index aaee152..284437e 100644
--- a/src/Container.php
+++ b/src/Container.php
@@ -21,6 +21,8 @@ class Container
 {
   private static $is_first_time = true;
 
+  private static $is_first_time_theme_options = true;
+
   public function __construct(CrbContainer $container)
   {
     $this->container = $container;
@@ -54,6 +56,12 @@ public function graphqlRegisterTypes()
       Container::$is_first_time = false;
       Container::registerStaticObjectTypes();
     }
+
+    if ($this->container->type === 'theme_options' && Container::$is_first_time_theme_options) {
+      Container::$is_first_time_theme_options = false;
+      Container::registerStaticThemeOptionObjectTypes();
+    }
+
     $this->registerFields();
   }
 
@@ -64,12 +72,14 @@ private function registerFields()
     }
   }
 
-  static function registerStaticObjectTypes()
+  static function registerStaticThemeOptionObjectTypes()
   {
-    register_graphql_object_type('Crb_ThemeOptions', [
-      'description' => \__("All the Carbon Field Theme Options", 'app'),
-      'fields' => [],
-    ]);
+    register_graphql_object_type(
+        'Crb_ThemeOptions', [
+        'description' => \__("All the Carbon Field Theme Options", 'app'),
+        'fields' => [],
+      ]
+    );
 
     register_graphql_field(
       'RootQuery',
@@ -81,7 +91,10 @@ static function registerStaticObjectTypes()
         }
       ]
     );
+  }
 
+  static function registerStaticObjectTypes()
+  {
     register_graphql_object_type('Crb_Select', [
       'description' => \__("The selected option/radio", 'app'),
       'fields' => [

Or do you've another idea?

@matepaiva
Copy link
Owner

Could you explain that solution? I don't understand how it fixes the issue 😕

@benada002
Copy link
Contributor Author

benada002 commented Mar 11, 2021

Sorry, I guess, I wasn't clear enough. This error only occurs if I'm not making a theme_options container (Container::make('theme_options')). I think it's because the registerStaticObjectTypes function, as it now is, then registers an empty Crb_ThemeOptions object type.

To resolve that I took the register_graphql_object_type function (for Crb_ThemeOptions) and the register_graphql_field function (for crb_ThemeOptions) out of registerStaticObjectTypes, and stuck them into it's own registerStaticThemeOptionObjectTypes function. Then in the graphqlRegisterTypes function, I check if the current container is a theme_options container and only then (and the first time) I call the registerStaticThemeOptionObjectTypes to register the Crb_ThemeOptions type. Otherwise the (then empty) Crb_ThemeOptions type isn't registred. Which for me solves it.

@matepaiva
Copy link
Owner

Now I get it. I don't have the time to test it now, but I will accept it as you said you tested and it solved the issue for you.

@matepaiva
Copy link
Owner

New release: https://github.com/matepaiva/wp-graphql-crb/releases/tag/0.0.3

Thank you for the contribution!

@benada002
Copy link
Contributor Author

Thanks. I updated and tried it again. Now I can run GraphQL Code Generator without problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants