Search in sources :

Example 1 with TypeDefinition

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

the class IntrospectionResultToSchema method createSchemaDefinition.

/**
 * Returns a IDL Document that reprSesents the schema as defined by the introspection result map
 *
 * @param introspectionResult the result of an introspection query on a schema
 *
 * @return a IDL Document of the schema
 */
@SuppressWarnings("unchecked")
public Document createSchemaDefinition(Map<String, Object> introspectionResult) {
    assertTrue(introspectionResult.get("__schema") != null, "__schema expected");
    Map<String, Object> schema = (Map<String, Object>) introspectionResult.get("__schema");
    Map<String, Object> queryType = (Map<String, Object>) schema.get("queryType");
    assertNotNull(queryType, "queryType expected");
    TypeName query = new TypeName((String) queryType.get("name"));
    boolean nonDefaultQueryName = !"Query".equals(query.getName());
    SchemaDefinition schemaDefinition = new SchemaDefinition();
    schemaDefinition.getOperationTypeDefinitions().add(new OperationTypeDefinition("query", query));
    Map<String, Object> mutationType = (Map<String, Object>) schema.get("mutationType");
    boolean nonDefaultMutationName = false;
    if (mutationType != null) {
        TypeName mutation = new TypeName((String) mutationType.get("name"));
        nonDefaultMutationName = !"Mutation".equals(mutation.getName());
        schemaDefinition.getOperationTypeDefinitions().add(new OperationTypeDefinition("mutation", mutation));
    }
    Map<String, Object> subscriptionType = (Map<String, Object>) schema.get("subscriptionType");
    boolean nonDefaultSubscriptionName = false;
    if (subscriptionType != null) {
        TypeName subscription = new TypeName((String) subscriptionType.get("name"));
        nonDefaultSubscriptionName = !"Subscription".equals(subscription.getName());
        schemaDefinition.getOperationTypeDefinitions().add(new OperationTypeDefinition("subscription", subscription));
    }
    Document document = new Document();
    if (nonDefaultQueryName || nonDefaultMutationName || nonDefaultSubscriptionName) {
        document.getDefinitions().add(schemaDefinition);
    }
    List<Map<String, Object>> types = (List<Map<String, Object>>) schema.get("types");
    for (Map<String, Object> type : types) {
        TypeDefinition typeDefinition = createTypeDefinition(type);
        if (typeDefinition == null)
            continue;
        document.getDefinitions().add(typeDefinition);
    }
    return document;
}
Also used : TypeName(graphql.language.TypeName) SchemaDefinition(graphql.language.SchemaDefinition) ArrayList(java.util.ArrayList) List(java.util.List) OperationTypeDefinition(graphql.language.OperationTypeDefinition) Document(graphql.language.Document) Map(java.util.Map) InputObjectTypeDefinition(graphql.language.InputObjectTypeDefinition) EnumTypeDefinition(graphql.language.EnumTypeDefinition) UnionTypeDefinition(graphql.language.UnionTypeDefinition) OperationTypeDefinition(graphql.language.OperationTypeDefinition) ScalarTypeDefinition(graphql.language.ScalarTypeDefinition) ObjectTypeDefinition(graphql.language.ObjectTypeDefinition) TypeDefinition(graphql.language.TypeDefinition) InterfaceTypeDefinition(graphql.language.InterfaceTypeDefinition)

Example 2 with TypeDefinition

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

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

the class SchemaTypeExtensionsChecker method checkTypeExtensionHasCorrespondingType.

private void checkTypeExtensionHasCorrespondingType(List<GraphQLError> errors, TypeDefinitionRegistry typeRegistry, String name, List<? extends TypeDefinition> extTypeList, Class<? extends TypeDefinition> targetClass) {
    TypeDefinition extensionDefinition = extTypeList.get(0);
    Optional<? extends TypeDefinition> typeDefinition = typeRegistry.getType(new TypeName(name), targetClass);
    if (!typeDefinition.isPresent()) {
        errors.add(new TypeExtensionMissingBaseTypeError(extensionDefinition));
    }
}
Also used : TypeName(graphql.language.TypeName) TypeExtensionMissingBaseTypeError(graphql.schema.idl.errors.TypeExtensionMissingBaseTypeError) InputObjectTypeDefinition(graphql.language.InputObjectTypeDefinition) EnumTypeDefinition(graphql.language.EnumTypeDefinition) UnionTypeDefinition(graphql.language.UnionTypeDefinition) ScalarTypeDefinition(graphql.language.ScalarTypeDefinition) ObjectTypeDefinition(graphql.language.ObjectTypeDefinition) TypeDefinition(graphql.language.TypeDefinition) InterfaceTypeDefinition(graphql.language.InterfaceTypeDefinition)

Example 4 with TypeDefinition

use of graphql.language.TypeDefinition 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 5 with TypeDefinition

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

Aggregations

InterfaceTypeDefinition (graphql.language.InterfaceTypeDefinition)15 ObjectTypeDefinition (graphql.language.ObjectTypeDefinition)15 TypeDefinition (graphql.language.TypeDefinition)15 UnionTypeDefinition (graphql.language.UnionTypeDefinition)15 EnumTypeDefinition (graphql.language.EnumTypeDefinition)13 InputObjectTypeDefinition (graphql.language.InputObjectTypeDefinition)13 ScalarTypeDefinition (graphql.language.ScalarTypeDefinition)13 OperationTypeDefinition (graphql.language.OperationTypeDefinition)12 Type (graphql.language.Type)9 TypeName (graphql.language.TypeName)9 ArrayList (java.util.ArrayList)9 GraphQLError (graphql.GraphQLError)8 ObjectTypeExtensionDefinition (graphql.language.ObjectTypeExtensionDefinition)8 SchemaDefinition (graphql.language.SchemaDefinition)8 SchemaProblem (graphql.schema.idl.errors.SchemaProblem)8 List (java.util.List)8 Map (java.util.Map)8 Directive (graphql.language.Directive)7 EnumValueDefinition (graphql.language.EnumValueDefinition)7 FieldDefinition (graphql.language.FieldDefinition)7