graphql.schema.idl.errors.SchemaProblem Java Examples

The following examples show how to use graphql.schema.idl.errors.SchemaProblem. 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: GraphQLFactory.java    From dropwizard-graphql with Apache License 2.0 6 votes vote down vote up
public GraphQLSchema build() throws SchemaProblem {
  if (graphQLSchema.isPresent()) {
    return graphQLSchema.get();
  }

  final SchemaParser parser = new SchemaParser();
  final TypeDefinitionRegistry registry = new TypeDefinitionRegistry();

  if (!schemaFiles.isEmpty()) {
    schemaFiles.stream()
        .filter(f -> !Strings.isNullOrEmpty(f))
        .map(f -> getResourceAsReader(f))
        .map(r -> parser.parse(r))
        .forEach(p -> registry.merge(p));
  }

  final SchemaGenerator generator = new SchemaGenerator();
  final GraphQLSchema schema = generator.makeExecutableSchema(registry, runtimeWiring);
  return schema;
}
 
Example #2
Source File: FederationTest.java    From federation-jvm with MIT License 5 votes vote down vote up
@Test
void testRequirements() {
    assertThrows(SchemaProblem.class, () ->
            Federation.transform(productSDL).build());
    assertThrows(SchemaProblem.class, () ->
            Federation.transform(productSDL).resolveEntityType(env -> null).build());
    assertThrows(SchemaProblem.class, () ->
            Federation.transform(productSDL).fetchEntities(env -> null).build());
}
 
Example #3
Source File: GeneratorBehavior.java    From smallrye-graphql with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldFailToGenerateApiWithInvalidSchema() {
    schema = "foo";
    Generator generator = givenGeneratorFor(HEROES);

    GraphQlGeneratorException thrown = catchThrowableOfType(generator::generateSourceFiles,
            GraphQlGeneratorException.class);

    then(thrown).hasMessage("can't parse schema: foo")
            .hasCauseInstanceOf(SchemaProblem.class);
    thenErrorCauseOf(thrown)
            .hasErrorType(InvalidSyntax)
            .hasMessage("Invalid Syntax : offending token 'foo' at line 1 column 1");
}
 
Example #4
Source File: GraphQLSchemaWithErrors.java    From js-graphql-intellij-plugin with MIT License 5 votes vote down vote up
public List<GraphQLError> getErrors() {
    final List<GraphQLException> rawErrors = Lists.newArrayList(exceptions);
    rawErrors.addAll(registry.getErrors());
    final List<GraphQLError> errors = Lists.newArrayList();
    for (GraphQLException exception : rawErrors) {
        if (exception instanceof SchemaProblem) {
            errors.addAll(((SchemaProblem) exception).getErrors());
        } else if (exception instanceof GraphQLError) {
            errors.add((GraphQLError) exception);
        } else {
            errors.add(new GraphQLInternalSchemaError(exception));
        }
    }
    return errors;
}
 
Example #5
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 #6
Source File: GeneratorBehavior.java    From smallrye-graphql with Apache License 2.0 4 votes vote down vote up
private GraphQlErrorAssert thenErrorCauseOf(GraphQlGeneratorException thrown) {
    List<GraphQLError> errors = ((SchemaProblem) thrown.getCause()).getErrors();
    then(errors).hasSize(1);
    GraphQLError error = errors.get(0);
    return new GraphQlErrorAssert(error);
}