use of graphql.schema.GraphQLFieldsContainer in project graphql-java by graphql-java.
the class OverlappingFieldsCanBeMerged method collectFieldsForField.
private void collectFieldsForField(Map<String, List<FieldAndType>> fieldMap, GraphQLType parentType, Field field) {
String responseName = field.getAlias() != null ? field.getAlias() : field.getName();
if (!fieldMap.containsKey(responseName)) {
fieldMap.put(responseName, new ArrayList<>());
}
GraphQLOutputType fieldType = null;
if (parentType instanceof GraphQLFieldsContainer) {
GraphQLFieldsContainer fieldsContainer = (GraphQLFieldsContainer) parentType;
GraphQLFieldDefinition fieldDefinition = getVisibleFieldDefinition(fieldsContainer, field);
fieldType = fieldDefinition != null ? fieldDefinition.getType() : null;
}
fieldMap.get(responseName).add(new FieldAndType(field, fieldType, parentType));
}
use of graphql.schema.GraphQLFieldsContainer in project timbuctoo by HuygensING.
the class PermissionBasedFieldVisibilityTest method getFieldDefinitionsShowsNonDataSetFields.
@Test
public void getFieldDefinitionsShowsNonDataSetFields() throws Exception {
final DataSetRepository dataSetRepository = mock(DataSetRepository.class);
DataSet dataSet = createDataSetWithUserPermissions("user__dataSetUserHasAccessTo", Sets.newHashSet(Permission.READ));
DataSet dataSet2 = createDataSetWithUserPermissions("user__dataSetUserDoesNotHasAccessTo", Sets.newHashSet());
Collection<DataSet> dataSetCollection = Sets.newHashSet(dataSet, dataSet2);
given(dataSetRepository.getDataSets()).willReturn(dataSetCollection);
final PermissionBasedFieldVisibility permissionBasedFieldVisibility = new PermissionBasedFieldVisibility(userPermissionCheck, dataSetRepository);
final GraphQLFieldsContainer graphQlFieldsContainer = createGraphQlFieldsContainer("user__dataSetUserHasAccessTo", "user__dataSetUserDoesNotHasAccessTo", "nonDataSetField");
List<GraphQLFieldDefinition> retrievedGraphQlFieldDefinitions = permissionBasedFieldVisibility.getFieldDefinitions(graphQlFieldsContainer);
assertThat(retrievedGraphQlFieldDefinitions, contains(hasProperty("name", is("user__dataSetUserHasAccessTo")), hasProperty("name", is("nonDataSetField"))));
}
use of graphql.schema.GraphQLFieldsContainer in project timbuctoo by HuygensING.
the class PermissionBasedFieldVisibilityTest method getFieldDefinitionReturnsFieldDefinitionIfNotDataSetField.
@Test
public void getFieldDefinitionReturnsFieldDefinitionIfNotDataSetField() throws Exception {
final DataSetRepository dataSetRepository = mock(DataSetRepository.class);
DataSet dataSet = createDataSetWithUserPermissions("user__dataSetUserHasAccessTo", Sets.newHashSet(Permission.READ));
DataSet dataSet2 = createDataSetWithUserPermissions("user__dataSetUserDoesNotHasAccessTo", Sets.newHashSet());
Collection<DataSet> dataSetCollection = Sets.newHashSet(dataSet, dataSet2);
given(dataSetRepository.getDataSets()).willReturn(dataSetCollection);
final PermissionBasedFieldVisibility permissionBasedFieldVisibility = new PermissionBasedFieldVisibility(userPermissionCheck, dataSetRepository);
final GraphQLFieldsContainer graphQlFieldsContainer = createGraphQlFieldsContainer("user__dataSetUserHasAccessTo", "user__dataSetUserDoesNotHasAccessTo", "nonDataSetField");
GraphQLFieldDefinition retrievedGraphQlFieldDefinition = permissionBasedFieldVisibility.getFieldDefinition(graphQlFieldsContainer, // new String to make sure the
new String("nonDataSetField"));
// contents are compared not the instance.
assertThat(retrievedGraphQlFieldDefinition, hasProperty("name", is("nonDataSetField")));
}
use of graphql.schema.GraphQLFieldsContainer in project graphql-java by graphql-java.
the class Introspection method getFieldDef.
/**
* This will look up a field definition by name, and understand that fields like __typename and __schema are special
* and take precedence in field resolution
*
* @param schema the schema to use
* @param parentType the type of the parent object
* @param fieldName the field to look up
*
* @return a field definition otherwise throws an assertion exception if its null
*/
public static GraphQLFieldDefinition getFieldDef(GraphQLSchema schema, GraphQLCompositeType parentType, String fieldName) {
if (schema.getQueryType() == parentType) {
if (fieldName.equals(SchemaMetaFieldDef.getName())) {
return SchemaMetaFieldDef;
}
if (fieldName.equals(TypeMetaFieldDef.getName())) {
return TypeMetaFieldDef;
}
}
if (fieldName.equals(TypeNameMetaFieldDef.getName())) {
return TypeNameMetaFieldDef;
}
assertTrue(parentType instanceof GraphQLFieldsContainer, "should not happen : parent type must be an object or interface %s", parentType);
GraphQLFieldsContainer fieldsContainer = (GraphQLFieldsContainer) parentType;
GraphQLFieldDefinition fieldDefinition = schema.getFieldVisibility().getFieldDefinition(fieldsContainer, fieldName);
Assert.assertTrue(fieldDefinition != null, "Unknown field '%s'", fieldName);
return fieldDefinition;
}
use of graphql.schema.GraphQLFieldsContainer in project timbuctoo by HuygensING.
the class PermissionBasedFieldVisibility method getFieldDefinition.
@Override
public GraphQLFieldDefinition getFieldDefinition(GraphQLFieldsContainer fieldsContainer, String fieldName) {
Collection<DataSet> dataSets = dataSetRepository.getDataSets();
Set<String> dataSetNamesWithOutReadPermission = dataSets.stream().filter(dataSet -> !userPermissionCheck.getPermissions(dataSet.getMetadata()).contains(Permission.READ)).map(dataSet -> dataSet.getMetadata().getCombinedId()).collect(Collectors.toSet());
Iterator<GraphQLFieldDefinition> graphQlFieldDefinitionIterator = fieldsContainer.getFieldDefinitions().iterator();
while (graphQlFieldDefinitionIterator.hasNext()) {
GraphQLFieldDefinition graphQlFieldDefinition = graphQlFieldDefinitionIterator.next();
if (!dataSetNamesWithOutReadPermission.contains(graphQlFieldDefinition.getName()) && graphQlFieldDefinition.getName().equals(fieldName)) {
return graphQlFieldDefinition;
}
}
return null;
}
Aggregations