use of com.graphql_java_generator.plugin.language.Directive in project graphql-maven-plugin-project by graphql-java-generator.
the class DocumentParser method parseOneDocument.
/**
* Generates the target classes for the given GraphQL schema definition
*
* @param document
*/
public void parseOneDocument(Document document) {
// List of all the names of the query types. There should be only one. But we're ready for more (for instance if
// several schema files have been merged)
List<String> queryObjectNames = new ArrayList<>();
// List of all the names of the mutation types. There should be only one. But we're ready for more (for instance
// if several schema files have been merged)
List<String> mutationObjectNames = new ArrayList<>();
// List of all the names of the subscription types. There should be only one. But we're ready for more (for
// instance if several schema files have been merged)
List<String> subscriptionObjectNames = new ArrayList<>();
// The Directives must be read first, as they may be found on almost any kind of definition in the GraphQL
// schema
document.getDefinitions().stream().filter(n -> (n instanceof DirectiveDefinition)).forEach(node -> directives.add(readDirectiveDefinition((DirectiveDefinition) node)));
// Looks for a schema definitions, to list the defined queries, mutations and subscriptions (should be only one
// of each), but we're ready for more. (for instance if several schema files have been merged)
logger.debug("Looking for schema definition");
for (Definition<?> node : document.getDefinitions()) {
if (node instanceof SchemaDefinition) {
readSchemaDefinition((SchemaDefinition) node, queryObjectNames, mutationObjectNames, subscriptionObjectNames);
}
// if
}
// for
logger.debug("Reading node definitions");
for (Definition<?> node : document.getDefinitions()) {
// directive
if (node instanceof DirectiveDefinition) {
// Directives are read latter
} else // enum
if (node instanceof EnumTypeDefinition) {
enumTypes.add(readEnumType((EnumTypeDefinition) node));
} else // input object
if (node instanceof InputObjectTypeDefinition) {
objectTypes.add(readInputObjectType((InputObjectTypeDefinition) node));
} else // interface
if (node instanceof InterfaceTypeDefinition) {
interfaceTypes.add(readInterfaceType((InterfaceTypeDefinition) node));
} else // extend object
if (node instanceof ObjectTypeExtensionDefinition) {
// ObjectTypeExtensionDefinition is a subclass of ObjectTypeDefinition, so we need to check it first.
//
// No action here: we'll manage all the object extensions once all object definitions have been read
objectTypeExtensionDefinitions.add((ObjectTypeExtensionDefinition) node);
} else // object
if (node instanceof ObjectTypeDefinition) {
// Let's check what kind of ObjectDefinition we have
String name = ((ObjectTypeDefinition) node).getName();
if (queryObjectNames.contains(name) || DEFAULT_QUERY_NAME.equals(name)) {
// We first read the object type, that'll go to the main package
ObjectType o = readObjectTypeDefinition((ObjectTypeDefinition) node);
o.setRequestType("query");
objectTypes.add(o);
// Then we read the query, that'll go in the util subpackage: its imports are different
if (queryType != null) {
throw new RuntimeException("Error while reading the query '" + ((ObjectTypeDefinition) node).getName() + "'. A Query root operation has already been read, with name'" + queryType.getName() + "'");
}
queryType = o;
} else if (mutationObjectNames.contains(name) || DEFAULT_MUTATION_NAME.equals(name)) {
// We first read the object type, that'll go to the main package
ObjectType o = readObjectTypeDefinition((ObjectTypeDefinition) node);
o.setRequestType("mutation");
objectTypes.add(o);
// Then we read the mutation, that'll go in the util subpackage: its imports are different
if (mutationType != null) {
throw new RuntimeException("Error while reading the mutation '" + ((ObjectTypeDefinition) node).getName() + "'. A Mutation root operation has already been read, with name'" + mutationType.getName() + "'");
}
mutationType = o;
} else if (subscriptionObjectNames.contains(name) || DEFAULT_SUBSCRIPTION_NAME.equals(name)) {
// We first read the object type, that'll go to the main package
ObjectType o = readObjectTypeDefinition((ObjectTypeDefinition) node);
o.setRequestType("subscription");
objectTypes.add(o);
// Then we read the subscription, that'll go in the util subpackage: its imports are different
if (subscriptionType != null) {
throw new RuntimeException("Error while reading the subscription '" + ((ObjectTypeDefinition) node).getName() + "'. A Subscription root operation has already been read, with name'" + subscriptionType.getName() + "'");
}
subscriptionType = o;
} else {
objectTypes.add(readObjectTypeDefinition((ObjectTypeDefinition) node));
}
} else // scalar extension
if (node instanceof ScalarTypeExtensionDefinition) {
readScalarExtensionType((ScalarTypeExtensionDefinition) node);
} else // scalar
if (node instanceof ScalarTypeDefinition) {
// Custom scalars implementation must be provided by the configuration. We just check that it's OK.
readCustomScalarType((ScalarTypeDefinition) node);
} else // schema
if (node instanceof SchemaDefinition) {
// No action, we already parsed it
} else // union
if (node instanceof UnionTypeDefinition) {
// Unions are read latter, once all GraphQL types have been parsed
} else {
logger.warn("Non managed node type: " + node.getClass().getName());
}
}
// for
// Once all Types have been properly read, we can read the union types
logger.debug("Reading union definitions");
document.getDefinitions().stream().filter(n -> (n instanceof UnionTypeDefinition)).forEach(n -> unionTypes.add(readUnionType((UnionTypeDefinition) n)));
}
use of com.graphql_java_generator.plugin.language.Directive in project graphql-maven-plugin-project by graphql-java-generator.
the class AddRelayConnectionsTest method checkRelayConnectionDirective.
private void checkRelayConnectionDirective() {
final String RELAY_CONNECTION = "RelayConnection";
boolean found = false;
for (Directive d : documentParser.getDirectives()) {
if (RELAY_CONNECTION.equals(d.getName())) {
// The directive should exist only once, so we may not already have found it.
if (found) {
fail("There are two directives with '" + RELAY_CONNECTION + "' as a name");
}
// We've found it.
found = true;
// Let's check its properties
assertEquals(0, d.getArguments().size(), "No arguments");
assertEquals(1, d.getDirectiveLocations().size(), "Directive has one location");
assertEquals(DirectiveLocation.FIELD_DEFINITION, d.getDirectiveLocations().get(0), "Directive locations may only be: FIELD_DEFINITION");
}
}
if (!found) {
fail("The directive " + RELAY_CONNECTION + " has not been found");
}
}
use of com.graphql_java_generator.plugin.language.Directive in project graphql-maven-plugin-project by graphql-java-generator.
the class AddRelayConnectionsTest method testAddRelayConnections_schemaWithWrongRelayConnectionDirective.
@Test
@Execution(ExecutionMode.CONCURRENT)
void testAddRelayConnections_schemaWithWrongRelayConnectionDirective() throws IOException {
// Preparation
loadSpringContext(AllGraphQLCases_Client_SpringConfiguration.class, "testAddRelayConnections_schemaWithWrongRelayConnectionDirective", true);
//
Directive relayConnection = documentParser.getDirectiveDefinition("RelayConnection");
// Let's remove the only item in the list
relayConnection.getDirectiveLocations().remove(0);
// Wrong location!
relayConnection.getDirectiveLocations().add(DirectiveLocation.ENUM);
documentParser.getDirectives().add(relayConnection);
// Verification
AssertionFailedError e = assertThrows(AssertionFailedError.class, () -> checkRelayConnectionDirective());
assertTrue(e.getMessage().contains("ENUM"));
}
Aggregations