Skip to content

Commit

Permalink
Input type LONG is converted to GraphQLString. #744
Browse files Browse the repository at this point in the history
  • Loading branch information
anatol-sialitski committed Feb 16, 2024
1 parent 07c89d6 commit 554827f
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 5 deletions.
Expand Up @@ -18,6 +18,7 @@

import com.enonic.app.guillotine.ServiceFacade;
import com.enonic.app.guillotine.graphql.GuillotineContext;
import com.enonic.app.guillotine.graphql.fetchers.ContentDataFieldDataFetcher;
import com.enonic.app.guillotine.graphql.fetchers.ContentTypeDataFetcher;
import com.enonic.app.guillotine.graphql.fetchers.FormItemDataFetcher;
import com.enonic.app.guillotine.graphql.fetchers.GetAsJsonWithoutContentIdDataFetcher;
Expand Down Expand Up @@ -288,7 +289,7 @@ private List<GraphQLFieldDefinition> getGenericContentFields( String contentType

context.registerDataFetcher( contentType, "owner", new GetContentFieldDataFetcher( "owner" ) );

context.registerDataFetcher( contentType, "dataAsJson", new GetAsJsonWithoutContentIdDataFetcher( "data" ) );
context.registerDataFetcher( contentType, "dataAsJson", new ContentDataFieldDataFetcher() );

context.registerDataFetcher( contentType, "xAsJson", new GetAsJsonWithoutContentIdDataFetcher( "x" ) );

Expand Down
@@ -0,0 +1,27 @@
package com.enonic.app.guillotine.graphql.fetchers;

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

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

import com.enonic.app.guillotine.graphql.helper.CastHelper;
import com.enonic.app.guillotine.graphql.helper.DataFetcherHelper;

public class ContentDataFieldDataFetcher
implements DataFetcher<Object>
{
@Override
public Object get( final DataFetchingEnvironment environment )
throws Exception
{
Map<String, Object> sourceAsMap = CastHelper.cast( environment.getSource() );
Object data = CastHelper.cast( sourceAsMap.get( "data" ) );
if ( Objects.equals( sourceAsMap.get( "type" ), "portal:site" ) || Objects.equals( sourceAsMap.get( "_path" ), "/content" ) )
{
data = DataFetcherHelper.removeField( data, "siteConfig" );
}
return DataFetcherHelper.removeContentIdField( data );
}
}
Expand Up @@ -7,22 +7,27 @@

public class DataFetcherHelper
{
public static Object removeContentIdField( final Object object )
public static Object removeField( final Object object, final String fieldName )
{
if ( object instanceof Map )
{
Map<String, Object> map = CastHelper.cast( object );
map.remove( Constants.CONTENT_ID_FIELD );
map.keySet().forEach( key -> removeContentIdField( map.get( key ) ) );
map.remove( fieldName );
map.keySet().forEach( key -> removeField( map.get( key ), fieldName ) );
}
if ( object instanceof Collection )
{
Collection<?> collection = CastHelper.cast( object );
for ( Object item : collection )
{
removeContentIdField( item );
removeField( item, fieldName );
}
}
return object;
}

public static Object removeContentIdField( final Object object )
{
return removeField( object, Constants.CONTENT_ID_FIELD );
}
}
Expand Up @@ -18,6 +18,7 @@
import com.enonic.xp.content.ContentId;
import com.enonic.xp.content.ContentIds;
import com.enonic.xp.content.ContentPath;
import com.enonic.xp.data.PropertySet;
import com.enonic.xp.data.PropertyTree;
import com.enonic.xp.form.FieldSet;
import com.enonic.xp.form.FormItemSet;
Expand Down Expand Up @@ -226,4 +227,31 @@ protected List<ContentType> getCustomContentTypes()

return result;
}

@Test
public void testGetContentDataAsJson()
{
final PropertyTree data = new PropertyTree();
data.addSet( "siteConfig", new PropertySet() );
data.addString( "k", "v" );

Site site = Site.create().name( "site" ).type( ContentTypeName.site() ).path( ContentPath.from( "/sitePath" ) ).parentPath(
ContentPath.ROOT ).data( data ).displayName( "Site" ).id( ContentId.from( "siteId" ) ).build();

when( contentService.getById( ContentId.from( "contentId" ) ) ).thenReturn( site );

GraphQLSchema graphQLSchema = getBean().createSchema();

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

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

Map<String, Object> getField = CastHelper.cast( getFieldFromGuillotine( result, "get" ) );
Map<String, Object> dataAsJson = CastHelper.cast( getField.get( "dataAsJson" ) );

assertFalse( dataAsJson.containsKey( "siteConfig" ) );
assertTrue( dataAsJson.containsKey( "k" ) );
}
}
7 changes: 7 additions & 0 deletions src/test/resources/graphql/getContentDataAsJson.graphql
@@ -0,0 +1,7 @@
query {
guillotine {
get(key: "contentId") {
dataAsJson
}
}
}

0 comments on commit 554827f

Please sign in to comment.