5252import java .net .URL ;
5353import java .text .MessageFormat ;
5454import java .util .*;
55+ import java .util .function .Function ;
5556import java .util .stream .Collectors ;
5657
5758import static graphql .Scalars .GraphQLBoolean ;
5859import static graphql .Scalars .GraphQLString ;
60+ import static org .jahia .modules .graphql .provider .dxm .sdl .SDLConstants .*;
5961
6062@ Component (service = SDLSchemaService .class , immediate = true )
6163public class SDLSchemaService {
@@ -198,17 +200,16 @@ public List<GraphQLFieldDefinition> getSDLQueries() {
198200
199201 List <GraphQLFieldDefinition > fieldDefinitions = graphQLSchema .getQueryType ().getFieldDefinitions ();
200202 for (GraphQLFieldDefinition fieldDefinition : fieldDefinitions ) {
201- GraphQLObjectType objectType = fieldDefinition .getType () instanceof GraphQLList ?
202- (GraphQLObjectType ) ((GraphQLList ) fieldDefinition .getType ()).getWrappedType () : (GraphQLObjectType ) fieldDefinition .getType ();
203-
204- GraphQLAppliedDirective directive = objectType .getAppliedDirective (SDLConstants .MAPPING_DIRECTIVE );
203+ GraphQLDirectiveContainer directiveContainer = fieldDefinition .getType () instanceof GraphQLList ?
204+ (GraphQLDirectiveContainer ) ((GraphQLList ) fieldDefinition .getType ()).getWrappedType () :
205+ (GraphQLDirectiveContainer ) fieldDefinition .getType ();
205206
207+ GraphQLAppliedDirective directive = directiveContainer .getAppliedDirective (SDLConstants .MAPPING_DIRECTIVE );
206208 if (directive == null ) {
207209 continue ;
208210 }
209211
210212 String nodeType = directive .getArgument (SDLConstants .MAPPING_DIRECTIVE_NODE ).getValue ().toString ();
211-
212213 //Handle connections
213214 if (fieldDefinition .getName ().contains (SDLConstants .CONNECTION_QUERY_SUFFIX )) {
214215 String queryFieldName = fieldDefinition .getName ().replace (SDLConstants .CONNECTION_QUERY_SUFFIX , "" );
@@ -256,6 +257,21 @@ public List<GraphQLType> getSDLTypes() {
256257 return types ;
257258 }
258259
260+ public Set <GraphQLDirective > getDirectives () {
261+ Set <GraphQLDirective > result = new HashSet <>();
262+ if (graphQLSchema == null ) {
263+ generateSchema ();
264+ }
265+
266+ if (graphQLSchema != null ) {
267+ List <GraphQLDirective > directives = graphQLSchema .getDirectives ();
268+ if (directives != null ) {
269+ result .addAll (directives );
270+ }
271+ }
272+ return result ;
273+ }
274+
259275 public GraphQLSchema getGraphQLSchema () {
260276 return graphQLSchema ;
261277 }
@@ -334,27 +350,27 @@ private void applyDefaultFetcher(final List<GraphQLFieldDefinition> defs, final
334350
335351 private TypeDefinitionRegistry prepareTypeRegistryDefinition () {
336352 TypeDefinitionRegistry typeDefinitionRegistry = new TypeDefinitionRegistry ();
337- typeDefinitionRegistry .add (new ObjectTypeDefinition ("Query" ));
353+ Function <GraphQLScalarType , TypeName > newType = scalar -> new TypeName (scalar .getName ());
354+
355+ typeDefinitionRegistry .add (ObjectTypeDefinition .newObjectTypeDefinition ().name ("Query" )
356+ .fieldDefinition (new FieldDefinition ("_empty" , newType .apply (GraphQLBoolean )))
357+ .build ());
338358 typeDefinitionRegistry .add (new ScalarTypeDefinition ("Date" ));
339359 typeDefinitionRegistry .add (DirectiveDefinition .newDirectiveDefinition ()
340360 .name (SDLConstants .MAPPING_DIRECTIVE )
341361 .directiveLocations (Arrays .asList (
342- DirectiveLocation . newDirectiveLocation (). name ( "OBJECT" ). build ( ),
343- DirectiveLocation . newDirectiveLocation (). name ( "FIELD_DEFINITION" ). build () ))
362+ new DirectiveLocation ( "OBJECT" ),
363+ new DirectiveLocation ( "FIELD_DEFINITION" ) ))
344364 .inputValueDefinitions (Arrays .asList (
345- InputValueDefinition . newInputValueDefinition (). name ( SDLConstants . MAPPING_DIRECTIVE_NODE ). type ( TypeName . newTypeName ( GraphQLString . getName ()). build ()). build ( ),
346- InputValueDefinition . newInputValueDefinition (). name ( SDLConstants . MAPPING_DIRECTIVE_PROPERTY ). type ( TypeName . newTypeName ( GraphQLString . getName ()). build ()). build ( ),
347- InputValueDefinition . newInputValueDefinition (). name ( SDLConstants . MAPPING_DIRECTIVE_IGNORE_DEFAULT_QUERIES ). type ( TypeName . newTypeName ( GraphQLBoolean . getName ()). build ()). build () ))
365+ new InputValueDefinition ( MAPPING_DIRECTIVE_NODE , newType . apply ( GraphQLString ) ),
366+ new InputValueDefinition ( MAPPING_DIRECTIVE_PROPERTY , newType . apply ( GraphQLString ) ),
367+ new InputValueDefinition ( MAPPING_DIRECTIVE_IGNORE_DEFAULT_QUERIES , newType . apply ( GraphQLBoolean )) ))
348368 .build ());
349-
350369 typeDefinitionRegistry .add (DirectiveDefinition .newDirectiveDefinition ()
351- .name (SDLConstants .FETCHER_DIRECTIVE )
352- .directiveLocations (Arrays .asList (
353- DirectiveLocation .newDirectiveLocation ().name ("FIELD_DEFINITION" ).build ()))
354- .inputValueDefinitions (Arrays .asList (
355- InputValueDefinition .newInputValueDefinition ().name (SDLConstants .FETCHER_DIRECTIVE_NAME ).type (TypeName .newTypeName (GraphQLString .getName ()).build ()).build ()))
370+ .name (FETCHER_DIRECTIVE )
371+ .directiveLocation (new DirectiveLocation ("FIELD_DEFINITION" ))
372+ .inputValueDefinition (new InputValueDefinition (FETCHER_DIRECTIVE_NAME , newType .apply (GraphQLString )))
356373 .build ());
357-
358374 return typeDefinitionRegistry ;
359375 }
360376
@@ -365,7 +381,7 @@ private void handleCustomConnectionTypes(TypeDefinitionRegistry typeDefinitionRe
365381 ObjectTypeExtensionDefinition query = typeDefinitionRegistry .objectTypeExtensions ().get ("Query" ).get (queryIndex );
366382 List <FieldDefinition > fields = query .getFieldDefinitions ();
367383
368- //Collect connection fields i. e. ones that map to <TypeName>Connection
384+ //Collect connection fields i.e. ones that map to <TypeName>Connection
369385 for (FieldDefinition f : fields ) {
370386 if (f .getName ().endsWith (SDLConstants .CONNECTION_QUERY_SUFFIX ) && f .getType () instanceof TypeName ) {
371387 String connectionName = ((TypeName ) f .getType ()).getName ();
0 commit comments