Search in sources :

Example 1 with ObjectTypeDefinition

use of graphql.language.ObjectTypeDefinition in project graphql-java by graphql-java.

the class GraphqlAntlrToLanguage method visitObjectTypeDefinition.

@Override
public Void visitObjectTypeDefinition(GraphqlParser.ObjectTypeDefinitionContext ctx) {
    ObjectTypeDefinition def = new ObjectTypeDefinition(ctx.name().getText());
    newNode(def, ctx);
    def.setDescription(newDescription(ctx.description()));
    result.getDefinitions().add(def);
    addContextProperty(ContextProperty.ObjectTypeDefinition, def);
    super.visitChildren(ctx);
    popContext();
    return null;
}
Also used : ObjectTypeDefinition(graphql.language.ObjectTypeDefinition) InputObjectTypeDefinition(graphql.language.InputObjectTypeDefinition)

Example 2 with ObjectTypeDefinition

use of graphql.language.ObjectTypeDefinition in project graphql-java by graphql-java.

the class SchemaTypeChecker method checkObjTypeFields.

private void checkObjTypeFields(List<GraphQLError> errors, ObjectTypeDefinition typeDefinition, List<FieldDefinition> fieldDefinitions) {
    // field unique ness
    checkNamedUniqueness(errors, fieldDefinitions, FieldDefinition::getName, (name, fieldDef) -> new NonUniqueNameError(typeDefinition, fieldDef));
    // field arg unique ness
    fieldDefinitions.forEach(fld -> checkNamedUniqueness(errors, fld.getInputValueDefinitions(), InputValueDefinition::getName, (name, inputValueDefinition) -> new NonUniqueArgumentError(typeDefinition, fld, name)));
    // directive checks
    fieldDefinitions.forEach(fld -> checkNamedUniqueness(errors, fld.getDirectives(), Directive::getName, (directiveName, directive) -> new NonUniqueDirectiveError(typeDefinition, fld, directiveName)));
    fieldDefinitions.forEach(fld -> fld.getDirectives().forEach(directive -> {
        checkDeprecatedDirective(errors, directive, () -> new InvalidDeprecationDirectiveError(typeDefinition, fld));
        checkNamedUniqueness(errors, directive.getArguments(), Argument::getName, (argumentName, argument) -> new NonUniqueArgumentError(typeDefinition, fld, argumentName));
    }));
}
Also used : MissingInterfaceTypeError(graphql.schema.idl.errors.MissingInterfaceTypeError) BiFunction(java.util.function.BiFunction) InputValueDefinition(graphql.language.InputValueDefinition) SchemaDefinition(graphql.language.SchemaDefinition) MissingInterfaceFieldError(graphql.schema.idl.errors.MissingInterfaceFieldError) EnumTypeDefinition(graphql.language.EnumTypeDefinition) Type(graphql.language.Type) MissingScalarImplementationError(graphql.schema.idl.errors.MissingScalarImplementationError) EnumValueDefinition(graphql.language.EnumValueDefinition) ObjectTypeExtensionDefinition(graphql.language.ObjectTypeExtensionDefinition) GraphQLError(graphql.GraphQLError) Map(java.util.Map) TypeName(graphql.language.TypeName) QueryOperationMissingError(graphql.schema.idl.errors.QueryOperationMissingError) OperationTypeDefinition(graphql.language.OperationTypeDefinition) ObjectTypeDefinition(graphql.language.ObjectTypeDefinition) InterfaceFieldRedefinitionError(graphql.schema.idl.errors.InterfaceFieldRedefinitionError) Predicate(java.util.function.Predicate) Collection(java.util.Collection) FieldDefinition(graphql.language.FieldDefinition) Set(java.util.Set) TypeDefinition(graphql.language.TypeDefinition) Collectors(java.util.stream.Collectors) BinaryOperator(java.util.function.BinaryOperator) AstPrinter(graphql.language.AstPrinter) MissingTypeError(graphql.schema.idl.errors.MissingTypeError) List(java.util.List) InterfaceTypeDefinition(graphql.language.InterfaceTypeDefinition) Optional(java.util.Optional) NonUniqueArgumentError(graphql.schema.idl.errors.NonUniqueArgumentError) InterfaceFieldArgumentRedefinitionError(graphql.schema.idl.errors.InterfaceFieldArgumentRedefinitionError) SchemaMissingError(graphql.schema.idl.errors.SchemaMissingError) NonUniqueDirectiveError(graphql.schema.idl.errors.NonUniqueDirectiveError) Internal(graphql.Internal) InputObjectTypeDefinition(graphql.language.InputObjectTypeDefinition) SchemaProblem(graphql.schema.idl.errors.SchemaProblem) MissingTypeResolverError(graphql.schema.idl.errors.MissingTypeResolverError) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) UnionTypeDefinition(graphql.language.UnionTypeDefinition) NonUniqueNameError(graphql.schema.idl.errors.NonUniqueNameError) OperationTypesMustBeObjects(graphql.schema.idl.errors.OperationTypesMustBeObjects) InvalidDeprecationDirectiveError(graphql.schema.idl.errors.InvalidDeprecationDirectiveError) MissingInterfaceFieldArgumentsError(graphql.schema.idl.errors.MissingInterfaceFieldArgumentsError) Directive(graphql.language.Directive) Consumer(java.util.function.Consumer) Argument(graphql.language.Argument) StringValue(graphql.language.StringValue) Collections(java.util.Collections) InvalidDeprecationDirectiveError(graphql.schema.idl.errors.InvalidDeprecationDirectiveError) FieldDefinition(graphql.language.FieldDefinition) NonUniqueArgumentError(graphql.schema.idl.errors.NonUniqueArgumentError) NonUniqueDirectiveError(graphql.schema.idl.errors.NonUniqueDirectiveError) NonUniqueNameError(graphql.schema.idl.errors.NonUniqueNameError)

Example 3 with ObjectTypeDefinition

use of graphql.language.ObjectTypeDefinition in project graphql-java by graphql-java.

the class TypeDefinitionRegistry method isPossibleType.

/**
 * Returns true of the abstract type is in implemented by the object type
 *
 * @param abstractType       the abstract type to check (interface or union)
 * @param possibleObjectType the object type to check
 *
 * @return true if the object type implements the abstract type
 */
@SuppressWarnings("ConstantConditions")
public boolean isPossibleType(Type abstractType, Type possibleObjectType) {
    if (!isInterfaceOrUnion(abstractType)) {
        return false;
    }
    if (!isObjectType(possibleObjectType)) {
        return false;
    }
    ObjectTypeDefinition targetObjectTypeDef = getType(possibleObjectType, ObjectTypeDefinition.class).get();
    TypeDefinition abstractTypeDef = getType(abstractType).get();
    if (abstractTypeDef instanceof UnionTypeDefinition) {
        List<Type> memberTypes = ((UnionTypeDefinition) abstractTypeDef).getMemberTypes();
        for (Type memberType : memberTypes) {
            Optional<ObjectTypeDefinition> checkType = getType(memberType, ObjectTypeDefinition.class);
            if (checkType.isPresent()) {
                if (checkType.get().getName().equals(targetObjectTypeDef.getName())) {
                    return true;
                }
            }
        }
        return false;
    } else {
        InterfaceTypeDefinition iFace = (InterfaceTypeDefinition) abstractTypeDef;
        List<ObjectTypeDefinition> objectTypeDefinitions = getImplementationsOf(iFace);
        return objectTypeDefinitions.stream().anyMatch(od -> od.getName().equals(targetObjectTypeDef.getName()));
    }
}
Also used : ObjectTypeDefinition(graphql.language.ObjectTypeDefinition) Type(graphql.language.Type) UnionTypeDefinition(graphql.language.UnionTypeDefinition) InterfaceTypeDefinition(graphql.language.InterfaceTypeDefinition) UnionTypeDefinition(graphql.language.UnionTypeDefinition) ScalarTypeDefinition(graphql.language.ScalarTypeDefinition) ObjectTypeDefinition(graphql.language.ObjectTypeDefinition) TypeDefinition(graphql.language.TypeDefinition) InterfaceTypeDefinition(graphql.language.InterfaceTypeDefinition)

Example 4 with ObjectTypeDefinition

use of graphql.language.ObjectTypeDefinition in project graphql-java by graphql-java.

the class SchemaGenerator method buildOutputType.

/**
 * This is the main recursive spot that builds out the various forms of Output types
 *
 * @param buildCtx the context we need to work out what we are doing
 * @param rawType  the type to be built
 *
 * @return an output type
 */
@SuppressWarnings("unchecked")
private <T extends GraphQLOutputType> T buildOutputType(BuildContext buildCtx, Type rawType) {
    TypeDefinition typeDefinition = buildCtx.getTypeDefinition(rawType);
    TypeInfo typeInfo = TypeInfo.typeInfo(rawType);
    GraphQLOutputType outputType = buildCtx.hasOutputType(typeDefinition);
    if (outputType != null) {
        return typeInfo.decorate(outputType);
    }
    if (buildCtx.stackContains(typeInfo)) {
        // otherwise we will go into an infinite loop
        return typeInfo.decorate(typeRef(typeInfo.getName()));
    }
    buildCtx.push(typeInfo);
    if (typeDefinition instanceof ObjectTypeDefinition) {
        outputType = buildObjectType(buildCtx, (ObjectTypeDefinition) typeDefinition);
    } else if (typeDefinition instanceof InterfaceTypeDefinition) {
        outputType = buildInterfaceType(buildCtx, (InterfaceTypeDefinition) typeDefinition);
    } else if (typeDefinition instanceof UnionTypeDefinition) {
        outputType = buildUnionType(buildCtx, (UnionTypeDefinition) typeDefinition);
    } else if (typeDefinition instanceof EnumTypeDefinition) {
        outputType = buildEnumType(buildCtx, (EnumTypeDefinition) typeDefinition);
    } else if (typeDefinition instanceof ScalarTypeDefinition) {
        outputType = buildScalar(buildCtx, (ScalarTypeDefinition) typeDefinition);
    } else {
        // typeDefinition is not a valid output type
        throw new NotAnOutputTypeError(typeDefinition);
    }
    buildCtx.put(outputType);
    buildCtx.pop();
    return (T) typeInfo.decorate(outputType);
}
Also used : ScalarTypeDefinition(graphql.language.ScalarTypeDefinition) ObjectTypeDefinition(graphql.language.ObjectTypeDefinition) InputObjectTypeDefinition(graphql.language.InputObjectTypeDefinition) GraphQLOutputType(graphql.schema.GraphQLOutputType) INPUT_OBJECT(graphql.introspection.Introspection.DirectiveLocation.INPUT_OBJECT) OBJECT(graphql.introspection.Introspection.DirectiveLocation.OBJECT) EnumTypeDefinition(graphql.language.EnumTypeDefinition) InterfaceTypeDefinition(graphql.language.InterfaceTypeDefinition) UnionTypeDefinition(graphql.language.UnionTypeDefinition) NotAnOutputTypeError(graphql.schema.idl.errors.NotAnOutputTypeError) EnumTypeDefinition(graphql.language.EnumTypeDefinition) OperationTypeDefinition(graphql.language.OperationTypeDefinition) ObjectTypeDefinition(graphql.language.ObjectTypeDefinition) TypeDefinition(graphql.language.TypeDefinition) InterfaceTypeDefinition(graphql.language.InterfaceTypeDefinition) InputObjectTypeDefinition(graphql.language.InputObjectTypeDefinition) UnionTypeDefinition(graphql.language.UnionTypeDefinition) ScalarTypeDefinition(graphql.language.ScalarTypeDefinition)

Example 5 with ObjectTypeDefinition

use of graphql.language.ObjectTypeDefinition 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)

Aggregations

ObjectTypeDefinition (graphql.language.ObjectTypeDefinition)14 InputObjectTypeDefinition (graphql.language.InputObjectTypeDefinition)10 TypeDefinition (graphql.language.TypeDefinition)10 FieldDefinition (graphql.language.FieldDefinition)8 InterfaceTypeDefinition (graphql.language.InterfaceTypeDefinition)8 UnionTypeDefinition (graphql.language.UnionTypeDefinition)8 EnumTypeDefinition (graphql.language.EnumTypeDefinition)7 OperationTypeDefinition (graphql.language.OperationTypeDefinition)7 ScalarTypeDefinition (graphql.language.ScalarTypeDefinition)7 Map (java.util.Map)7 Type (graphql.language.Type)6 TypeName (graphql.language.TypeName)6 List (java.util.List)6 GraphQLError (graphql.GraphQLError)5 Directive (graphql.language.Directive)5 EnumValueDefinition (graphql.language.EnumValueDefinition)5 InputValueDefinition (graphql.language.InputValueDefinition)5 ArrayList (java.util.ArrayList)5 Optional (java.util.Optional)5 Collectors (java.util.stream.Collectors)5