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();
}
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);
}
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));
}
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);
}
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);
}
}
Aggregations