graphql.introspection.IntrospectionResultToSchema Maven / Gradle / Ivy
package graphql.introspection;
import graphql.PublicApi;
import graphql.language.Argument;
import graphql.language.Comment;
import graphql.language.Directive;
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.NonNullType;
import graphql.language.ObjectTypeDefinition;
import graphql.language.OperationTypeDefinition;
import graphql.language.SchemaDefinition;
import graphql.language.SourceLocation;
import graphql.language.StringValue;
import graphql.language.Type;
import graphql.language.TypeDefinition;
import graphql.language.TypeName;
import graphql.language.UnionTypeDefinition;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import static graphql.Assert.assertNotNull;
import static graphql.Assert.assertShouldNeverHappen;
import static graphql.Assert.assertTrue;
@SuppressWarnings("unchecked")
@PublicApi
public class IntrospectionResultToSchema {
@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 = new TypeName((String) queryType.get("name"));
boolean nonDefaultQueryName = !"Query".equals(query.getName());
SchemaDefinition schemaDefinition = new SchemaDefinition();
schemaDefinition.getOperationTypeDefinitions().add(new OperationTypeDefinition("query", query));
Map mutationType = (Map) schema.get("mutationType");
boolean nonDefaultMutationName = false;
if (mutationType != null) {
TypeName mutation = new TypeName((String) mutationType.get("name"));
nonDefaultMutationName = !"Mutation".equals(mutation.getName());
schemaDefinition.getOperationTypeDefinitions().add(new OperationTypeDefinition("mutation", mutation));
}
Map subscriptionType = (Map) schema.get("subscriptionType");
boolean nonDefaultSubscriptionName = false;
if (subscriptionType != null) {
TypeName subscription = new TypeName((String) subscriptionType.get("name"));
nonDefaultSubscriptionName = !"Subscription".equals(subscription.getName());
schemaDefinition.getOperationTypeDefinitions().add(new OperationTypeDefinition("subscription", subscription));
}
Document document = new Document();
if (nonDefaultQueryName || nonDefaultMutationName || nonDefaultSubscriptionName) {
document.getDefinitions().add(schemaDefinition);
}
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy