graphql.schema.GraphQLFieldsContainer Java Examples

The following examples show how to use graphql.schema.GraphQLFieldsContainer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PermissionBasedFieldVisibilityTest.java    From timbuctoo with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void getFieldDefinitionReturnsFieldDefinitionIfUserHasPermission() 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"
  );

  GraphQLFieldDefinition retrievedGraphQlFieldDefinition = permissionBasedFieldVisibility
    .getFieldDefinition(graphQlFieldsContainer,"user__dataSetUserHasAccessTo");

  assertThat(retrievedGraphQlFieldDefinition,hasProperty("name", is("user__dataSetUserHasAccessTo")));
}
 
Example #2
Source File: PermissionBasedFieldVisibilityTest.java    From timbuctoo with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void getFieldDefinitionReturnsNullIfUserHasNoPermission() 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"
  );

  GraphQLFieldDefinition retrievedGraphQlFieldDefinition = permissionBasedFieldVisibility
    .getFieldDefinition(graphQlFieldsContainer,"user__dataSetUserDoesNotHasAccessTo");

  assertThat(retrievedGraphQlFieldDefinition,is(nullValue()));
}
 
Example #3
Source File: ComplexityAnalyzer.java    From graphql-spqr with Apache License 2.0 6 votes vote down vote up
private ResolvedField collectFields(FieldCollectorParameters parameters, List<ResolvedField> fields) {
    ResolvedField field = fields.get(0);
    if (!fields.stream().allMatch(f -> f.getFieldType() instanceof GraphQLFieldsContainer)) {
        field.setComplexityScore(complexityFunction.getComplexity(field, 0));
        return field;
    }
    List<Field> rawFields = fields.stream().map(ResolvedField::getField).collect(Collectors.toList());
    Map<String, ResolvedField> children = collectFields(parameters, rawFields, (GraphQLFieldsContainer) field.getFieldType());
    ResolvedField node = new ResolvedField(field.getField(), field.getFieldDefinition(), field.getArguments(), children);
    int childScore = children.values().stream().mapToInt(ResolvedField::getComplexityScore).sum();
    int complexityScore = complexityFunction.getComplexity(node, childScore);
    if (complexityScore > maximumComplexity) {
        throw new ComplexityLimitExceededException(complexityScore, maximumComplexity);
    }
    node.setComplexityScore(complexityScore);
    return node;
}
 
Example #4
Source File: PermissionBasedFieldVisibilityTest.java    From timbuctoo with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void getFieldDefinitionsShowsOnlyDataSetsThatUserHasAccessTo() 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"
  );

  List<GraphQLFieldDefinition> retrievedGraphQlFieldDefinitions = permissionBasedFieldVisibility
    .getFieldDefinitions(graphQlFieldsContainer);

  assertThat(retrievedGraphQlFieldDefinitions, contains(hasProperty("name", is("user__dataSetUserHasAccessTo"))));
}
 
Example #5
Source File: PermissionBasedFieldVisibility.java    From timbuctoo with GNU General Public License v3.0 6 votes vote down vote up
@Override
public GraphQLFieldDefinition getFieldDefinition(GraphQLFieldsContainer fieldsContainer, String fieldName) {
  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;


}
 
Example #6
Source File: TypeRegistryTest.java    From graphql-spqr with Apache License 2.0 6 votes vote down vote up
private void assertSameType(GraphQLType t1, GraphQLType t2, GraphQLCodeRegistry code1, GraphQLCodeRegistry code2) {
    assertSame(t1, t2);
    if (t1 instanceof GraphQLInterfaceType) {
        GraphQLInterfaceType i = (GraphQLInterfaceType) t1;
        assertSame(code1.getTypeResolver(i), code2.getTypeResolver(i));
    }
    if (t1 instanceof GraphQLUnionType) {
        GraphQLUnionType u = (GraphQLUnionType) t1;
        assertSame(code1.getTypeResolver(u), code2.getTypeResolver(u));
    }
    if (t1 instanceof GraphQLFieldsContainer) {
        GraphQLFieldsContainer c = (GraphQLFieldsContainer) t1;
        c.getFieldDefinitions().forEach(fieldDef ->
                assertSame(code1.getDataFetcher(c, fieldDef), code2.getDataFetcher(c, fieldDef)));
    }
}
 
Example #7
Source File: UppercaseDirective.java    From samples with MIT License 6 votes vote down vote up
@Override
public GraphQLFieldDefinition onField(SchemaDirectiveWiringEnvironment<GraphQLFieldDefinition> env) {
  GraphQLFieldDefinition field = env.getElement();
  GraphQLFieldsContainer parentType = env.getFieldsContainer();

  // build a data fetcher that transforms the given value to uppercase
  DataFetcher originalFetcher = env.getCodeRegistry().getDataFetcher(parentType, field);
  DataFetcher dataFetcher = DataFetcherFactories
      .wrapDataFetcher(originalFetcher, ((dataFetchingEnvironment, value) -> {
        if (value instanceof String) {
          return ((String) value).toUpperCase();
        }
        return value;
      }));

  // now change the field definition to use the new uppercase data fetcher
  env.getCodeRegistry().dataFetcher(parentType, field, dataFetcher);
  return field;
}
 
Example #8
Source File: MethodDataFetcher.java    From graphql-apigen with Apache License 2.0 5 votes vote down vote up
private GraphQLFieldDefinition getFieldType(GraphQLType type) {
    if ( type instanceof GraphQLFieldsContainer ) {
    		GraphQLFieldDefinition fieldType = ((GraphQLFieldsContainer)type).getFieldDefinition(propertyName);
    		
    		if (null == fieldType && null != this.graphQLPropertyName) {
    			fieldType = ((GraphQLFieldsContainer)type).getFieldDefinition(graphQLPropertyName);
    		}
    		
    		return fieldType;
    }
    return null;
}
 
Example #9
Source File: PermissionBasedFieldVisibilityTest.java    From timbuctoo with GNU General Public License v3.0 5 votes vote down vote up
@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("nonDataSetField")); //new String to make sure the
  //contents are compared not the instance.

  assertThat(retrievedGraphQlFieldDefinition,hasProperty("name", is("nonDataSetField")));
}
 
Example #10
Source File: PermissionBasedFieldVisibilityTest.java    From timbuctoo with GNU General Public License v3.0 5 votes vote down vote up
@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"))));
}
 
Example #11
Source File: PermissionBasedFieldVisibility.java    From timbuctoo with GNU General Public License v3.0 5 votes vote down vote up
@Override
public List<GraphQLFieldDefinition> getFieldDefinitions(GraphQLFieldsContainer fieldsContainer) {
  List<GraphQLFieldDefinition> graphQlFieldDefinitions = new ArrayList<>();

  Iterator<GraphQLFieldDefinition> graphQlFieldDefinitionIterator =
      fieldsContainer.getFieldDefinitions().iterator();
  while (graphQlFieldDefinitionIterator.hasNext()) {
    GraphQLFieldDefinition graphQlFieldDefinition = graphQlFieldDefinitionIterator.next();
    if (!dataSetNamesWithOutReadPermission.contains(graphQlFieldDefinition.getName())) {
      graphQlFieldDefinitions.add(graphQlFieldDefinition);
    }
  }

  return graphQlFieldDefinitions;
}
 
Example #12
Source File: GraphQLFieldPsiElement.java    From js-graphql-intellij-plugin with MIT License 5 votes vote down vote up
@Override
public GraphQLType getTypeScope() {
    final GraphQLSchema schema = GraphQLTypeDefinitionRegistryServiceImpl.getService(getProject()).getSchema(this);
    final String fieldName = this.getName();
    if (schema != null && fieldName != null) {
        // the type scope for a field is the output type of the field, given the name of the field and its parent
        final GraphQLTypeScopeProvider parentTypeScopeProvider = PsiTreeUtil.getParentOfType(this, GraphQLTypeScopeProvider.class);
        if (parentTypeScopeProvider != null) {
            GraphQLType parentType = parentTypeScopeProvider.getTypeScope();
            if (parentType != null) {
                // found a parent operation, field, or fragment
                parentType = GraphQLUtil.getUnmodifiedType(parentType); // unwrap list, non-null since we want a specific field
                if (parentType instanceof GraphQLFieldsContainer) {
                    final graphql.schema.GraphQLFieldDefinition fieldDefinition = ((GraphQLFieldsContainer) parentType).getFieldDefinition(fieldName);
                    if (fieldDefinition != null) {
                        return fieldDefinition.getType();
                    } else if (fieldName.equals(GraphQLConstants.__TYPE)) {
                        return Introspection.__Type;
                    } else if (fieldName.equals(GraphQLConstants.__SCHEMA)) {
                        return Introspection.__Schema;
                    }
                }
            }
        }
    }
    return null;
}
 
Example #13
Source File: ComplexityAnalyzer.java    From graphql-spqr with Apache License 2.0 5 votes vote down vote up
private void collectField(FieldCollectorParameters parameters, Map<String, List<ResolvedField>> fields, Field field, GraphQLFieldsContainer parent) {
    if (!conditionalNodes.shouldInclude(parameters.getVariables(), field.getDirectives())) {
        return;
    }
    GraphQLFieldDefinition fieldDefinition = parent.getFieldDefinition(field.getName());
    Map<String, Object> argumentValues = valuesResolver.getArgumentValues(fieldDefinition.getArguments(), field.getArguments(), parameters.getVariables());
    ResolvedField node = new ResolvedField(field, fieldDefinition, argumentValues);
    fields.putIfAbsent(node.getName(), new ArrayList<>());
    fields.get(node.getName()).add(node);
}
 
Example #14
Source File: ComplexityAnalyzer.java    From graphql-spqr with Apache License 2.0 5 votes vote down vote up
private void collectFields(FieldCollectorParameters parameters, Map<String, List<ResolvedField>> fields, List<Selection> selectionSet,
                           List<String> visitedFragments, GraphQLFieldsContainer parent) {

    for (Selection selection : selectionSet) {
        if (selection instanceof Field) {
            collectField(parameters, fields, (Field) selection, parent);
        } else if (selection instanceof InlineFragment) {
            collectInlineFragment(parameters, fields, visitedFragments, (InlineFragment) selection, parent);
        } else if (selection instanceof FragmentSpread) {
            collectFragmentSpread(parameters, fields, visitedFragments, (FragmentSpread) selection, parent);
        }
    }
}
 
Example #15
Source File: ComplexityAnalyzer.java    From graphql-spqr with Apache License 2.0 5 votes vote down vote up
/**
 * Given a list of fields this will collect the sub-field selections and return it as a map
 *
 * @param parameters the parameters to this method
 * @param fields     the list of fields to collect for
 *
 * @return a map of the sub field selections
 */
private Map<String, ResolvedField> collectFields(FieldCollectorParameters parameters, List<Field> fields, GraphQLFieldsContainer parent) {
    List<String> visitedFragments = new ArrayList<>();
    Map<String, List<ResolvedField>> unconditionalSubFields = new LinkedHashMap<>();
    Map<String, Map<String, List<ResolvedField>>> conditionalSubFields = new LinkedHashMap<>();

    fields.stream()
            .filter(field -> field.getSelectionSet() != null)
            .forEach(field -> collectFields(parameters, unconditionalSubFields, getUnconditionalSelections(field.getSelectionSet()), visitedFragments, parent));

    fields.stream()
            .filter(field -> field.getSelectionSet() != null)
            .forEach(field ->
                    getConditionalSelections(field.getSelectionSet()).forEach((condition, selections) -> {
                                Map<String, List<ResolvedField>> subFields = new LinkedHashMap<>();
                                collectFields(parameters, subFields, selections, visitedFragments, parent);
                                conditionalSubFields.put(condition, subFields);
                            }
                    ));

    if (conditionalSubFields.isEmpty()) {
        return unconditionalSubFields.values().stream()
                .map(nodes -> collectFields(parameters, nodes))
                .collect(Collectors.toMap(ResolvedField::getName, Function.identity()));
    } else {
        return reduceAlternatives(parameters, unconditionalSubFields, conditionalSubFields);
    }
}
 
Example #16
Source File: GraphQLTypeAssertions.java    From graphql-spqr with Apache License 2.0 4 votes vote down vote up
public static void assertFieldNamesEqual(GraphQLFieldsContainer fieldsContainer, String... fieldNames) {
    Set<String> discoveredNames = fieldsContainer.getFieldDefinitions().stream()
            .map(GraphQLFieldDefinition::getName).collect(Collectors.toSet());
    assertEquals(fieldNames.length, discoveredNames.size());
    Arrays.stream(fieldNames).forEach(fldName -> assertTrue(discoveredNames.contains(fldName)));
}
 
Example #17
Source File: PermissionBasedFieldVisibilityTest.java    From timbuctoo with GNU General Public License v3.0 4 votes vote down vote up
private GraphQLFieldsContainer createGraphQlFieldsContainer(String... fieldNames) {
  List<GraphQLFieldDefinition> graphQlFieldDefinitions = new ArrayList<>();

  final GraphQLFieldsContainer graphQlFieldsContainer = mock(GraphQLFieldsContainer.class);


  for (String fieldName : fieldNames) {
    GraphQLFieldDefinition graphQlFieldDefinition = createGraphQlFieldDefinition(fieldName);
    graphQlFieldDefinitions.add(graphQlFieldDefinition);
    given(graphQlFieldsContainer.getFieldDefinition(fieldName)).willReturn(graphQlFieldDefinition);
  }


  given(graphQlFieldsContainer.getFieldDefinitions()).willReturn(graphQlFieldDefinitions);
  return graphQlFieldsContainer;
}
 
Example #18
Source File: GraphQLSchemaGenerator.java    From graphql-spqr with Apache License 2.0 4 votes vote down vote up
@Override
public DataFetcher<?> getDataFetcher(GraphQLFieldsContainer parentType, GraphQLFieldDefinition fieldDef) {
    return codeRegistry.getDataFetcher(parentType, fieldDef);
}
 
Example #19
Source File: GraphQLSchemaGenerator.java    From graphql-spqr with Apache License 2.0 4 votes vote down vote up
default DataFetcher<?> getDataFetcher(GraphQLFieldsContainer parentType, GraphQLFieldDefinition fieldDef) {
    return null;
}