private Builder(ObjectTypeDefinition existing) { this.sourceLocation = existing.getSourceLocation(); this.comments = existing.getComments(); this.name = existing.getName(); this.description = existing.getDescription(); this.directives = existing.getDirectives(); this.implementz = existing.getImplements(); this.fieldDefinitions = existing.getFieldDefinitions(); this.ignoredChars = existing.getIgnoredChars(); }
@Override public ObjectTypeDefinition deepCopy() { return new ObjectTypeDefinition(name, deepCopy(implementz), deepCopy(directives), deepCopy(fieldDefinitions), description, getSourceLocation(), getComments(), getIgnoredChars() ); }
@Override public TraversalControl visitObjectTypeDefinition(ObjectTypeDefinition node, TraverserContext<Node> context) { ObjectTypeDefinition changedNode = node.transform(builder -> { builder.directives(sort(node.getDirectives(), comparing(Directive::getName))); builder.implementz(sort(node.getImplements(), comparingTypes())); builder.fieldDefinitions(sort(node.getFieldDefinitions(), comparing(FieldDefinition::getName))); }); return changeNode(context, changedNode); }
private NodePrinter<ObjectTypeDefinition> objectTypeDefinition() { return (out, node) -> { out.printf("%s", comments(node)); out.printf("%s", spaced( "type", node.getName(), wrap("implements ", join(node.getImplements(), " & "), ""), directives(node.getDirectives()), block(node.getFieldDefinitions()) )); }; }
builder.name(typeDefinition.getName()); builder.description(schemaGeneratorHelper.buildDescription(typeDefinition, typeDefinition.getDescription())); buildDirectives(typeDefinition.getDirectives(), directivesOf(extensions), OBJECT, buildCtx.getDirectiveDefinitions()) ); typeDefinition.getFieldDefinitions().forEach(fieldDef -> { FieldDefAndDirectiveParams fieldAndParams = buildField(buildCtx, typeDefinition, fieldDef); builder.field(fieldAndParams.fieldDefinition);
private void checkObjectType(DiffCtx ctx, ObjectTypeDefinition oldDef, ObjectTypeDefinition newDef) { Map<String, FieldDefinition> oldFields = sortedMap(oldDef.getFieldDefinitions(), FieldDefinition::getName); Map<String, FieldDefinition> newFields = sortedMap(newDef.getFieldDefinitions(), FieldDefinition::getName); checkFields(ctx, oldDef, oldFields, newDef, newFields); checkImplements(ctx, oldDef, oldDef.getImplements(), newDef.getImplements()); checkDirectives(ctx, oldDef, newDef); }
InterfaceTypeDefinition interfaceTypeDef = (InterfaceTypeDefinition) type.get(); Map<String, FieldDefinition> objectFields = objectTypeDef.getFieldDefinitions().stream() .collect(Collectors.toMap( FieldDefinition::getName, Function.identity(), mergeFirstValue()
private void checkInterfacesAreImplemented(List<GraphQLError> errors, TypeDefinitionRegistry typeRegistry) { Map<String, TypeDefinition> typesMap = typeRegistry.types(); // objects List<ObjectTypeDefinition> objectTypes = filterTo(typesMap, ObjectTypeDefinition.class); objectTypes.forEach(objectType -> { List<Type> implementsTypes = objectType.getImplements(); implementsTypes.forEach(checkInterfaceIsImplemented("object", typeRegistry, errors, objectType)); }); Map<String, List<ObjectTypeExtensionDefinition>> typeExtensions = typeRegistry.objectTypeExtensions(); typeExtensions.values().forEach(extList -> extList.forEach(typeExtension -> { List<Type> implementsTypes = typeExtension.getImplements(); implementsTypes.forEach(checkInterfaceIsImplemented("extension", typeRegistry, errors, typeExtension)); })); }
private List<ObjectTypeExtensionDefinition> objectTypeExtensions(ObjectTypeDefinition typeDefinition, BuildContext buildCtx) { return nvl(buildCtx.typeRegistry.objectTypeExtensions().get(typeDefinition.getName())); }
private static List<Directive> getDirectives(Definition def) { if ( def instanceof ObjectTypeDefinition ) { return ((ObjectTypeDefinition)def).getDirectives(); } if ( def instanceof InterfaceTypeDefinition ) { return ((InterfaceTypeDefinition)def).getDirectives(); } if ( def instanceof EnumTypeDefinition ) { return ((EnumTypeDefinition)def).getDirectives(); } if ( def instanceof ScalarTypeDefinition ) { return ((ScalarTypeDefinition)def).getDirectives(); } if ( def instanceof UnionTypeDefinition ) { return ((UnionTypeDefinition)def).getDirectives(); } if ( def instanceof InputObjectTypeDefinition ) { return ((InputObjectTypeDefinition)def).getDirectives(); } if ( def instanceof SchemaDefinition ) { return ((SchemaDefinition)def).getDirectives(); } return Collections.emptyList(); }
public ObjectTypeDefinition build() { ObjectTypeDefinition objectTypeDefinition = new ObjectTypeDefinition(name, implementz, directives, fieldDefinitions, description, sourceLocation, comments, ignoredChars); return objectTypeDefinition; } }
private AstDescriptionAndComments getDescriptionAndComments(Object descriptionHolder) { if (descriptionHolder instanceof GraphQLObjectType) { GraphQLObjectType type = (GraphQLObjectType) descriptionHolder; return descriptionAndComments(type::getDescription, type::getDefinition, () -> type.getDefinition().getDescription()); } else if (descriptionHolder instanceof GraphQLEnumType) { GraphQLEnumType type = (GraphQLEnumType) descriptionHolder;
private NodePrinter<ObjectTypeDefinition> objectTypeDefinition() { return (out, node) -> { out.printf("%s", comments(node)); out.printf("%s", spaced( "type", node.getName(), wrap("implements ", join(node.getImplements(), " & "), ""), directives(node.getDirectives()), block(node.getFieldDefinitions()) )); }; }
@Override public TraversalControl visitObjectTypeDefinition(ObjectTypeDefinition node, TraverserContext<Node> context) { ObjectTypeDefinition changedNode = node.transform(builder -> { builder.directives(sort(node.getDirectives(), comparing(Directive::getName))); builder.implementz(sort(node.getImplements(), comparingTypes())); builder.fieldDefinitions(sort(node.getFieldDefinitions(), comparing(FieldDefinition::getName))); }); return changeNode(context, changedNode); }
builder.name(typeDefinition.getName()); builder.description(schemaGeneratorHelper.buildDescription(typeDefinition, typeDefinition.getDescription())); buildDirectives(typeDefinition.getDirectives(), directivesOf(extensions), OBJECT, buildCtx.getDirectiveDefinitions()) ); typeDefinition.getFieldDefinitions().forEach(fieldDef -> { FieldDefAndDirectiveParams fieldAndParams = buildField(buildCtx, typeDefinition, fieldDef); builder.field(fieldAndParams.fieldDefinition);
objectTypes.forEach(objectType -> { List<Type> implementsTypes = objectType.getImplements(); implementsTypes.forEach(checkInterfaceTypeExists(typeRegistry, errors, objectType)); checkFieldTypesPresent(typeRegistry, errors, objectType, objectType.getFieldDefinitions());
private void checkFieldsAreSensible(List<GraphQLError> errors, TypeDefinitionRegistry typeRegistry) { Map<String, TypeDefinition> typesMap = typeRegistry.types(); // objects List<ObjectTypeDefinition> objectTypes = filterTo(typesMap, ObjectTypeDefinition.class); objectTypes.forEach(objectType -> checkObjTypeFields(errors, objectType, objectType.getFieldDefinitions())); // interfaces List<InterfaceTypeDefinition> interfaceTypes = filterTo(typesMap, InterfaceTypeDefinition.class); interfaceTypes.forEach(interfaceType -> checkInterfaceFields(errors, interfaceType, interfaceType.getFieldDefinitions())); // enum types List<EnumTypeDefinition> enumTypes = filterTo(typesMap, EnumTypeDefinition.class); enumTypes.forEach(enumType -> checkEnumValues(errors, enumType, enumType.getEnumValueDefinitions())); // input types List<InputObjectTypeDefinition> inputTypes = filterTo(typesMap, InputObjectTypeDefinition.class); inputTypes.forEach(inputType -> checkInputValues(errors, inputType, inputType.getInputValueDefinitions())); }
/** * Returns the list of object types that implement the given interface type * * @param targetInterface the target to search for * * @return the list of object types that implement the given interface type */ public List<ObjectTypeDefinition> getImplementationsOf(InterfaceTypeDefinition targetInterface) { List<ObjectTypeDefinition> objectTypeDefinitions = getTypes(ObjectTypeDefinition.class); return objectTypeDefinitions.stream().filter(objectTypeDefinition -> { List<Type> implementsList = objectTypeDefinition.getImplements(); for (Type iFace : implementsList) { Optional<InterfaceTypeDefinition> interfaceTypeDef = getType(iFace, InterfaceTypeDefinition.class); if (interfaceTypeDef.isPresent()) { boolean equals = interfaceTypeDef.get().getName().equals(targetInterface.getName()); if (equals) { return true; } } } return false; }).collect(Collectors.toList()); }
Optional<ObjectTypeDefinition> checkType = getType(memberType, ObjectTypeDefinition.class); if (checkType.isPresent()) { if (checkType.get().getName().equals(targetObjectTypeDef.getName())) { return true; List<ObjectTypeDefinition> objectTypeDefinitions = getImplementationsOf(iFace); return objectTypeDefinitions.stream() .anyMatch(od -> od.getName().equals(targetObjectTypeDef.getName()));
public ObjectTypeDefinition build() { ObjectTypeDefinition objectTypeDefinition = new ObjectTypeDefinition(name, implementz, directives, fieldDefinitions, description, sourceLocation, comments, ignoredChars); return objectTypeDefinition; } }