graphql.schema.GraphQLDirectiveContainer Java Examples

The following examples show how to use graphql.schema.GraphQLDirectiveContainer. 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: DirectivesAndTypeWalker.java    From samples with MIT License 5 votes vote down vote up
private static boolean walkInputType(GraphQLInputType inputType, List<GraphQLDirective> directives, BiFunction<GraphQLInputType, GraphQLDirective, Boolean> isSuitable) {
  GraphQLInputType unwrappedInputType = Util.unwrapNonNull(inputType);
  for (GraphQLDirective directive : directives) {
    if (isSuitable.apply(unwrappedInputType, directive)) {
      return true;
    }
  }
  if (unwrappedInputType instanceof GraphQLInputObjectType) {
    GraphQLInputObjectType inputObjType = (GraphQLInputObjectType) unwrappedInputType;
    for (GraphQLInputObjectField inputField : inputObjType.getFieldDefinitions()) {
      inputType = inputField.getType();
      directives = inputField.getDirectives();

      if (walkInputType(inputType, directives, isSuitable)) {
        return true;
      }
    }
  }
  if (unwrappedInputType instanceof GraphQLList) {
    GraphQLInputType innerListType = Util.unwrapOneAndAllNonNull(unwrappedInputType);
    if (innerListType instanceof GraphQLDirectiveContainer) {
      directives = ((GraphQLDirectiveContainer) innerListType).getDirectives();
      if (walkInputType(innerListType, directives, isSuitable)) {
        return true;
      }
    }
  }
  return false;
}
 
Example #2
Source File: RangeDirective.java    From samples with MIT License 5 votes vote down vote up
private List<GraphQLError> apply(GraphQLDirectiveContainer it, DataFetchingEnvironment env, Object value) {
  GraphQLDirective directive = it.getDirective("range");
  double min = (double) directive.getArgument("min").getValue();
  double max = (double) directive.getArgument("max").getValue();

  if (value instanceof Double && ((double) value < min || (double) value > max)) {
    return singletonList(
        GraphqlErrorBuilder.newError(env)
            .message(String.format("Argument value %s is out of range. The range is %s to %s.", value, min, max))
            .build()
    );
  }
  return emptyList();
}
 
Example #3
Source File: DirectiveTest.java    From graphql-spqr with Apache License 2.0 5 votes vote down vote up
private void assertDirective(GraphQLDirectiveContainer container, String directiveName, String innerName) {
    Optional<graphql.schema.GraphQLArgument> argument = DirectivesUtil.directiveWithArg(container.getDirectives(), directiveName, "value");
    assertTrue(argument.isPresent());
    GraphQLInputObjectType argType = (GraphQLInputObjectType) GraphQLUtils.unwrapNonNull(argument.get().getType());
    assertEquals("WrapperInput", argType.getName());
    assertSame(Scalars.GraphQLString, argType.getFieldDefinition("name").getType());
    assertSame(Scalars.GraphQLString, argType.getFieldDefinition("value").getType());
    Wrapper wrapper = (Wrapper) argument.get().getValue();
    assertEquals(innerName, wrapper.name());
    assertEquals("test", wrapper.value());
}
 
Example #4
Source File: SchemaTransformer.java    From federation-jvm with MIT License 4 votes vote down vote up
@NotNull
public final GraphQLSchema build() throws SchemaProblem {
    final List<GraphQLError> errors = new ArrayList<>();

    // Make new Schema
    final GraphQLSchema.Builder newSchema = GraphQLSchema.newSchema(originalSchema);

    final GraphQLObjectType originalQueryType = originalSchema.getQueryType();

    final GraphQLCodeRegistry.Builder newCodeRegistry =
            GraphQLCodeRegistry.newCodeRegistry(originalSchema.getCodeRegistry());

    // Print the original schema as sdl and expose it as query { _service { sdl } }
    final String sdl = sdl(originalSchema);
    final GraphQLObjectType.Builder newQueryType = GraphQLObjectType.newObject(originalQueryType)
            .field(_Service.field);
    newCodeRegistry.dataFetcher(FieldCoordinates.coordinates(
            originalQueryType.getName(),
            _Service.fieldName
            ),
            (DataFetcher<Object>) environment -> DUMMY);
    newCodeRegistry.dataFetcher(FieldCoordinates.coordinates(
            _Service.typeName,
            _Service.sdlFieldName
            ),
            (DataFetcher<String>) environment -> sdl);

    // Collecting all entity types: Types with @key directive and all types that implement them
    final Set<String> entityTypeNames = originalSchema.getAllTypesAsList().stream()
            .filter(t -> t instanceof GraphQLDirectiveContainer &&
                    ((GraphQLDirectiveContainer) t).getDirective(FederationDirectives.keyName) != null)
            .map(GraphQLNamedType::getName)
            .collect(Collectors.toSet());

    final Set<String> entityConcreteTypeNames = originalSchema.getAllTypesAsList()
            .stream()
            .filter(type -> type instanceof GraphQLObjectType)
            .filter(type -> entityTypeNames.contains(type.getName()) ||
                    ((GraphQLObjectType) type).getInterfaces()
                            .stream()
                            .anyMatch(itf -> entityTypeNames.contains(itf.getName())))
            .map(GraphQLNamedType::getName)
            .collect(Collectors.toSet());

    // If there are entity types install: Query._entities(representations: [_Any!]!): [_Entity]!
    if (!entityConcreteTypeNames.isEmpty()) {
        newQueryType.field(_Entity.field(entityConcreteTypeNames));

        final GraphQLType originalAnyType = originalSchema.getType(_Any.typeName);
        if (originalAnyType == null) {
            newSchema.additionalType(_Any.type(coercingForAny));
        }

        if (entityTypeResolver != null) {
            newCodeRegistry.typeResolver(_Entity.typeName, entityTypeResolver);
        } else {
            if (!newCodeRegistry.hasTypeResolver(_Entity.typeName)) {
                errors.add(new FederationError("Missing a type resolver for _Entity"));
            }
        }

        final FieldCoordinates _entities = FieldCoordinates.coordinates(originalQueryType.getName(), _Entity.fieldName);
        if (entitiesDataFetcher != null) {
            newCodeRegistry.dataFetcher(_entities, entitiesDataFetcher);
        } else if (entitiesDataFetcherFactory != null) {
            newCodeRegistry.dataFetcher(_entities, entitiesDataFetcherFactory);
        } else if (!newCodeRegistry.hasDataFetcher(_entities)) {
            errors.add(new FederationError("Missing a data fetcher for _entities"));
        }
    }

    if (!errors.isEmpty()) {
        throw new SchemaProblem(errors);
    }

    return newSchema
            .query(newQueryType.build())
            .codeRegistry(newCodeRegistry.build())
            .build();
}
 
Example #5
Source File: RangeDirective.java    From samples with MIT License 4 votes vote down vote up
private boolean appliesTo(GraphQLDirectiveContainer container) {
  return container.getDirective("range") != null;
}
 
Example #6
Source File: Directives.java    From graphql-spqr with Apache License 2.0 4 votes vote down vote up
public static boolean isMappedType(GraphQLType type) {
    return type instanceof GraphQLDirectiveContainer && DirectivesUtil.directiveWithArg(((GraphQLDirectiveContainer) type).getDirectives(), MAPPED_TYPE, TYPE).isPresent();
}
 
Example #7
Source File: Directives.java    From graphql-spqr with Apache License 2.0 4 votes vote down vote up
public static AnnotatedType getMappedType(GraphQLType type) {
    return DirectivesUtil.directiveWithArg(((GraphQLDirectiveContainer) type).getDirectives(), MAPPED_TYPE, TYPE)
            .map(arg -> (AnnotatedType) arg.getValue())
            .orElseThrow(() -> new IllegalArgumentException("GraphQL type " + name(type) + " does not have a mapped Java type"));
}