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

Graphql Error on association field with custom post type or taxonomy if it has multiple types. #4

Closed
benada002 opened this issue Mar 7, 2021 · 9 comments

Comments

@benada002
Copy link
Contributor

I've a association field with a custom subtype and multiple types, like this:

Field::make('association', 'test', __('Materials:'))
    ->set_types(
        array(
            array(
                'type'      => 'post',
                'post_type' => 'materials',
            ),
            array(
                'type'      => 'term',
                'taxonomy' => 'material_category'
            )
        )
    )

If the graphql_single_name option (from the post type or taxonomy) not starts with an uppercase letter, it results in a graphql error: Type loader is expected to return type "materialCategory", but it returned "MaterialCategory".

I took a look at it and saw that register_graphql_union_type function, triggers a prepare_type function that uses ucfirst for the type name. The following fix works for me.

diff --git a/src/Field.php b/src/Field.php
index 0ea53e9..b1a9bf6 100644
--- a/src/Field.php
+++ b/src/Field.php
@@ -193,7 +193,7 @@ private function getTypeFromAssociation()
             return 'Post';
           }
 
-          return $graphql_single_name;
+          return ucfirst($graphql_single_name);
         }
 
         if ($object instanceof Term) {
@@ -206,7 +206,7 @@ private function getTypeFromAssociation()
             return 'Tag';
           }
 
-          return $taxonomy_name;
+          return ucfirst($taxonomy_name);
         }
 
         if ($object instanceof Comment) {
@matepaiva
Copy link
Owner

@benada002 thank you for the contribution. I will have a look at this tonight, ok?

@matepaiva
Copy link
Owner

Great contribution! I will merge and release a new version.

matepaiva added a commit that referenced this issue Mar 8, 2021
@matepaiva
Copy link
Owner

I will wait for another PR and then I will release a new version. I let you know here.

@benada002
Copy link
Contributor Author

benada002 commented Mar 8, 2021

Sounds great.

Since getTypeFromAssociation now uses ucfirst, I don't think the if checks for post, category, and tag are needed anymore. Like this:

diff --git a/src/Field.php b/src/Field.php
index b1a9bf6..57b6669 100644
--- a/src/Field.php
+++ b/src/Field.php
@@ -189,22 +189,11 @@ private function getTypeFromAssociation()
         if ($object instanceof Post) {
           $graphql_single_name = \get_post_type_object($object->post_type)->graphql_single_name;
 
-          if ($graphql_single_name === 'post') {
-            return 'Post';
-          }
-
           return ucfirst($graphql_single_name);
         }
 
         if ($object instanceof Term) {
           $taxonomy_name = \get_taxonomy($object->taxonomyName)->graphql_single_name;
-          if ($taxonomy_name === 'category') {
-            return 'Category';
-          }
-
-          if ($taxonomy_name === 'tag') {
-            return 'Tag';
-          }
 
           return ucfirst($taxonomy_name);
         }

What do you think about it? Should I raise another PR? Or do you want to keep it like it is?

@matepaiva
Copy link
Owner

If you could open another PR, it would be awesome :)

@benada002
Copy link
Contributor Author

Ok, I'll do it in the afternoon.

@matepaiva
Copy link
Owner

Thank you!

@matepaiva
Copy link
Owner

Hello, I already fixed it, so I will update the release and close this issue. Thank you again!

@matepaiva
Copy link
Owner

New version available:

Thank you for your contribution!

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