-
Hi, me again ... ;) I'm trying to get Interface Types working but it seems that the documentation of how to do that is still missing in the docs. Therefore, I've gone through the source and tried to understand how you have implemented GraphQL Interfaces for WP GraphQL (e.g. I have tried the following: Register the Object Typeregister_graphql_object_type( 'MyType', [
'description' => 'This is an object type with interface',
'interfaces' => ['MyInterface'],
'fields' => [
'one' => [
'type' => 'Integer',
'description' => 'A field of type Integer'
]
]
]); Register the Interface Typeregister_graphql_interface_type( 'MyInterface', [
'description' => 'This is a GraphQL interface type',
'resolveType' => function( $data ) use ( $typeRegistry ) {
// I'm not sure what to do here, so I just return the Object Type 'MyType'
return 'MyType';
} ,
'fields' => [
'message' => [
'type' => ['non_null' => 'String'],
'description' => 'This is a mandatory message'
]
]
]); Register a Custom Fieldregister_graphql_field('RootQuery', 'MyCustomField', [
'type' => 'MyType',
'description' => 'This is a custom field',
'resolve' => function () {
return [ 'one' => 1, message = 'This needs to be said!' ];
}
]); The Query
I can query the field Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hey @Poolshark I realize I'm pretty late to replying here. Possibly there was a bug in the version you tried this with, but on WPGraphQL v1.6.10 I'm able to use your code and query what you were querying and get the data from the response. Here's the code exactly as I have it: add_action( 'graphql_register_types', function( $type_registry ) {
register_graphql_object_type( 'MyType', [
'description' => 'This is an object type with interface',
'interfaces' => ['MyInterface'],
'fields' => [
'one' => [
'type' => 'Integer',
'description' => 'A field of type Integer'
]
]
]);
register_graphql_interface_type( 'MyInterface', [
'description' => 'This is a GraphQL interface type',
'resolveType' => function( $data ) use ( $type_registry ) {
// I'm not sure what to do here, so I just return the Object Type 'MyType'
return 'MyType';
} ,
'fields' => [
'message' => [
'type' => ['non_null' => 'String'],
'description' => 'This is a mandatory message'
]
]
]);
register_graphql_field('RootQuery', 'MyCustomField', [
'type' => 'MyType',
'description' => 'This is a custom field',
'resolve' => function () {
return [ 'one' => 1, 'message' => 'This needs to be said!' ];
}
]);
} ); And in GraphiQL I can query: {
myCustomField {
one
message
}
} ![Screen Shot 2021-12-10 at 2 01 25 AM](https://user-images.githubusercontent.com/1260765/145546805-1cbd0801-2007-42e1-8f53-888a54392a85.png And I can also see |
Beta Was this translation helpful? Give feedback.
Hey @Poolshark I realize I'm pretty late to replying here. Possibly there was a bug in the version you tried this with, but on WPGraphQL v1.6.10 I'm able to use your code and query what you were querying and get the data from the response.
Here's the code exactly as I have it: