graphql.introspection.IntrospectionResultToSchema Maven / Gradle / Ivy
package graphql.introspection;
import graphql.ExecutionResult;
import graphql.PublicApi;
import graphql.language.Argument;
import graphql.language.Description;
import graphql.language.Directive;
import graphql.language.DirectiveDefinition;
import graphql.language.DirectiveLocation;
import graphql.language.Document;
import graphql.language.EnumTypeDefinition;
import graphql.language.EnumValueDefinition;
import graphql.language.FieldDefinition;
import graphql.language.InputObjectTypeDefinition;
import graphql.language.InputValueDefinition;
import graphql.language.InterfaceTypeDefinition;
import graphql.language.ListType;
import graphql.language.NodeDirectivesBuilder;
import graphql.language.NonNullType;
import graphql.language.ObjectTypeDefinition;
import graphql.language.OperationTypeDefinition;
import graphql.language.ScalarTypeDefinition;
import graphql.language.SchemaDefinition;
import graphql.language.StringValue;
import graphql.language.Type;
import graphql.language.TypeDefinition;
import graphql.language.TypeName;
import graphql.language.UnionTypeDefinition;
import graphql.language.Value;
import graphql.parser.Parser;
import graphql.schema.idl.ScalarInfo;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import static graphql.Assert.assertNotEmpty;
import static graphql.Assert.assertNotNull;
import static graphql.Assert.assertShouldNeverHappen;
import static graphql.Assert.assertTrue;
import static graphql.collect.ImmutableKit.map;
import static graphql.schema.idl.DirectiveInfo.isGraphqlSpecifiedDirective;
@SuppressWarnings("unchecked")
@PublicApi
public class IntrospectionResultToSchema {
/**
* Returns a IDL Document that represents the schema as defined by the introspection execution result
*
* @param introspectionResult the result of an introspection query on a schema
*
* @return a IDL Document of the schema
*/
public Document createSchemaDefinition(ExecutionResult introspectionResult) {
if (!introspectionResult.isDataPresent()) {
return null;
}
Map introspectionResultMap = introspectionResult.getData();
return createSchemaDefinition(introspectionResultMap);
}
/**
* Returns a IDL Document that represents 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 introspectionResult) {
assertTrue(introspectionResult.get("__schema") != null, () -> "__schema expected");
Map schema = (Map) introspectionResult.get("__schema");
Map queryType = (Map) schema.get("queryType");
assertNotNull(queryType, () -> "queryType expected");
TypeName query = TypeName.newTypeName().name((String) queryType.get("name")).build();
boolean nonDefaultQueryName = !"Query".equals(query.getName());
SchemaDefinition.Builder schemaDefinition = SchemaDefinition.newSchemaDefinition();
schemaDefinition.description(toDescription(schema));
schemaDefinition.operationTypeDefinition(OperationTypeDefinition.newOperationTypeDefinition().name("query").typeName(query).build());
Map mutationType = (Map) schema.get("mutationType");
boolean nonDefaultMutationName = false;
if (mutationType != null) {
TypeName mutation = TypeName.newTypeName().name((String) mutationType.get("name")).build();
nonDefaultMutationName = !"Mutation".equals(mutation.getName());
schemaDefinition.operationTypeDefinition(OperationTypeDefinition.newOperationTypeDefinition().name("mutation").typeName(mutation).build());
}
Map subscriptionType = (Map) schema.get("subscriptionType");
boolean nonDefaultSubscriptionName = false;
if (subscriptionType != null) {
TypeName subscription = TypeName.newTypeName().name(((String) subscriptionType.get("name"))).build();
nonDefaultSubscriptionName = !"Subscription".equals(subscription.getName());
schemaDefinition.operationTypeDefinition(OperationTypeDefinition.newOperationTypeDefinition().name("subscription").typeName(subscription).build());
}
Document.Builder document = Document.newDocument();
if (nonDefaultQueryName || nonDefaultMutationName || nonDefaultSubscriptionName) {
document.definition(schemaDefinition.build());
}
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy