Search in sources :

Example 1 with SchemaProblem

use of graphql.schema.idl.errors.SchemaProblem 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;
}
Also used : SchemaRedefinitionError(graphql.schema.idl.errors.SchemaRedefinitionError) ObjectTypeExtensionDefinition(graphql.language.ObjectTypeExtensionDefinition) InputObjectTypeExtensionDefinition(graphql.language.InputObjectTypeExtensionDefinition) ArrayList(java.util.ArrayList) InterfaceTypeExtensionDefinition(graphql.language.InterfaceTypeExtensionDefinition) UnionTypeDefinition(graphql.language.UnionTypeDefinition) ScalarTypeDefinition(graphql.language.ScalarTypeDefinition) ObjectTypeDefinition(graphql.language.ObjectTypeDefinition) TypeDefinition(graphql.language.TypeDefinition) InterfaceTypeDefinition(graphql.language.InterfaceTypeDefinition) LinkedHashMap(java.util.LinkedHashMap) ScalarTypeDefinition(graphql.language.ScalarTypeDefinition) SchemaProblem(graphql.schema.idl.errors.SchemaProblem) GraphQLError(graphql.GraphQLError) EnumTypeExtensionDefinition(graphql.language.EnumTypeExtensionDefinition) ScalarTypeExtensionDefinition(graphql.language.ScalarTypeExtensionDefinition) InputObjectTypeExtensionDefinition(graphql.language.InputObjectTypeExtensionDefinition) UnionTypeExtensionDefinition(graphql.language.UnionTypeExtensionDefinition)

Example 2 with SchemaProblem

use of graphql.schema.idl.errors.SchemaProblem in project graphql-java by graphql-java.

the class SchemaParser method buildRegistry.

/**
 * special method to build directly a TypeDefinitionRegistry from a Document
 * useful for Introspection =&gt; IDL (Document) =&gt; TypeDefinitionRegistry
 *
 * @param document containing type definitions
 *
 * @return the TypeDefinitionRegistry containing all type definitions from the document
 *
 * @throws SchemaProblem if an error occurs
 */
public TypeDefinitionRegistry buildRegistry(Document document) {
    List<GraphQLError> errors = new ArrayList<>();
    TypeDefinitionRegistry typeRegistry = new TypeDefinitionRegistry();
    List<Definition> definitions = document.getDefinitions();
    for (Definition definition : definitions) {
        typeRegistry.add(definition).ifPresent(errors::add);
    }
    if (errors.size() > 0) {
        throw new SchemaProblem(errors);
    } else {
        return typeRegistry;
    }
}
Also used : SchemaProblem(graphql.schema.idl.errors.SchemaProblem) ArrayList(java.util.ArrayList) Definition(graphql.language.Definition) GraphQLError(graphql.GraphQLError)

Aggregations

GraphQLError (graphql.GraphQLError)2 SchemaProblem (graphql.schema.idl.errors.SchemaProblem)2 ArrayList (java.util.ArrayList)2 Definition (graphql.language.Definition)1 EnumTypeExtensionDefinition (graphql.language.EnumTypeExtensionDefinition)1 InputObjectTypeExtensionDefinition (graphql.language.InputObjectTypeExtensionDefinition)1 InterfaceTypeDefinition (graphql.language.InterfaceTypeDefinition)1 InterfaceTypeExtensionDefinition (graphql.language.InterfaceTypeExtensionDefinition)1 ObjectTypeDefinition (graphql.language.ObjectTypeDefinition)1 ObjectTypeExtensionDefinition (graphql.language.ObjectTypeExtensionDefinition)1 ScalarTypeDefinition (graphql.language.ScalarTypeDefinition)1 ScalarTypeExtensionDefinition (graphql.language.ScalarTypeExtensionDefinition)1 TypeDefinition (graphql.language.TypeDefinition)1 UnionTypeDefinition (graphql.language.UnionTypeDefinition)1 UnionTypeExtensionDefinition (graphql.language.UnionTypeExtensionDefinition)1 SchemaRedefinitionError (graphql.schema.idl.errors.SchemaRedefinitionError)1 LinkedHashMap (java.util.LinkedHashMap)1