Skip to content

Commit

Permalink
PageUrl generation #589
Browse files Browse the repository at this point in the history
  • Loading branch information
anatol-sialitski committed Oct 12, 2023
1 parent ee4a6a9 commit 0374ab4
Show file tree
Hide file tree
Showing 21 changed files with 333 additions and 125 deletions.
4 changes: 1 addition & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import com.enonic.app.guillotine.graphql.fetchers.GetContentDataDataFetcher;
import com.enonic.app.guillotine.graphql.fetchers.GetContentFieldDataFetcher;
import com.enonic.app.guillotine.graphql.fetchers.GetContentParentDataFetcher;
import com.enonic.app.guillotine.graphql.fetchers.GetContentPathDataFetcher;
import com.enonic.app.guillotine.graphql.fetchers.GetContentReferencesDataFetcher;
import com.enonic.app.guillotine.graphql.fetchers.GetContentSiteDataFetcher;
import com.enonic.app.guillotine.graphql.fetchers.GetImageUrlDataFetcher;
Expand Down Expand Up @@ -162,7 +161,7 @@ private GraphQLFieldDefinition createMediaUrlField()

arguments.add( newArgument( "download", Scalars.GraphQLBoolean ) );
arguments.add( newArgument( "type", GraphQLTypeReference.typeRef( "UrlType" ) ) );
arguments.add( newArgument( "params", Scalars.GraphQLString ) );
arguments.add( newArgument( "params", ExtendedScalars.Json ) );

return outputField( "mediaUrl", Scalars.GraphQLString, arguments );
}
Expand Down Expand Up @@ -218,8 +217,7 @@ private List<GraphQLFieldDefinition> getGenericContentFields( String contentType

result.add( outputField( "_id", new GraphQLNonNull( Scalars.GraphQLID ) ) );
result.add( outputField( "_name", new GraphQLNonNull( Scalars.GraphQLString ) ) );
result.add( outputField( "_path", new GraphQLNonNull( Scalars.GraphQLString ),
List.of( newArgument( "type", GraphQLTypeReference.typeRef( "ContentPathType" ) ) ) ) );
result.add( outputField( "_path", new GraphQLNonNull( Scalars.GraphQLString ) ) );
result.add( outputField( "_references", new GraphQLList( GraphQLTypeReference.typeRef( "Content" ) ) ) );
result.add( outputField( "_score", Scalars.GraphQLFloat ) );
result.add( outputField( "creator", GraphQLTypeReference.typeRef( "PrincipalKey" ) ) );
Expand All @@ -245,9 +243,7 @@ private List<GraphQLFieldDefinition> getGenericContentFields( String contentType
newArgument( "resolveFragment", Scalars.GraphQLBoolean ) ) ) );
result.add( outputField( "attachments", new GraphQLList( GraphQLTypeReference.typeRef( "Attachment" ) ) ) );
result.add( outputField( "publish", GraphQLTypeReference.typeRef( "PublishInfo" ) ) );
result.add( outputField( "pageUrl", Scalars.GraphQLString,
List.of( newArgument( "type", GraphQLTypeReference.typeRef( "UrlType" ) ),
newArgument( "params", Scalars.GraphQLString ) ) ) );
result.add( outputField( "pageUrl", Scalars.GraphQLString, List.of( newArgument( "params", ExtendedScalars.Json ) ) ) );
result.add( outputField( "site", GraphQLTypeReference.typeRef( "portal_Site" ) ) );
result.add( outputField( "parent", GraphQLTypeReference.typeRef( "Content" ) ) );
result.add( outputField( "children", new GraphQLList( GraphQLTypeReference.typeRef( "Content" ) ),
Expand All @@ -258,8 +254,6 @@ private List<GraphQLFieldDefinition> getGenericContentFields( String contentType
newArgument( "sort", Scalars.GraphQLString ) ) ) );
result.add( outputField( "permissions", GraphQLTypeReference.typeRef( "Permissions" ) ) );

context.registerDataFetcher( contentType, "_path", new GetContentPathDataFetcher( serviceFacade.getContentService() ) );

context.registerDataFetcher( contentType, "contentType",
new ContentTypeDataFetcher( serviceFacade.getMixinService(), serviceFacade.getContentTypeService() ) );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ public void create()
{
registerEnum( "UrlType", "URL type.", List.of( "server", "absolute" ) );

registerEnum( "ContentPathType", "Content path type.", List.of( "siteRelative" ) );

registerEnum( "MediaIntentType", "Media intent type.", List.of( "download", "inline" ) );

registerEnum( "DslOperatorType", "DSL Operator type.", List.of( "OR", "AND" ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ private void createAttachmentType()
fields.add( outputField( "attachmentUrl", Scalars.GraphQLString, List.of( newArgument( "download", Scalars.GraphQLBoolean ),
newArgument( "type",
GraphQLTypeReference.typeRef( "UrlType" ) ),
newArgument( "params", Scalars.GraphQLString ) ) ) );
newArgument( "params", ExtendedScalars.Json ) ) ) );

