@@ -795,7 +795,7 @@ where
795795impl < C , I > CustomSetView < C , I >
796796where
797797 C : Context ,
798- I : Sync + Clone + Send + CustomSerialize ,
798+ I : Sync + Send + CustomSerialize ,
799799{
800800 /// Returns the list of indices in the set. The order is determined by the custom
801801 /// serialization.
@@ -939,70 +939,74 @@ pub type HashedCustomSetView<C, I> =
939939mod graphql {
940940 use std:: borrow:: Cow ;
941941
942+ use serde:: { de:: DeserializeOwned , Serialize } ;
943+
942944 use super :: { CustomSetView , SetView } ;
943- use crate :: context:: Context ;
945+ use crate :: {
946+ common:: CustomSerialize ,
947+ context:: Context ,
948+ graphql:: { hash_name, mangle} ,
949+ } ;
944950
945- impl < C : Context , I : async_graphql:: OutputType > async_graphql:: OutputType for SetView < C , I >
946- where
947- I : serde:: ser:: Serialize + serde:: de:: DeserializeOwned + Clone + Send + Sync ,
948- {
951+ impl < C : Send + Sync , I : async_graphql:: OutputType > async_graphql:: TypeName for SetView < C , I > {
949952 fn type_name ( ) -> Cow < ' static , str > {
950- format ! ( "[{}]" , I :: qualified_type_name( ) ) . into ( )
953+ format ! (
954+ "SetView_{}_{:08x}" ,
955+ mangle( I :: type_name( ) ) ,
956+ hash_name:: <I >( ) ,
957+ )
958+ . into ( )
951959 }
960+ }
952961
953- fn qualified_type_name ( ) -> String {
954- format ! ( "[{}]!" , I :: qualified_type_name( ) )
962+ #[ async_graphql:: Object ( cache_control( no_cache) , name_type) ]
963+ impl < C , I > SetView < C , I >
964+ where
965+ C : Context ,
966+ I : Send + Sync + Serialize + DeserializeOwned + async_graphql:: OutputType ,
967+ {
968+ async fn elements ( & self , count : Option < usize > ) -> Result < Vec < I > , async_graphql:: Error > {
969+ let mut indices = self . indices ( ) . await ?;
970+ if let Some ( count) = count {
971+ indices. truncate ( count) ;
972+ }
973+ Ok ( indices)
955974 }
956975
957- fn create_type_info ( registry : & mut async_graphql :: registry :: Registry ) -> String {
958- I :: create_type_info ( registry ) ;
959- Self :: qualified_type_name ( )
976+ # [ graphql ( derived ( name = "count" ) ) ]
977+ async fn count_ ( & self ) -> Result < u32 , async_graphql :: Error > {
978+ Ok ( self . count ( ) . await ? as u32 )
960979 }
980+ }
961981
962- async fn resolve (
963- & self ,
964- ctx : & async_graphql:: ContextSelectionSet < ' _ > ,
965- field : & async_graphql:: Positioned < async_graphql:: parser:: types:: Field > ,
966- ) -> async_graphql:: ServerResult < async_graphql:: Value > {
967- let indices = self
968- . indices ( )
969- . await
970- . map_err ( |e| async_graphql:: Error :: from ( e) . into_server_error ( ctx. item . pos ) ) ?;
971- let indices_len = indices. len ( ) ;
972- async_graphql:: resolver_utils:: resolve_list ( ctx, field, indices, Some ( indices_len) )
973- . await
982+ impl < C : Send + Sync , I : async_graphql:: OutputType > async_graphql:: TypeName for CustomSetView < C , I > {
983+ fn type_name ( ) -> Cow < ' static , str > {
984+ format ! (
985+ "CustomSetView_{}_{:08x}" ,
986+ mangle( I :: type_name( ) ) ,
987+ hash_name:: <I >( ) ,
988+ )
989+ . into ( )
974990 }
975991 }
976992
977- impl < C : Context , I : async_graphql:: OutputType > async_graphql:: OutputType for CustomSetView < C , I >
993+ #[ async_graphql:: Object ( cache_control( no_cache) , name_type) ]
994+ impl < C , I > CustomSetView < C , I >
978995 where
979- I : crate :: common:: CustomSerialize + Clone + Send + Sync ,
996+ C : Context ,
997+ I : Send + Sync + CustomSerialize + async_graphql:: OutputType ,
980998 {
981- fn type_name ( ) -> Cow < ' static , str > {
982- format ! ( "[{}]" , I :: qualified_type_name( ) ) . into ( )
983- }
984-
985- fn qualified_type_name ( ) -> String {
986- format ! ( "[{}]!" , I :: qualified_type_name( ) )
987- }
988-
989- fn create_type_info ( registry : & mut async_graphql:: registry:: Registry ) -> String {
990- I :: create_type_info ( registry) ;
991- Self :: qualified_type_name ( )
999+ async fn elements ( & self , count : Option < usize > ) -> Result < Vec < I > , async_graphql:: Error > {
1000+ let mut indices = self . indices ( ) . await ?;
1001+ if let Some ( count) = count {
1002+ indices. truncate ( count) ;
1003+ }
1004+ Ok ( indices)
9921005 }
9931006
994- async fn resolve (
995- & self ,
996- ctx : & async_graphql:: ContextSelectionSet < ' _ > ,
997- field : & async_graphql:: Positioned < async_graphql:: parser:: types:: Field > ,
998- ) -> async_graphql:: ServerResult < async_graphql:: Value > {
999- let indices = self
1000- . indices ( )
1001- . await
1002- . map_err ( |e| async_graphql:: Error :: from ( e) . into_server_error ( ctx. item . pos ) ) ?;
1003- let indices_len = indices. len ( ) ;
1004- async_graphql:: resolver_utils:: resolve_list ( ctx, field, indices, Some ( indices_len) )
1005- . await
1007+ #[ graphql( derived( name = "count" ) ) ]
1008+ async fn count_ ( & self ) -> Result < u32 , async_graphql:: Error > {
1009+ Ok ( self . count ( ) . await ? as u32 )
10061010 }
10071011 }
10081012}
0 commit comments