use of graphql.language.TypeDefinition in project graphql-java by graphql-java.
the class TypeDefinitionRegistry method merge.
/**
* This will merge these type registries together and return this one
*
* @param typeRegistry the registry to be merged into this one
*
* @return this registry
*
* @throws SchemaProblem if there are problems merging the types such as redefinitions
*/
public TypeDefinitionRegistry merge(TypeDefinitionRegistry typeRegistry) throws SchemaProblem {
List<GraphQLError> errors = new ArrayList<>();
Map<String, TypeDefinition> tempTypes = new LinkedHashMap<>();
typeRegistry.types.values().forEach(newEntry -> {
Optional<GraphQLError> defined = define(this.types, tempTypes, newEntry);
defined.ifPresent(errors::add);
});
Map<String, ScalarTypeDefinition> tempScalarTypes = new LinkedHashMap<>();
typeRegistry.scalarTypes.values().forEach(newEntry -> define(this.scalarTypes, tempScalarTypes, newEntry).ifPresent(errors::add));
if (typeRegistry.schema != null && this.schema != null) {
errors.add(new SchemaRedefinitionError(this.schema, typeRegistry.schema));
}
if (!errors.isEmpty()) {
throw new SchemaProblem(errors);
}
if (this.schema == null) {
// ensure schema is not overwritten by merge
this.schema = typeRegistry.schema;
}
// ok commit to the merge
this.types.putAll(tempTypes);
this.scalarTypes.putAll(tempScalarTypes);
//
// merge type extensions since they can be redefined by design
typeRegistry.typeExtensions.forEach((key, value) -> {
List<ObjectTypeExtensionDefinition> currentList = this.typeExtensions.computeIfAbsent(key, k -> new ArrayList<>());
currentList.addAll(value);
});
typeRegistry.interfaceTypeExtensions.forEach((key, value) -> {
List<InterfaceTypeExtensionDefinition> currentList = this.interfaceTypeExtensions.computeIfAbsent(key, k -> new ArrayList<>());
currentList.addAll(value);
});
typeRegistry.unionTypeExtensions.forEach((key, value) -> {
List<UnionTypeExtensionDefinition> currentList = this.unionTypeExtensions.computeIfAbsent(key, k -> new ArrayList<>());
currentList.addAll(value);
});
typeRegistry.enumTypeExtensions.forEach((key, value) -> {
List<EnumTypeExtensionDefinition> currentList = this.enumTypeExtensions.computeIfAbsent(key, k -> new ArrayList<>());
currentList.addAll(value);
});
typeRegistry.scalarTypeExtensions.forEach((key, value) -> {
List<ScalarTypeExtensionDefinition> currentList = this.scalarTypeExtensions.computeIfAbsent(key, k -> new ArrayList<>());
currentList.addAll(value);
});
typeRegistry.inputObjectTypeExtensions.forEach((key, value) -> {
List<InputObjectTypeExtensionDefinition> currentList = this.inputObjectTypeExtensions.computeIfAbsent(key, k -> new ArrayList<>());
currentList.addAll(value);
});
return this;
}
use of graphql.language.TypeDefinition in project graphql-java by graphql-java.
the class SchemaGenerator method buildObjectTypeInterfaces.
private void buildObjectTypeInterfaces(BuildContext buildCtx, ObjectTypeDefinition typeDefinition, GraphQLObjectType.Builder builder, List<ObjectTypeExtensionDefinition> extensions) {
Map<String, GraphQLInterfaceType> interfaces = new LinkedHashMap<>();
typeDefinition.getImplements().forEach(type -> {
GraphQLInterfaceType newInterfaceType = buildOutputType(buildCtx, type);
interfaces.put(newInterfaceType.getName(), newInterfaceType);
});
extensions.forEach(extension -> extension.getImplements().forEach(type -> {
GraphQLInterfaceType interfaceType = buildOutputType(buildCtx, type);
if (!interfaces.containsKey(interfaceType.getName())) {
interfaces.put(interfaceType.getName(), interfaceType);
}
}));
interfaces.values().forEach(builder::withInterface);
}
use of graphql.language.TypeDefinition in project graphql-java by graphql-java.
the class SchemaGenerator method buildInputType.
private GraphQLInputType buildInputType(BuildContext buildCtx, Type rawType) {
TypeDefinition typeDefinition = buildCtx.getTypeDefinition(rawType);
TypeInfo typeInfo = TypeInfo.typeInfo(rawType);
GraphQLInputType inputType = buildCtx.hasInputType(typeDefinition);
if (inputType != null) {
return typeInfo.decorate(inputType);
}
if (buildCtx.stackContains(typeInfo)) {
// we have circled around so put in a type reference and fix it later
return typeInfo.decorate(typeRef(typeInfo.getName()));
}
buildCtx.push(typeInfo);
if (typeDefinition instanceof InputObjectTypeDefinition) {
inputType = buildInputObjectType(buildCtx, (InputObjectTypeDefinition) typeDefinition);
} else if (typeDefinition instanceof EnumTypeDefinition) {
inputType = buildEnumType(buildCtx, (EnumTypeDefinition) typeDefinition);
} else if (typeDefinition instanceof ScalarTypeDefinition) {
inputType = buildScalar(buildCtx, (ScalarTypeDefinition) typeDefinition);
} else {
// typeDefinition is not a valid InputType
throw new NotAnInputTypeError(typeDefinition);
}
buildCtx.put(inputType);
buildCtx.pop();
return typeInfo.decorate(inputType);
}
use of graphql.language.TypeDefinition in project graphql-java by graphql-java.
the class SchemaGenerator method buildUnionType.
private GraphQLUnionType buildUnionType(BuildContext buildCtx, UnionTypeDefinition typeDefinition) {
GraphQLUnionType.Builder builder = GraphQLUnionType.newUnionType();
builder.definition(typeDefinition);
builder.name(typeDefinition.getName());
builder.description(schemaGeneratorHelper.buildDescription(typeDefinition, typeDefinition.getDescription()));
builder.typeResolver(getTypeResolverForUnion(buildCtx, typeDefinition));
List<UnionTypeExtensionDefinition> extensions = unionTypeExtensions(typeDefinition, buildCtx);
typeDefinition.getMemberTypes().forEach(mt -> {
GraphQLOutputType outputType = buildOutputType(buildCtx, mt);
if (outputType instanceof GraphQLTypeReference) {
builder.possibleType((GraphQLTypeReference) outputType);
} else {
builder.possibleType((GraphQLObjectType) outputType);
}
});
builder.withDirectives(buildDirectives(typeDefinition.getDirectives(), directivesOf(extensions), UNION));
extensions.forEach(extension -> extension.getMemberTypes().forEach(mt -> {
GraphQLOutputType outputType = buildOutputType(buildCtx, mt);
if (!builder.containType(outputType.getName())) {
if (outputType instanceof GraphQLTypeReference) {
builder.possibleType((GraphQLTypeReference) outputType);
} else {
builder.possibleType((GraphQLObjectType) outputType);
}
}
}));
return builder.build();
}
use of graphql.language.TypeDefinition in project graphql-java by graphql-java.
the class SchemaDiff method checkType.
private void checkType(DiffCtx ctx, Type oldType, Type newType) {
String typeName = getTypeName(oldType);
// prevent circular references
if (ctx.examiningType(typeName)) {
return;
}
if (isSystemScalar(typeName)) {
return;
}
if (isReservedType(typeName)) {
return;
}
Optional<TypeDefinition> oldTD = ctx.getOldTypeDef(oldType, TypeDefinition.class);
Optional<TypeDefinition> newTD = ctx.getNewTypeDef(newType, TypeDefinition.class);
if (!oldTD.isPresent()) {
ctx.report(DiffEvent.apiInfo().typeName(typeName).reasonMsg("Type '%s' is missing", typeName).build());
return;
}
TypeDefinition oldDef = oldTD.get();
ctx.report(DiffEvent.apiInfo().typeName(typeName).typeKind(getTypeKind(oldDef)).reasonMsg("Examining type '%s' ...", typeName).build());
if (!newTD.isPresent()) {
ctx.report(DiffEvent.apiBreakage().category(DiffCategory.MISSING).typeName(typeName).typeKind(getTypeKind(oldDef)).reasonMsg("The new API does not have a type called '%s'", typeName).build());
ctx.exitType();
return;
}
TypeDefinition newDef = newTD.get();
if (!oldDef.getClass().equals(newDef.getClass())) {
ctx.report(DiffEvent.apiBreakage().category(DiffCategory.INVALID).typeName(typeName).typeKind(getTypeKind(oldDef)).components(getTypeKind(oldDef), getTypeKind(newDef)).reasonMsg("The new API has changed '%s' from a '%s' to a '%s'", typeName, getTypeKind(oldDef), getTypeKind(newDef)).build());
ctx.exitType();
return;
}
if (oldDef instanceof ObjectTypeDefinition) {
checkObjectType(ctx, (ObjectTypeDefinition) oldDef, (ObjectTypeDefinition) newDef);
}
if (oldDef instanceof InterfaceTypeDefinition) {
checkInterfaceType(ctx, (InterfaceTypeDefinition) oldDef, (InterfaceTypeDefinition) newDef);
}
if (oldDef instanceof UnionTypeDefinition) {
checkUnionType(ctx, (UnionTypeDefinition) oldDef, (UnionTypeDefinition) newDef);
}
if (oldDef instanceof InputObjectTypeDefinition) {
checkInputObjectType(ctx, (InputObjectTypeDefinition) oldDef, (InputObjectTypeDefinition) newDef);
}
if (oldDef instanceof EnumTypeDefinition) {
checkEnumType(ctx, (EnumTypeDefinition) oldDef, (EnumTypeDefinition) newDef);
}
if (oldDef instanceof ScalarTypeDefinition) {
checkScalarType(ctx, (ScalarTypeDefinition) oldDef, (ScalarTypeDefinition) newDef);
}
ctx.exitType();
}
Aggregations