GraphQLObjectType outputObject = newObject( context.uniqueName( "Attachment" ), "Attachment.", fields );
context.registerType( outputObject.getName(), outputObject );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import graphql.schema.DataFetchingEnvironment;

import com.enonic.app.guillotine.graphql.helper.GuillotineLocalContextHelper;
import com.enonic.app.guillotine.graphql.helper.ParamsUrHelper;
import com.enonic.xp.portal.PortalRequest;
import com.enonic.xp.portal.PortalRequestAccessor;
import com.enonic.xp.portal.url.AttachmentUrlParams;
Expand Down Expand Up @@ -41,6 +42,8 @@ private String doGet( final DataFetchingEnvironment environment )
Objects.toString( environment.getArgument( "download" ), "false" ) ).type( environment.getArgument( "type" ) ).portalRequest(
portalRequest );

ParamsUrHelper.resolveParams( params.getParams(), environment.getArgument( "params" ) );

return portalUrlService.attachmentUrl( params );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import com.enonic.app.guillotine.graphql.Constants;
import com.enonic.app.guillotine.graphql.helper.GuillotineLocalContextHelper;
import com.enonic.app.guillotine.graphql.helper.ParamsUrHelper;
import com.enonic.xp.portal.PortalRequest;
import com.enonic.xp.portal.PortalRequestAccessor;
import com.enonic.xp.portal.url.AttachmentUrlParams;
Expand Down Expand Up @@ -41,6 +42,9 @@ private String doGet( final DataFetchingEnvironment environment )
AttachmentUrlParams params = new AttachmentUrlParams().id( attachmentAsMap.get( Constants.CONTENT_ID_FIELD ).toString() ).name(
attachmentAsMap.get( "name" ).toString() ).download( Objects.toString( environment.getArgument( "download" ), "false" ) ).type(
environment.getArgument( "type" ) ).portalRequest( portalRequest );

ParamsUrHelper.resolveParams( params.getParams(), environment.getArgument( "params" ) );

return portalUrlService.attachmentUrl( params );
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import graphql.schema.DataFetchingEnvironment;

import com.enonic.app.guillotine.graphql.helper.GuillotineLocalContextHelper;
import com.enonic.app.guillotine.graphql.helper.ParamsUrHelper;
import com.enonic.xp.portal.PortalRequest;
import com.enonic.xp.portal.PortalRequestAccessor;
import com.enonic.xp.portal.url.ImageUrlParams;
Expand Down Expand Up @@ -63,6 +64,8 @@ private String doGet( final DataFetchingEnvironment environment )
params.type( environment.getArgument( "type" ) );
}

ParamsUrHelper.resolveParams( params.getParams(), environment.getArgument( "params" ) );

return portalUrlService.imageUrl( params );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import graphql.schema.DataFetchingEnvironment;

import com.enonic.app.guillotine.graphql.helper.GuillotineLocalContextHelper;
import com.enonic.app.guillotine.graphql.helper.ParamsUrHelper;
import com.enonic.xp.portal.PortalRequest;
import com.enonic.xp.portal.PortalRequestAccessor;
import com.enonic.xp.portal.url.PageUrlParams;
Expand Down Expand Up @@ -38,7 +39,9 @@ private String doGet( final DataFetchingEnvironment environment )

PageUrlParams params =
new PageUrlParams().id( sourceAsMap.get( "_id" ).toString() ).type( environment.getArgument( "type" ) ).portalRequest(
portalRequest ); // TODO include params
portalRequest );

ParamsUrHelper.resolveParams( params.getParams(), environment.getArgument( "params" ) );

return portalUrlService.pageUrl( params );
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.enonic.app.guillotine.graphql.helper;

import java.util.Map;
import java.util.Objects;

import com.google.common.collect.Multimap;

public class ParamsUrHelper

Check warning on line 8 in src/main/java/com/enonic/app/guillotine/graphql/helper/ParamsUrHelper.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/enonic/app/guillotine/graphql/helper/ParamsUrHelper.java#L8

