Search in sources :

Example 1 with GraphQLOutputType

use of graphql.schema.GraphQLOutputType in project graphql-java by graphql-java.

the class SchemaGenerator method buildField.

private GraphQLFieldDefinition buildField(BuildContext buildCtx, TypeDefinition parentType, FieldDefinition fieldDef) {
    GraphQLFieldDefinition.Builder builder = GraphQLFieldDefinition.newFieldDefinition();
    builder.definition(fieldDef);
    builder.name(fieldDef.getName());
    builder.description(schemaGeneratorHelper.buildDescription(fieldDef, fieldDef.getDescription()));
    builder.deprecate(schemaGeneratorHelper.buildDeprecationReason(fieldDef.getDirectives()));
    GraphQLDirective[] directives = buildDirectives(fieldDef.getDirectives(), Collections.emptyList(), Introspection.DirectiveLocation.FIELD);
    builder.withDirectives(directives);
    fieldDef.getInputValueDefinitions().forEach(inputValueDefinition -> builder.argument(buildArgument(buildCtx, inputValueDefinition)));
    GraphQLOutputType fieldType = buildOutputType(buildCtx, fieldDef.getType());
    builder.type(fieldType);
    builder.dataFetcherFactory(buildDataFetcherFactory(buildCtx, parentType, fieldDef, fieldType, Arrays.asList(directives)));
    return builder.build();
}
Also used : GraphQLOutputType(graphql.schema.GraphQLOutputType) GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) GraphQLDirective(graphql.schema.GraphQLDirective)

Example 2 with GraphQLOutputType

use of graphql.schema.GraphQLOutputType 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 3 with GraphQLOutputType

use of graphql.schema.GraphQLOutputType in project graphql-java by graphql-java.

the class OverlappingFieldsCanBeMerged method collectFieldsForField.

private void collectFieldsForField(Map<String, List<FieldAndType>> fieldMap, GraphQLType parentType, Field field) {
    String responseName = field.getAlias() != null ? field.getAlias() : field.getName();
    if (!fieldMap.containsKey(responseName)) {
        fieldMap.put(responseName, new ArrayList<>());
    }
    GraphQLOutputType fieldType = null;
    if (parentType instanceof GraphQLFieldsContainer) {
        GraphQLFieldsContainer fieldsContainer = (GraphQLFieldsContainer) parentType;
        GraphQLFieldDefinition fieldDefinition = getVisibleFieldDefinition(fieldsContainer, field);
        fieldType = fieldDefinition != null ? fieldDefinition.getType() : null;
    }
    fieldMap.get(responseName).add(new FieldAndType(field, fieldType, parentType));
}
Also used : GraphQLOutputType(graphql.schema.GraphQLOutputType) GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) GraphQLFieldsContainer(graphql.schema.GraphQLFieldsContainer)

Example 4 with GraphQLOutputType

use of graphql.schema.GraphQLOutputType in project graphql-java by graphql-java.

the class OverlappingFieldsCanBeMerged method collectFieldsForFragmentSpread.

private void collectFieldsForFragmentSpread(Map<String, List<FieldAndType>> fieldMap, Set<String> visitedFragmentSpreads, FragmentSpread fragmentSpread) {
    FragmentDefinition fragment = getValidationContext().getFragment(fragmentSpread.getName());
    if (fragment == null)
        return;
    if (visitedFragmentSpreads.contains(fragment.getName())) {
        return;
    }
    visitedFragmentSpreads.add(fragment.getName());
    GraphQLOutputType graphQLType = (GraphQLOutputType) TypeFromAST.getTypeFromAST(getValidationContext().getSchema(), fragment.getTypeCondition());
    collectFields(fieldMap, fragment.getSelectionSet(), graphQLType, visitedFragmentSpreads);
}
Also used : GraphQLOutputType(graphql.schema.GraphQLOutputType) FragmentDefinition(graphql.language.FragmentDefinition)

Example 5 with GraphQLOutputType

use of graphql.schema.GraphQLOutputType in project graphql-java by graphql-java.

the class PossibleFragmentSpreads method checkInlineFragment.

@Override
public void checkInlineFragment(InlineFragment inlineFragment) {
    GraphQLOutputType fragType = getValidationContext().getOutputType();
    GraphQLCompositeType parentType = getValidationContext().getParentType();
    if (fragType == null || parentType == null)
        return;
    if (!doTypesOverlap(fragType, parentType)) {
        String message = String.format("Fragment cannot be spread here as objects of " + "type %s can never be of type %s", parentType, fragType);
        addError(ValidationErrorType.InvalidFragmentType, inlineFragment.getSourceLocation(), message);
    }
}
Also used : GraphQLOutputType(graphql.schema.GraphQLOutputType) GraphQLCompositeType(graphql.schema.GraphQLCompositeType)

Aggregations

GraphQLOutputType (graphql.schema.GraphQLOutputType)14 GraphQLObjectType (graphql.schema.GraphQLObjectType)5 GraphQLFieldDefinition (graphql.schema.GraphQLFieldDefinition)4 GraphQLList (graphql.schema.GraphQLList)4 GraphQLInterfaceType (graphql.schema.GraphQLInterfaceType)3 GraphQLScalarType (graphql.schema.GraphQLScalarType)3 GraphQLUnionType (graphql.schema.GraphQLUnionType)3 ArrayList (java.util.ArrayList)3 Instrumentation (graphql.execution.instrumentation.Instrumentation)2 InstrumentationFieldFetchParameters (graphql.execution.instrumentation.parameters.InstrumentationFieldFetchParameters)2 Introspection (graphql.introspection.Introspection)2 Field (graphql.language.Field)2 TypeName (graphql.language.TypeName)2 DataFetcher (graphql.schema.DataFetcher)2 DataFetchingEnvironment (graphql.schema.DataFetchingEnvironment)2 DataFetchingEnvironmentBuilder.newDataFetchingEnvironment (graphql.schema.DataFetchingEnvironmentBuilder.newDataFetchingEnvironment)2 DataFetchingFieldSelectionSet (graphql.schema.DataFetchingFieldSelectionSet)2 GraphQLEnumType (graphql.schema.GraphQLEnumType)2 GraphQLSchema (graphql.schema.GraphQLSchema)2 GraphQLType (graphql.schema.GraphQLType)2