Search in sources :

Example 1 with TypeName

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

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

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

the class SchemaTypeExtensionsChecker method checkTypeExtensionDirectiveRedefinition.

@SuppressWarnings("unchecked")
private void checkTypeExtensionDirectiveRedefinition(List<GraphQLError> errors, TypeDefinitionRegistry typeRegistry, String name, List<? extends TypeDefinition> extensions, Class<? extends TypeDefinition> targetClass) {
    Optional<? extends TypeDefinition> typeDefinition = typeRegistry.getType(new TypeName(name), targetClass);
    if (typeDefinition.isPresent() && typeDefinition.get().getClass().equals(targetClass)) {
        List<Directive> directives = typeDefinition.get().getDirectives();
        Map<String, Directive> directiveMap = FpKit.getByName(directives, Directive::getName, mergeFirst());
        extensions.forEach(typeExt -> {
            List<Directive> extDirectives = typeExt.getDirectives();
            extDirectives.forEach(directive -> {
                if (directiveMap.containsKey(directive.getName())) {
                    errors.add(new TypeExtensionDirectiveRedefinitionError(typeDefinition.get(), directive));
                }
            });
        });
    }
}
Also used : TypeName(graphql.language.TypeName) TypeExtensionDirectiveRedefinitionError(graphql.schema.idl.errors.TypeExtensionDirectiveRedefinitionError) SchemaTypeChecker.checkDeprecatedDirective(graphql.schema.idl.SchemaTypeChecker.checkDeprecatedDirective) Directive(graphql.language.Directive)

Example 4 with TypeName

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

the class SchemaGenerator method makeExecutableSchemaImpl.

private GraphQLSchema makeExecutableSchemaImpl(BuildContext buildCtx) {
    GraphQLObjectType query;
    GraphQLObjectType mutation;
    GraphQLObjectType subscription;
    GraphQLSchema.Builder schemaBuilder = GraphQLSchema.newSchema();
    // 
    // Schema can be missing if the type is called 'Query'.  Pre flight checks have checked that!
    // 
    TypeDefinitionRegistry typeRegistry = buildCtx.getTypeRegistry();
    if (!typeRegistry.schemaDefinition().isPresent()) {
        @SuppressWarnings({ "OptionalGetWithoutIsPresent", "ConstantConditions" }) TypeDefinition queryTypeDef = typeRegistry.getType("Query").get();
        query = buildOutputType(buildCtx, new TypeName(queryTypeDef.getName()));
        schemaBuilder.query(query);
        Optional<TypeDefinition> mutationTypeDef = typeRegistry.getType("Mutation");
        if (mutationTypeDef.isPresent()) {
            mutation = buildOutputType(buildCtx, new TypeName(mutationTypeDef.get().getName()));
            schemaBuilder.mutation(mutation);
        }
        Optional<TypeDefinition> subscriptionTypeDef = typeRegistry.getType("Subscription");
        if (subscriptionTypeDef.isPresent()) {
            subscription = buildOutputType(buildCtx, new TypeName(subscriptionTypeDef.get().getName()));
            schemaBuilder.subscription(subscription);
        }
    } else {
        SchemaDefinition schemaDefinition = typeRegistry.schemaDefinition().get();
        List<OperationTypeDefinition> operationTypes = schemaDefinition.getOperationTypeDefinitions();
        // pre-flight checked via checker
        @SuppressWarnings({ "OptionalGetWithoutIsPresent", "ConstantConditions" }) OperationTypeDefinition queryOp = operationTypes.stream().filter(op -> "query".equals(op.getName())).findFirst().get();
        Optional<OperationTypeDefinition> mutationOp = operationTypes.stream().filter(op -> "mutation".equals(op.getName())).findFirst();
        Optional<OperationTypeDefinition> subscriptionOp = operationTypes.stream().filter(op -> "subscription".equals(op.getName())).findFirst();
        query = buildOperation(buildCtx, queryOp);
        schemaBuilder.query(query);
        if (mutationOp.isPresent()) {
            mutation = buildOperation(buildCtx, mutationOp.get());
            schemaBuilder.mutation(mutation);
        }
        if (subscriptionOp.isPresent()) {
            subscription = buildOperation(buildCtx, subscriptionOp.get());
            schemaBuilder.subscription(subscription);
        }
    }
    Set<GraphQLType> additionalTypes = buildAdditionalTypes(buildCtx);
    schemaBuilder.fieldVisibility(buildCtx.getWiring().getFieldVisibility());
    return schemaBuilder.build(additionalTypes);
}
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) TypeName(graphql.language.TypeName) SchemaDefinition(graphql.language.SchemaDefinition) GraphQLType(graphql.schema.GraphQLType) OperationTypeDefinition(graphql.language.OperationTypeDefinition) GraphQLSchema(graphql.schema.GraphQLSchema) 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) GraphQLObjectType(graphql.schema.GraphQLObjectType)

Example 5 with TypeName

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

the class VariablesAreInputTypes method checkVariableDefinition.

@Override
public void checkVariableDefinition(VariableDefinition variableDefinition) {
    TypeName unmodifiedAstType = getValidationUtil().getUnmodifiedType(variableDefinition.getType());
    GraphQLType type = getValidationContext().getSchema().getType(unmodifiedAstType.getName());
    if (type == null)
        return;
    if (!schemaUtil.isInputType(type)) {
        String message = "Wrong type for a variable";
        addError(ValidationErrorType.NonInputTypeOnVariable, variableDefinition.getSourceLocation(), message);
    }
}
Also used : TypeName(graphql.language.TypeName) GraphQLType(graphql.schema.GraphQLType)

Aggregations

TypeName (graphql.language.TypeName)13 InputObjectTypeDefinition (graphql.language.InputObjectTypeDefinition)6 ObjectTypeDefinition (graphql.language.ObjectTypeDefinition)5 UnionTypeDefinition (graphql.language.UnionTypeDefinition)5 EnumTypeDefinition (graphql.language.EnumTypeDefinition)4 InterfaceTypeDefinition (graphql.language.InterfaceTypeDefinition)4 OperationTypeDefinition (graphql.language.OperationTypeDefinition)4 ScalarTypeDefinition (graphql.language.ScalarTypeDefinition)4 TypeDefinition (graphql.language.TypeDefinition)4 List (java.util.List)4 Map (java.util.Map)4 Directive (graphql.language.Directive)3 FieldDefinition (graphql.language.FieldDefinition)3 InputValueDefinition (graphql.language.InputValueDefinition)3 GraphQLType (graphql.schema.GraphQLType)3 GraphQLError (graphql.GraphQLError)2 EnumValueDefinition (graphql.language.EnumValueDefinition)2 InputObjectTypeExtensionDefinition (graphql.language.InputObjectTypeExtensionDefinition)2 SchemaDefinition (graphql.language.SchemaDefinition)2 Type (graphql.language.Type)2