0

Here i am creating an objectType object called Product

new ObjectType([
  'name' => 'Product',
   'fields' => fn() => [
      'databaseId' => [
       'type' => Type::Int(),
    ],
]);

It is working fine... i am also creating a UnionType

new UnionType([
  'name' => 'SimpleProduct',
   'types' => [
      AvatarType(),
      UserType(),
   ],
   'resolveType' => function( $value )
    {
      if ( $value->type === 'User' )
      {
         return UserType();
      }
      elseif( $value->type == 'Avatar' )
      {
         return AvatarType();
      }
    }
]);

Here both AvatarType & UserType is an Object itself!!

I want to use this query as

query RootQuery {
   products {
    databaseId
    ... on Avatar{
      url
    }
    ... on User{
      username
    }
  }
}

Here RootQuery.products is Type::listOf( 'Product' )

How do i connect the Union SimpleProduct to Product ObjectType

Read some blogs about interfaces but not sure how to connect that Union to Object without using fields!! for example i saw blog articles as...

new ObjectType([
    'name' => 'Product',
    'fields' => fn() => [
        'fieldA' => [
           'type' => SimpleProduct,
    ],
]);

but that just give me...

query RootQuery {
   products {
    fieldA{
     ... on Avatar{
       url
     }
     ... on User{
       username
     }
    }
  }
}

How can i add Union directly to Object without using fields!!

4

0 回答 0