Search in sources :

Example 1 with ScalarTypeDefinition

use of graphql.language.ScalarTypeDefinition 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 2 with ScalarTypeDefinition

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

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

the class SchemaGenerator method buildInputType.

private GraphQLInputType buildInputType(BuildContext buildCtx, Type rawType) {
    TypeDefinition typeDefinition = buildCtx.getTypeDefinition(rawType);
    TypeInfo typeInfo = TypeInfo.typeInfo(rawType);
    GraphQLInputType inputType = buildCtx.hasInputType(typeDefinition);
    if (inputType != null) {
        return typeInfo.decorate(inputType);
    }
    if (buildCtx.stackContains(typeInfo)) {
        // we have circled around so put in a type reference and fix it later
        return typeInfo.decorate(typeRef(typeInfo.getName()));
    }
    buildCtx.push(typeInfo);
    if (typeDefinition instanceof InputObjectTypeDefinition) {
        inputType = buildInputObjectType(buildCtx, (InputObjectTypeDefinition) typeDefinition);
    } else if (typeDefinition instanceof EnumTypeDefinition) {
        inputType = buildEnumType(buildCtx, (EnumTypeDefinition) typeDefinition);
    } else if (typeDefinition instanceof ScalarTypeDefinition) {
        inputType = buildScalar(buildCtx, (ScalarTypeDefinition) typeDefinition);
    } else {
        // typeDefinition is not a valid InputType
        throw new NotAnInputTypeError(typeDefinition);
    }
    buildCtx.put(inputType);
    buildCtx.pop();
    return typeInfo.decorate(inputType);
}
Also used : GraphQLInputType(graphql.schema.GraphQLInputType) ScalarTypeDefinition(graphql.language.ScalarTypeDefinition) EnumTypeDefinition(graphql.language.EnumTypeDefinition) NotAnInputTypeError(graphql.schema.idl.errors.NotAnInputTypeError) InputObjectTypeDefinition(graphql.language.InputObjectTypeDefinition) 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 4 with ScalarTypeDefinition

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

the class GraphqlAntlrToLanguage method visitDirective.

@Override
public Void visitDirective(GraphqlParser.DirectiveContext ctx) {
    Directive directive = new Directive(ctx.name().getText());
    newNode(directive, ctx);
    for (ContextEntry contextEntry : contextStack) {
        if (contextEntry.contextProperty == ContextProperty.Field) {
            ((Field) contextEntry.value).getDirectives().add(directive);
            break;
        } else if (contextEntry.contextProperty == ContextProperty.FragmentDefinition) {
            ((FragmentDefinition) contextEntry.value).getDirectives().add(directive);
            break;
        } else if (contextEntry.contextProperty == ContextProperty.FragmentSpread) {
            ((FragmentSpread) contextEntry.value).getDirectives().add(directive);
            break;
        } else if (contextEntry.contextProperty == ContextProperty.InlineFragment) {
            ((InlineFragment) contextEntry.value).getDirectives().add(directive);
            break;
        } else if (contextEntry.contextProperty == ContextProperty.OperationDefinition) {
            ((OperationDefinition) contextEntry.value).getDirectives().add(directive);
            break;
        } else if (contextEntry.contextProperty == ContextProperty.EnumValueDefinition) {
            ((EnumValueDefinition) contextEntry.value).getDirectives().add(directive);
            break;
        } else if (contextEntry.contextProperty == ContextProperty.FieldDefinition) {
            ((FieldDefinition) contextEntry.value).getDirectives().add(directive);
            break;
        } else if (contextEntry.contextProperty == ContextProperty.InputValueDefinition) {
            ((InputValueDefinition) contextEntry.value).getDirectives().add(directive);
            break;
        } else if (contextEntry.contextProperty == ContextProperty.InterfaceTypeDefinition) {
            ((InterfaceTypeDefinition) contextEntry.value).getDirectives().add(directive);
            break;
        } else if (contextEntry.contextProperty == ContextProperty.EnumTypeDefinition) {
            ((EnumTypeDefinition) contextEntry.value).getDirectives().add(directive);
            break;
        } else if (contextEntry.contextProperty == ContextProperty.ObjectTypeDefinition) {
            ((ObjectTypeDefinition) contextEntry.value).getDirectives().add(directive);
            break;
        } else if (contextEntry.contextProperty == ContextProperty.ScalarTypeDefinition) {
            ((ScalarTypeDefinition) contextEntry.value).getDirectives().add(directive);
            break;
        } else if (contextEntry.contextProperty == ContextProperty.UnionTypeDefinition) {
            ((UnionTypeDefinition) contextEntry.value).getDirectives().add(directive);
            break;
        } else if (contextEntry.contextProperty == ContextProperty.InputObjectTypeDefinition) {
            ((InputObjectTypeDefinition) contextEntry.value).getDirectives().add(directive);
            break;
        } else if (contextEntry.contextProperty == ContextProperty.SchemaDefinition) {
            ((SchemaDefinition) contextEntry.value).getDirectives().add(directive);
            break;
        }
    }
    addContextProperty(ContextProperty.Directive, directive);
    super.visitDirective(ctx);
    popContext();
    return null;
}
Also used : ScalarTypeDefinition(graphql.language.ScalarTypeDefinition) FragmentDefinition(graphql.language.FragmentDefinition) EnumValueDefinition(graphql.language.EnumValueDefinition) EnumTypeDefinition(graphql.language.EnumTypeDefinition) InputObjectTypeDefinition(graphql.language.InputObjectTypeDefinition) InputValueDefinition(graphql.language.InputValueDefinition) Directive(graphql.language.Directive) InlineFragment(graphql.language.InlineFragment)

Example 5 with ScalarTypeDefinition

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

the class GraphqlAntlrToLanguage method visitScalarTypeDefinition.

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

Aggregations

ScalarTypeDefinition (graphql.language.ScalarTypeDefinition)6 EnumTypeDefinition (graphql.language.EnumTypeDefinition)4 InputObjectTypeDefinition (graphql.language.InputObjectTypeDefinition)4 InterfaceTypeDefinition (graphql.language.InterfaceTypeDefinition)4 ObjectTypeDefinition (graphql.language.ObjectTypeDefinition)4 TypeDefinition (graphql.language.TypeDefinition)4 UnionTypeDefinition (graphql.language.UnionTypeDefinition)4 OperationTypeDefinition (graphql.language.OperationTypeDefinition)3 GraphQLError (graphql.GraphQLError)1 INPUT_OBJECT (graphql.introspection.Introspection.DirectiveLocation.INPUT_OBJECT)1 OBJECT (graphql.introspection.Introspection.DirectiveLocation.OBJECT)1 Directive (graphql.language.Directive)1 EnumTypeExtensionDefinition (graphql.language.EnumTypeExtensionDefinition)1 EnumValueDefinition (graphql.language.EnumValueDefinition)1 FragmentDefinition (graphql.language.FragmentDefinition)1 InlineFragment (graphql.language.InlineFragment)1 InputObjectTypeExtensionDefinition (graphql.language.InputObjectTypeExtensionDefinition)1 InputValueDefinition (graphql.language.InputValueDefinition)1 InterfaceTypeExtensionDefinition (graphql.language.InterfaceTypeExtensionDefinition)1 ObjectTypeExtensionDefinition (graphql.language.ObjectTypeExtensionDefinition)1