wp-graphql / wp-graphql

:rocket: GraphQL API for WordPress
https://www.wpgraphql.com
GNU General Public License v3.0
3.67k stars 445 forks source link

`register_graphql_interfaces_to_types` is not working #2172

Closed jasonbahl closed 2 years ago

jasonbahl commented 2 years ago

Problem

The register_graphql_interfaces_to_types() function isn't working as expected.

The tests (https://github.com/wp-graphql/wp-graphql/blob/develop/tests/wpunit/RegisterInterfaceToTypeTest.php) for this functionality pass, but the functionality doesn't seem to work in practice.

Steps to reproduce

  1. Take the code from the test and drop in in an active PHP file (like a plugin file or theme's functions.php):
add_action( 'graphql_register_types', function() {

    register_graphql_object_type( 'TestType', [
        'fields' => [
            'a' => [
                'type' => 'String'
            ]
        ]
    ]);

    register_graphql_interface_type( 'TestInterface', [
        'fields' => [
            'b' => [
                'type' => 'String',
            ],
        ],
    ]);

    register_graphql_interfaces_to_types( [ 'TestInterface' ], [ 'TestType' ] );

} );
  1. Open GraphiQL and execute the same query that's executed in the test:
query GetType {
  __type(name: "TestType") {
    name
    kind
    interfaces {
      name
    }
    fields {
      name
    }
  }
}
  1. See that the Interfaces are not returned:
{
  "data": {
    "__type": {
      "name": "TestType",
      "kind": "OBJECT",
      "interfaces": [],
      "fields": [
        {
          "name": "a"
        }
      ]
    }
  }
}

Expected Behavior

I expect the GraphQL Query for the Type to return the interfaces that are applied by the Type, and I expect the field enforced by the Interface to also be returned in the fields list.

{
  "data": {
    "__type": {
      "name": "TestType",
      "kind": "OBJECT",
      "interfaces": [
        {
          "name": "TestInterface" <-- this is expected, but missing
        }
      ],
      "fields": [
        {
          "name": "a"
        },
        {
          "name": "b" # <-- this is expected, but missing
        }
      ]
    }
  }
jasonbahl commented 2 years ago

Updates:

jasonbahl commented 2 years ago

Aha!

This is only an issue when I'm also running my beta "WPGraphQL Block Editor" extension šŸ¤”

Will dig into that šŸ‘€

I have a feeling it has to do with multiple plugins registering interfaces to types? šŸ¤”

jasonbahl commented 2 years ago

Ok, closing this as I've determined it's not a bug with WPGraphQL but was a bug with my local setup. šŸ¤¦šŸ»ā€ā™‚ļø

I had a plugin that I was working on that had WPGraphQL as a composer dependency and so I was loading WPGraphQL twice which was causing some real funky behavior šŸ¤¦šŸ»ā€ā™‚ļø