Search in sources :

Example 1 with GraphQLFieldsContainer

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));
}
Also used : GraphQLOutputType(graphql.schema.GraphQLOutputType) GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) GraphQLFieldsContainer(graphql.schema.GraphQLFieldsContainer)

Example 2 with GraphQLFieldsContainer

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"))));
}
Also used : DataSet(nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet) DataSetRepository(nl.knaw.huygens.timbuctoo.v5.dataset.DataSetRepository) GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) GraphQLFieldsContainer(graphql.schema.GraphQLFieldsContainer) Test(org.junit.Test)

Example 3 with GraphQLFieldsContainer

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")));
}
Also used : DataSet(nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet) DataSetRepository(nl.knaw.huygens.timbuctoo.v5.dataset.DataSetRepository) GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) GraphQLFieldsContainer(graphql.schema.GraphQLFieldsContainer) Test(org.junit.Test)

Example 4 with GraphQLFieldsContainer

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;
}
Also used : GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) GraphQLFieldsContainer(graphql.schema.GraphQLFieldsContainer)

Example 5 with GraphQLFieldsContainer

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;
}
Also used : List(java.util.List) Iterator(java.util.Iterator) DataSetRepository(nl.knaw.huygens.timbuctoo.v5.dataset.DataSetRepository) GraphqlFieldVisibility(graphql.schema.visibility.GraphqlFieldVisibility) Collection(java.util.Collection) GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) Set(java.util.Set) GraphQLFieldsContainer(graphql.schema.GraphQLFieldsContainer) DataSet(nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet) Collectors(java.util.stream.Collectors) Permission(nl.knaw.huygens.timbuctoo.v5.security.dto.Permission) ArrayList(java.util.ArrayList) DataSet(nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet) GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition)

Aggregations

GraphQLFieldDefinition (graphql.schema.GraphQLFieldDefinition)10 GraphQLFieldsContainer (graphql.schema.GraphQLFieldsContainer)10 DataSetRepository (nl.knaw.huygens.timbuctoo.v5.dataset.DataSetRepository)7 DataSet (nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet)7 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)3 GraphqlFieldVisibility (graphql.schema.visibility.GraphqlFieldVisibility)2 Collection (java.util.Collection)2 Iterator (java.util.Iterator)2 List (java.util.List)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 Permission (nl.knaw.huygens.timbuctoo.v5.security.dto.Permission)2 GraphQLOutputType (graphql.schema.GraphQLOutputType)1