Search in sources :

Example 1 with ObjectTypeExtensionDefinition

use of graphql.language.ObjectTypeExtensionDefinition 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();
}
Also used : Arrays(java.util.Arrays) Value(graphql.language.Value) INPUT_FIELD_DEFINITION(graphql.introspection.Introspection.DirectiveLocation.INPUT_FIELD_DEFINITION) GraphQLInputObjectType(graphql.schema.GraphQLInputObjectType) GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) GraphQLInterfaceType(graphql.schema.GraphQLInterfaceType) InputValueDefinition(graphql.language.InputValueDefinition) GraphQLInputObjectField(graphql.schema.GraphQLInputObjectField) GraphQLUnionType(graphql.schema.GraphQLUnionType) SchemaDefinition(graphql.language.SchemaDefinition) GraphQLEnumValueDefinition(graphql.schema.GraphQLEnumValueDefinition) UNION(graphql.introspection.Introspection.DirectiveLocation.UNION) EnumTypeDefinition(graphql.language.EnumTypeDefinition) Type(graphql.language.Type) INPUT_OBJECT(graphql.introspection.Introspection.DirectiveLocation.INPUT_OBJECT) TypeResolverProxy(graphql.schema.TypeResolverProxy) GraphQLEnumValueDefinition.newEnumValueDefinition(graphql.schema.GraphQLEnumValueDefinition.newEnumValueDefinition) DataFetcherFactory(graphql.schema.DataFetcherFactory) EnumValueDefinition(graphql.language.EnumValueDefinition) ObjectTypeExtensionDefinition(graphql.language.ObjectTypeExtensionDefinition) OBJECT(graphql.introspection.Introspection.DirectiveLocation.OBJECT) GraphQLError(graphql.GraphQLError) Map(java.util.Map) TypeName(graphql.language.TypeName) TypeResolver(graphql.schema.TypeResolver) OperationTypeDefinition(graphql.language.OperationTypeDefinition) GraphQLObjectType(graphql.schema.GraphQLObjectType) GraphQLDirective(graphql.schema.GraphQLDirective) NotAnInputTypeError(graphql.schema.idl.errors.NotAnInputTypeError) ObjectTypeDefinition(graphql.language.ObjectTypeDefinition) UnionTypeExtensionDefinition(graphql.language.UnionTypeExtensionDefinition) Collections.emptyList(java.util.Collections.emptyList) FieldDefinition(graphql.language.FieldDefinition) GraphQLInputType(graphql.schema.GraphQLInputType) Set(java.util.Set) TypeDefinition(graphql.language.TypeDefinition) GraphQLArgument(graphql.schema.GraphQLArgument) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) NotAnOutputTypeError(graphql.schema.idl.errors.NotAnOutputTypeError) List(java.util.List) Stream(java.util.stream.Stream) ENUM_VALUE(graphql.introspection.Introspection.DirectiveLocation.ENUM_VALUE) InterfaceTypeDefinition(graphql.language.InterfaceTypeDefinition) Optional(java.util.Optional) GraphQLEnumType(graphql.schema.GraphQLEnumType) InterfaceTypeExtensionDefinition(graphql.language.InterfaceTypeExtensionDefinition) DirectiveLocation(graphql.introspection.Introspection.DirectiveLocation) GraphQLScalarType(graphql.schema.GraphQLScalarType) InputObjectTypeDefinition(graphql.language.InputObjectTypeDefinition) SchemaProblem(graphql.schema.idl.errors.SchemaProblem) HashMap(java.util.HashMap) GraphQLType(graphql.schema.GraphQLType) Stack(java.util.Stack) ArrayList(java.util.ArrayList) ENUM(graphql.introspection.Introspection.DirectiveLocation.ENUM) Introspection(graphql.introspection.Introspection) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) UnionTypeDefinition(graphql.language.UnionTypeDefinition) DataFetcherFactories(graphql.schema.DataFetcherFactories) InputObjectTypeExtensionDefinition(graphql.language.InputObjectTypeExtensionDefinition) ScalarTypeExtensionDefinition(graphql.language.ScalarTypeExtensionDefinition) DataFetcher(graphql.schema.DataFetcher) GraphQLSchema(graphql.schema.GraphQLSchema) ScalarTypeDefinition(graphql.language.ScalarTypeDefinition) EnumTypeExtensionDefinition(graphql.language.EnumTypeExtensionDefinition) GraphQLOutputType(graphql.schema.GraphQLOutputType) Directive(graphql.language.Directive) GraphQLTypeReference(graphql.schema.GraphQLTypeReference) PublicApi(graphql.PublicApi) Assert.assertNotNull(graphql.Assert.assertNotNull) GraphQLTypeReference.typeRef(graphql.schema.GraphQLTypeReference.typeRef) Collections(java.util.Collections) ObjectTypeExtensionDefinition(graphql.language.ObjectTypeExtensionDefinition) InputObjectTypeExtensionDefinition(graphql.language.InputObjectTypeExtensionDefinition) GraphQLObjectType(graphql.schema.GraphQLObjectType) GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition)

Example 2 with ObjectTypeExtensionDefinition

use of graphql.language.ObjectTypeExtensionDefinition 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 3 with ObjectTypeExtensionDefinition

use of graphql.language.ObjectTypeExtensionDefinition 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);
}
Also used : Arrays(java.util.Arrays) Value(graphql.language.Value) INPUT_FIELD_DEFINITION(graphql.introspection.Introspection.DirectiveLocation.INPUT_FIELD_DEFINITION) GraphQLInputObjectType(graphql.schema.GraphQLInputObjectType) GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) GraphQLInterfaceType(graphql.schema.GraphQLInterfaceType) InputValueDefinition(graphql.language.InputValueDefinition) GraphQLInputObjectField(graphql.schema.GraphQLInputObjectField) GraphQLUnionType(graphql.schema.GraphQLUnionType) SchemaDefinition(graphql.language.SchemaDefinition) GraphQLEnumValueDefinition(graphql.schema.GraphQLEnumValueDefinition) UNION(graphql.introspection.Introspection.DirectiveLocation.UNION) EnumTypeDefinition(graphql.language.EnumTypeDefinition) Type(graphql.language.Type) INPUT_OBJECT(graphql.introspection.Introspection.DirectiveLocation.INPUT_OBJECT) TypeResolverProxy(graphql.schema.TypeResolverProxy) GraphQLEnumValueDefinition.newEnumValueDefinition(graphql.schema.GraphQLEnumValueDefinition.newEnumValueDefinition) DataFetcherFactory(graphql.schema.DataFetcherFactory) EnumValueDefinition(graphql.language.EnumValueDefinition) ObjectTypeExtensionDefinition(graphql.language.ObjectTypeExtensionDefinition) OBJECT(graphql.introspection.Introspection.DirectiveLocation.OBJECT) GraphQLError(graphql.GraphQLError) Map(java.util.Map) TypeName(graphql.language.TypeName) TypeResolver(graphql.schema.TypeResolver) OperationTypeDefinition(graphql.language.OperationTypeDefinition) GraphQLObjectType(graphql.schema.GraphQLObjectType) GraphQLDirective(graphql.schema.GraphQLDirective) NotAnInputTypeError(graphql.schema.idl.errors.NotAnInputTypeError) ObjectTypeDefinition(graphql.language.ObjectTypeDefinition) UnionTypeExtensionDefinition(graphql.language.UnionTypeExtensionDefinition) Collections.emptyList(java.util.Collections.emptyList) FieldDefinition(graphql.language.FieldDefinition) GraphQLInputType(graphql.schema.GraphQLInputType) Set(java.util.Set) TypeDefinition(graphql.language.TypeDefinition) GraphQLArgument(graphql.schema.GraphQLArgument) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) NotAnOutputTypeError(graphql.schema.idl.errors.NotAnOutputTypeError) List(java.util.List) Stream(java.util.stream.Stream) ENUM_VALUE(graphql.introspection.Introspection.DirectiveLocation.ENUM_VALUE) InterfaceTypeDefinition(graphql.language.InterfaceTypeDefinition) Optional(java.util.Optional) GraphQLEnumType(graphql.schema.GraphQLEnumType) InterfaceTypeExtensionDefinition(graphql.language.InterfaceTypeExtensionDefinition) DirectiveLocation(graphql.introspection.Introspection.DirectiveLocation) GraphQLScalarType(graphql.schema.GraphQLScalarType) InputObjectTypeDefinition(graphql.language.InputObjectTypeDefinition) SchemaProblem(graphql.schema.idl.errors.SchemaProblem) HashMap(java.util.HashMap) GraphQLType(graphql.schema.GraphQLType) Stack(java.util.Stack) ArrayList(java.util.ArrayList) ENUM(graphql.introspection.Introspection.DirectiveLocation.ENUM) Introspection(graphql.introspection.Introspection) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) UnionTypeDefinition(graphql.language.UnionTypeDefinition) DataFetcherFactories(graphql.schema.DataFetcherFactories) InputObjectTypeExtensionDefinition(graphql.language.InputObjectTypeExtensionDefinition) ScalarTypeExtensionDefinition(graphql.language.ScalarTypeExtensionDefinition) DataFetcher(graphql.schema.DataFetcher) GraphQLSchema(graphql.schema.GraphQLSchema) ScalarTypeDefinition(graphql.language.ScalarTypeDefinition) EnumTypeExtensionDefinition(graphql.language.EnumTypeExtensionDefinition) GraphQLOutputType(graphql.schema.GraphQLOutputType) Directive(graphql.language.Directive) GraphQLTypeReference(graphql.schema.GraphQLTypeReference) PublicApi(graphql.PublicApi) Assert.assertNotNull(graphql.Assert.assertNotNull) GraphQLTypeReference.typeRef(graphql.schema.GraphQLTypeReference.typeRef) Collections(java.util.Collections) GraphQLInterfaceType(graphql.schema.GraphQLInterfaceType) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

GraphQLError (graphql.GraphQLError)3 EnumTypeExtensionDefinition (graphql.language.EnumTypeExtensionDefinition)3 InputObjectTypeExtensionDefinition (graphql.language.InputObjectTypeExtensionDefinition)3 InterfaceTypeDefinition (graphql.language.InterfaceTypeDefinition)3 InterfaceTypeExtensionDefinition (graphql.language.InterfaceTypeExtensionDefinition)3 ObjectTypeDefinition (graphql.language.ObjectTypeDefinition)3 ObjectTypeExtensionDefinition (graphql.language.ObjectTypeExtensionDefinition)3 ScalarTypeDefinition (graphql.language.ScalarTypeDefinition)3 ScalarTypeExtensionDefinition (graphql.language.ScalarTypeExtensionDefinition)3 TypeDefinition (graphql.language.TypeDefinition)3 UnionTypeDefinition (graphql.language.UnionTypeDefinition)3 UnionTypeExtensionDefinition (graphql.language.UnionTypeExtensionDefinition)3 Assert.assertNotNull (graphql.Assert.assertNotNull)2 PublicApi (graphql.PublicApi)2 Introspection (graphql.introspection.Introspection)2 DirectiveLocation (graphql.introspection.Introspection.DirectiveLocation)2 ENUM (graphql.introspection.Introspection.DirectiveLocation.ENUM)2 ENUM_VALUE (graphql.introspection.Introspection.DirectiveLocation.ENUM_VALUE)2 INPUT_FIELD_DEFINITION (graphql.introspection.Introspection.DirectiveLocation.INPUT_FIELD_DEFINITION)2 INPUT_OBJECT (graphql.introspection.Introspection.DirectiveLocation.INPUT_OBJECT)2