use of graphql.language.TypeDefinition in project graphql-java by graphql-java.
the class SchemaGenerator method makeExecutableSchemaImpl.
private GraphQLSchema makeExecutableSchemaImpl(BuildContext buildCtx) {
GraphQLObjectType query;
GraphQLObjectType mutation;
GraphQLObjectType subscription;
GraphQLSchema.Builder schemaBuilder = GraphQLSchema.newSchema();
//
// Schema can be missing if the type is called 'Query'. Pre flight checks have checked that!
//
TypeDefinitionRegistry typeRegistry = buildCtx.getTypeRegistry();
if (!typeRegistry.schemaDefinition().isPresent()) {
@SuppressWarnings({ "OptionalGetWithoutIsPresent", "ConstantConditions" }) TypeDefinition queryTypeDef = typeRegistry.getType("Query").get();
query = buildOutputType(buildCtx, new TypeName(queryTypeDef.getName()));
schemaBuilder.query(query);
Optional<TypeDefinition> mutationTypeDef = typeRegistry.getType("Mutation");
if (mutationTypeDef.isPresent()) {
mutation = buildOutputType(buildCtx, new TypeName(mutationTypeDef.get().getName()));
schemaBuilder.mutation(mutation);
}
Optional<TypeDefinition> subscriptionTypeDef = typeRegistry.getType("Subscription");
if (subscriptionTypeDef.isPresent()) {
subscription = buildOutputType(buildCtx, new TypeName(subscriptionTypeDef.get().getName()));
schemaBuilder.subscription(subscription);
}
} else {
SchemaDefinition schemaDefinition = typeRegistry.schemaDefinition().get();
List<OperationTypeDefinition> operationTypes = schemaDefinition.getOperationTypeDefinitions();
// pre-flight checked via checker
@SuppressWarnings({ "OptionalGetWithoutIsPresent", "ConstantConditions" }) OperationTypeDefinition queryOp = operationTypes.stream().filter(op -> "query".equals(op.getName())).findFirst().get();
Optional<OperationTypeDefinition> mutationOp = operationTypes.stream().filter(op -> "mutation".equals(op.getName())).findFirst();
Optional<OperationTypeDefinition> subscriptionOp = operationTypes.stream().filter(op -> "subscription".equals(op.getName())).findFirst();
query = buildOperation(buildCtx, queryOp);
schemaBuilder.query(query);
if (mutationOp.isPresent()) {
mutation = buildOperation(buildCtx, mutationOp.get());
schemaBuilder.mutation(mutation);
}
if (subscriptionOp.isPresent()) {
subscription = buildOperation(buildCtx, subscriptionOp.get());
schemaBuilder.subscription(subscription);
}
}
Set<GraphQLType> additionalTypes = buildAdditionalTypes(buildCtx);
schemaBuilder.fieldVisibility(buildCtx.getWiring().getFieldVisibility());
return schemaBuilder.build(additionalTypes);
}
use of graphql.language.TypeDefinition in project graphql-java by graphql-java.
the class SchemaGenerator method buildEnumType.
private GraphQLEnumType buildEnumType(BuildContext buildCtx, EnumTypeDefinition typeDefinition) {
GraphQLEnumType.Builder builder = GraphQLEnumType.newEnum();
builder.definition(typeDefinition);
builder.name(typeDefinition.getName());
builder.description(schemaGeneratorHelper.buildDescription(typeDefinition, typeDefinition.getDescription()));
List<EnumTypeExtensionDefinition> extensions = enumTypeExtensions(typeDefinition, buildCtx);
EnumValuesProvider enumValuesProvider = buildCtx.getWiring().getEnumValuesProviders().get(typeDefinition.getName());
typeDefinition.getEnumValueDefinitions().forEach(evd -> {
GraphQLEnumValueDefinition enumValueDefinition = buildEnumValue(buildCtx, typeDefinition, enumValuesProvider, evd);
builder.value(enumValueDefinition);
});
extensions.forEach(extension -> extension.getEnumValueDefinitions().forEach(evd -> {
GraphQLEnumValueDefinition enumValueDefinition = buildEnumValue(buildCtx, typeDefinition, enumValuesProvider, evd);
if (!builder.hasValue(enumValueDefinition.getName())) {
builder.value(enumValueDefinition);
}
}));
builder.withDirectives(buildDirectives(typeDefinition.getDirectives(), directivesOf(extensions), ENUM));
return builder.build();
}
use of graphql.language.TypeDefinition in project graphql-java by graphql-java.
the class SchemaGenerator method buildObjectType.
private GraphQLObjectType buildObjectType(BuildContext buildCtx, ObjectTypeDefinition typeDefinition) {
GraphQLObjectType.Builder builder = GraphQLObjectType.newObject();
builder.definition(typeDefinition);
builder.name(typeDefinition.getName());
builder.description(schemaGeneratorHelper.buildDescription(typeDefinition, typeDefinition.getDescription()));
List<ObjectTypeExtensionDefinition> extensions = objectTypeExtensions(typeDefinition, buildCtx);
builder.withDirectives(buildDirectives(typeDefinition.getDirectives(), directivesOf(extensions), OBJECT));
typeDefinition.getFieldDefinitions().forEach(fieldDef -> {
GraphQLFieldDefinition newFieldDefinition = buildField(buildCtx, typeDefinition, fieldDef);
builder.field(newFieldDefinition);
});
extensions.forEach(extension -> extension.getFieldDefinitions().forEach(fieldDef -> {
GraphQLFieldDefinition newFieldDefinition = buildField(buildCtx, typeDefinition, fieldDef);
if (!builder.hasField(newFieldDefinition.getName())) {
builder.field(newFieldDefinition);
}
}));
buildObjectTypeInterfaces(buildCtx, typeDefinition, builder, extensions);
return builder.build();
}
use of graphql.language.TypeDefinition in project graphql-java by graphql-java.
the class SchemaDiff method checkOperation.
private void checkOperation(DiffCtx ctx, String opName, Optional<SchemaDefinition> oldSchemaDef, Optional<SchemaDefinition> newSchemaDef) {
// if schema declaration is missing then it is assumed to contain Query / Mutation / Subscription
Optional<OperationTypeDefinition> oldOpTypeDef;
oldOpTypeDef = oldSchemaDef.map(schemaDefinition -> getOpDef(opName, schemaDefinition)).orElseGet(() -> synthOperationTypeDefinition(type -> ctx.getOldTypeDef(type, ObjectTypeDefinition.class), opName));
Optional<OperationTypeDefinition> newOpTypeDef;
newOpTypeDef = newSchemaDef.map(schemaDefinition -> getOpDef(opName, schemaDefinition)).orElseGet(() -> synthOperationTypeDefinition(type -> ctx.getNewTypeDef(type, ObjectTypeDefinition.class), opName));
// must be new
if (!oldOpTypeDef.isPresent()) {
return;
}
ctx.report(DiffEvent.apiInfo().typeName(capitalize(opName)).typeKind(TypeKind.Operation).components(opName).reasonMsg("Examining operation '%s' ...", capitalize(opName)).build());
if (oldOpTypeDef.isPresent() && !newOpTypeDef.isPresent()) {
ctx.report(DiffEvent.apiBreakage().category(DiffCategory.MISSING).typeName(capitalize(opName)).typeKind(TypeKind.Operation).components(opName).reasonMsg("The new API no longer has the operation '%s'", opName).build());
return;
}
OperationTypeDefinition oldOpTypeDefinition = oldOpTypeDef.get();
OperationTypeDefinition newOpTypeDefinition = newOpTypeDef.get();
Type oldType = oldOpTypeDefinition.getType();
//
// if we have no old op, then it must have been added (which is ok)
Optional<TypeDefinition> oldTD = ctx.getOldTypeDef(oldType, TypeDefinition.class);
if (!oldTD.isPresent()) {
return;
}
checkType(ctx, oldType, newOpTypeDefinition.getType());
}
use of graphql.language.TypeDefinition in project graphql-java by graphql-java.
the class SchemaTypeChecker method checkTypeResolversArePresent.
private void checkTypeResolversArePresent(List<GraphQLError> errors, TypeDefinitionRegistry typeRegistry, RuntimeWiring wiring) {
Predicate<InterfaceTypeDefinition> noDynamicResolverForInterface = interaceTypeDef -> !wiring.getWiringFactory().providesTypeResolver(new InterfaceWiringEnvironment(typeRegistry, interaceTypeDef));
Predicate<UnionTypeDefinition> noDynamicResolverForUnion = unionTypeDef -> !wiring.getWiringFactory().providesTypeResolver(new UnionWiringEnvironment(typeRegistry, unionTypeDef));
Predicate<TypeDefinition> noTypeResolver = typeDefinition -> !wiring.getTypeResolvers().containsKey(typeDefinition.getName());
Consumer<TypeDefinition> addError = typeDefinition -> errors.add(new MissingTypeResolverError(typeDefinition));
typeRegistry.types().values().stream().filter(typeDef -> typeDef instanceof InterfaceTypeDefinition).map(InterfaceTypeDefinition.class::cast).filter(noDynamicResolverForInterface).filter(noTypeResolver).forEach(addError);
typeRegistry.types().values().stream().filter(typeDef -> typeDef instanceof UnionTypeDefinition).map(UnionTypeDefinition.class::cast).filter(noDynamicResolverForUnion).filter(noTypeResolver).forEach(addError);
}
Aggregations