Added line #L8 was not covered by tests
{
public static void resolveParams( final Multimap<String, String> holder, final Object originalParams )
{
if ( originalParams instanceof Map )
{
for ( Map.Entry<String, Object> entry : ( (Map<String, Object>) originalParams ).entrySet() )
{
applyParam( holder, entry.getKey(), entry.getValue() );
}
}
}

private static void applyParam( final Multimap<String, String> holder, final String key, final Object value )
{
if ( value instanceof Iterable )
{
applyParam( holder, key, (Iterable) value );
}
else
{
holder.put( key, Objects.toString( value, null ) );
}
}

private static void applyParam( final Multimap<String, String> holder, final String key, final Iterable values )
{
for ( final Object value : values )
{
holder.put( key, Objects.toString( value, null ) );
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.enonic.app.guillotine.graphql;

import java.util.Map;

import org.junit.jupiter.api.Test;

import graphql.schema.GraphQLSchema;

import com.enonic.app.guillotine.graphql.helper.CastHelper;
import com.enonic.xp.content.ContentId;
import com.enonic.xp.portal.url.AttachmentUrlParams;

import static com.enonic.app.guillotine.graphql.ResourceHelper.readGraphQLQuery;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

public class GetMediaContentGraphQLIntegrationTest
extends BaseGraphQLIntegrationTest
{
@Override
protected void initialize()
throws Exception
{
super.initialize();
}

@Test
public void testMediaAndAttachmentUrls()
{
when( serviceFacade.getPortalUrlService().attachmentUrl( any( AttachmentUrlParams.class ) ) ).thenReturn( "url?a=1&b=2&b=3&c" );
when( contentService.getById( ContentId.from( "contentId" ) ) ).thenReturn( ContentFixtures.createMediaContent() );

GraphQLSchema graphQLSchema = getBean().createSchema();

Map<String, Object> result = executeQuery( graphQLSchema, readGraphQLQuery( "graphql/getMediaContent.graphql" ) );

assertFalse( result.containsKey( "errors" ) );
assertTrue( result.containsKey( "data" ) );

Map<String, Object> attachmentUrlField = CastHelper.cast( getFieldFromGuillotine( result, "attachmentUrl" ) );
assertEquals( "url?a=1&b=2&b=3&c", attachmentUrlField.get( "mediaUrl" ) );
}

@Test
public void testDownloadAttachmentUrl()
{
when( serviceFacade.getPortalUrlService().attachmentUrl( any( AttachmentUrlParams.class ) ) ).thenReturn( "url?download" );
when( contentService.getById( ContentId.from( "contentId" ) ) ).thenReturn( ContentFixtures.createMediaContent() );

GraphQLSchema graphQLSchema = getBean().createSchema();

Map<String, Object> result = executeQuery( graphQLSchema, readGraphQLQuery( "graphql/getMediaContent.graphql" ) );

assertFalse( result.containsKey( "errors" ) );
assertTrue( result.containsKey( "data" ) );

Map<String, Object> downloadAttachmentUrlField = CastHelper.cast( getFieldFromGuillotine( result, "downloadAttachmentUrl" ) );
assertEquals( "url?download", downloadAttachmentUrlField.get( "mediaUrl" ) );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.enonic.app.guillotine.graphql;

import java.util.Locale;
import java.util.Map;

import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import graphql.schema.GraphQLSchema;

import com.enonic.app.guillotine.graphql.helper.CastHelper;
import com.enonic.xp.content.ContentId;
import com.enonic.xp.content.ContentPath;
import com.enonic.xp.portal.url.PageUrlParams;
import com.enonic.xp.site.Site;

import static com.enonic.app.guillotine.graphql.ResourceHelper.readGraphQLQuery;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

public class GetPageUrlGraphQLIntegrationTest
extends BaseGraphQLIntegrationTest
{
@Test
public void testMediaAndAttachmentUrls()
{
when( serviceFacade.getPortalUrlService().pageUrl( any( PageUrlParams.class ) ) ).thenReturn( "pageUrl" );
when( contentService.getById( ContentId.from( "contentId" ) ) ).thenReturn( ContentFixtures.createMediaContent() );
when( contentService.getByPath( Mockito.any() ) ).thenReturn( Site.create().
description( "Site" ).
name( "test-site" ).
parentPath( ContentPath.ROOT ).
language( Locale.ENGLISH ).
build() );

GraphQLSchema graphQLSchema = getBean().createSchema();

Map<String, Object> result = executeQuery( graphQLSchema, readGraphQLQuery( "graphql/getPageUrl.graphql" ) );

assertFalse( result.containsKey( "errors" ) );
assertTrue( result.containsKey( "data" ) );

Map<String, Object> getField = CastHelper.cast( getFieldFromGuillotine( result, "get" ) );
assertEquals( "pageUrl", getField.get( "pageUrl" ) );
}
}
Loading

0 comments on commit 0374ab4

Please sign in to comment.