use of graphql.schema.GraphQLType in project graphql-java by graphql-java.
the class AstValueHelper method handleList.
private static Value handleList(Object _value, GraphQLList type) {
GraphQLType itemType = type.getWrappedType();
if (_value instanceof Iterable) {
Iterable iterable = (Iterable) _value;
List<Value> valuesNodes = new ArrayList<>();
for (Object item : iterable) {
Value itemNode = astFromValue(item, itemType);
if (itemNode != null) {
valuesNodes.add(itemNode);
}
}
return new ArrayValue(valuesNodes);
}
return astFromValue(_value, itemType);
}
use of graphql.schema.GraphQLType in project graphql-java by graphql-java.
the class SchemaPrinter method print.
/**
* This can print an in memory GraphQL schema back to a logical schema definition
*
* @param schema the schema in play
*
* @return the logical schema definition
*/
public String print(GraphQLSchema schema) {
StringWriter sw = new StringWriter();
PrintWriter out = new PrintWriter(sw);
GraphqlFieldVisibility visibility = schema.getFieldVisibility();
printer(schema.getClass()).print(out, schema, visibility);
List<GraphQLType> typesAsList = schema.getAllTypesAsList().stream().sorted(Comparator.comparing(GraphQLType::getName)).collect(toList());
printType(out, typesAsList, GraphQLInterfaceType.class, visibility);
printType(out, typesAsList, GraphQLUnionType.class, visibility);
printType(out, typesAsList, GraphQLObjectType.class, visibility);
printType(out, typesAsList, GraphQLEnumType.class, visibility);
printType(out, typesAsList, GraphQLScalarType.class, visibility);
printType(out, typesAsList, GraphQLInputObjectType.class, visibility);
String result = sw.toString();
if (result.endsWith("\n\n")) {
result = result.substring(0, result.length() - 1);
}
return result;
}
use of graphql.schema.GraphQLType in project graphql-java by graphql-java.
the class SchemaPrinter method typeString.
String typeString(GraphQLType rawType) {
StringBuilder sb = new StringBuilder();
Stack<String> stack = new Stack<>();
GraphQLType type = rawType;
while (true) {
if (type instanceof GraphQLNonNull) {
type = ((GraphQLNonNull) type).getWrappedType();
stack.push("!");
} else if (type instanceof GraphQLList) {
type = ((GraphQLList) type).getWrappedType();
sb.append("[");
stack.push("]");
} else {
sb.append(type.getName());
break;
}
}
while (!stack.isEmpty()) {
sb.append(stack.pop());
}
return sb.toString();
}
use of graphql.schema.GraphQLType in project graphql-java by graphql-java.
the class TypeInfo method decorate.
/**
* This will decorate a graphql type with the original hierarchy of non null and list'ness
* it originally contained in its definition type
*
* @param objectType this should be a graphql type that was originally built from this raw type
* @param <T> the type
*
* @return the decorated type
*/
public <T extends GraphQLType> T decorate(GraphQLType objectType) {
GraphQLType out = objectType;
Stack<Class<?>> wrappingStack = new Stack<>();
wrappingStack.addAll(this.decoration);
while (!wrappingStack.isEmpty()) {
Class<?> clazz = wrappingStack.pop();
if (clazz.equals(NonNullType.class)) {
out = new GraphQLNonNull(out);
}
if (clazz.equals(ListType.class)) {
out = new GraphQLList(out);
}
}
// noinspection unchecked
return (T) out;
}
use of graphql.schema.GraphQLType 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);
}
Aggregations