use of graphql.language.SchemaDefinition in project graphql-java by graphql-java.
the class GraphqlAntlrToLanguage method visitSchemaDefinition.
@Override
public Void visitSchemaDefinition(GraphqlParser.SchemaDefinitionContext ctx) {
SchemaDefinition def = new SchemaDefinition();
newNode(def, ctx);
result.getDefinitions().add(def);
addContextProperty(ContextProperty.SchemaDefinition, def);
super.visitChildren(ctx);
popContext();
return null;
}
use of graphql.language.SchemaDefinition in project graphql-java by graphql-java.
the class IntrospectionResultToSchema method createSchemaDefinition.
/**
* Returns a IDL Document that reprSesents 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<String, Object> introspectionResult) {
assertTrue(introspectionResult.get("__schema") != null, "__schema expected");
Map<String, Object> schema = (Map<String, Object>) introspectionResult.get("__schema");
Map<String, Object> queryType = (Map<String, Object>) 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<String, Object> mutationType = (Map<String, Object>) 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<String, Object> subscriptionType = (Map<String, Object>) 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<Map<String, Object>> types = (List<Map<String, Object>>) schema.get("types");
for (Map<String, Object> type : types) {
TypeDefinition typeDefinition = createTypeDefinition(type);
if (typeDefinition == null)
continue;
document.getDefinitions().add(typeDefinition);
}
return document;
}
use of graphql.language.SchemaDefinition in project graphql-java by graphql-java.
the class SchemaTypeChecker method checkSchemaInvariants.
private void checkSchemaInvariants(List<GraphQLError> errors, TypeDefinitionRegistry typeRegistry) {
// schema
if (!typeRegistry.schemaDefinition().isPresent()) {
if (!typeRegistry.getType("Query").isPresent()) {
errors.add(new SchemaMissingError());
}
} else {
SchemaDefinition schemaDefinition = typeRegistry.schemaDefinition().get();
List<OperationTypeDefinition> operationTypeDefinitions = schemaDefinition.getOperationTypeDefinitions();
operationTypeDefinitions.forEach(checkOperationTypesExist(typeRegistry, errors));
operationTypeDefinitions.forEach(checkOperationTypesAreObjects(typeRegistry, errors));
// ensure we have a "query" one
Optional<OperationTypeDefinition> query = operationTypeDefinitions.stream().filter(op -> "query".equals(op.getName())).findFirst();
if (!query.isPresent()) {
errors.add(new QueryOperationMissingError());
}
}
}
use of graphql.language.SchemaDefinition 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);
}
use of graphql.language.SchemaDefinition in project graphql-java by graphql-java.
the class SchemaDiff method diffSchemaImpl.
private void diffSchemaImpl(DiffSet diffSet, DifferenceReporter reporter) {
Map<String, Object> oldApi = diffSet.getOld();
Map<String, Object> newApi = diffSet.getNew();
Document oldDoc = new IntrospectionResultToSchema().createSchemaDefinition(oldApi);
Document newDoc = new IntrospectionResultToSchema().createSchemaDefinition(newApi);
DiffCtx ctx = new DiffCtx(reporter, oldDoc, newDoc);
Optional<SchemaDefinition> oldSchemaDef = getSchemaDef(oldDoc);
Optional<SchemaDefinition> newSchemaDef = getSchemaDef(newDoc);
// check query operation
checkOperation(ctx, "query", oldSchemaDef, newSchemaDef);
checkOperation(ctx, "mutation", oldSchemaDef, newSchemaDef);
checkOperation(ctx, "subscription", oldSchemaDef, newSchemaDef);
reporter.onEnd();
}
Aggregations