use of io.smallrye.graphql.schema.model.Operation in project smallrye-graphql by smallrye.
the class OperationCreator method createOperation.
/**
* This creates a single operation.
* It translate to one entry under a query / mutation in the schema or
* one method in the Java class annotated with Query or Mutation
*
* @param methodInfo the java method
* @param operationType the type of operation (Query / Mutation)
* @param type
* @return a Operation that defines this GraphQL Operation
*/
public Operation createOperation(MethodInfo methodInfo, OperationType operationType, final io.smallrye.graphql.schema.model.Type type) {
if (!Modifier.isPublic(methodInfo.flags())) {
throw new IllegalArgumentException("Method " + methodInfo.declaringClass().name().toString() + "#" + methodInfo.name() + " is used as an operation, but is not public");
}
Annotations annotationsForMethod = Annotations.getAnnotationsForMethod(methodInfo);
Annotations annotationsForClass = Annotations.getAnnotationsForClass(methodInfo.declaringClass());
Type fieldType = getReturnType(methodInfo);
// Name
String name = getOperationName(methodInfo, operationType, annotationsForMethod);
// Field Type
validateFieldType(methodInfo, operationType);
Reference reference = referenceCreator.createReferenceForOperationField(fieldType, annotationsForMethod);
// Execution
Execute execute = getExecution(annotationsForMethod, annotationsForClass, reference);
Operation operation = new Operation(methodInfo.declaringClass().name().toString(), methodInfo.name(), MethodHelper.getPropertyName(Direction.OUT, methodInfo.name()), name, reference, operationType, execute);
if (type != null) {
operation.setSourceFieldOn(new Reference(type));
}
// Arguments
List<Type> parameters = methodInfo.parameters();
for (short i = 0; i < parameters.size(); i++) {
Optional<Argument> maybeArgument = argumentCreator.createArgument(operation, methodInfo, i);
maybeArgument.ifPresent(operation::addArgument);
}
populateField(Direction.OUT, operation, fieldType, annotationsForMethod);
return operation;
}
use of io.smallrye.graphql.schema.model.Operation in project smallrye-graphql by smallrye.
the class Bootstrap method createNamedType.
private GraphQLObjectType createNamedType(String parent, Group group, Set<Operation> operations) {
String namedTypeName = group.getName() + parent;
GraphQLObjectType.Builder objectTypeBuilder = GraphQLObjectType.newObject().name(namedTypeName).description(group.getDescription());
// Operations
for (Operation operation : operations) {
operation = eventEmitter.fireCreateOperation(operation);
GraphQLFieldDefinition graphQLFieldDefinition = createGraphQLFieldDefinitionFromOperation(namedTypeName, operation);
objectTypeBuilder = objectTypeBuilder.field(graphQLFieldDefinition);
}
return objectTypeBuilder.build();
}
use of io.smallrye.graphql.schema.model.Operation in project smallrye-graphql by smallrye.
the class Bootstrap method addRootObject.
private void addRootObject(GraphQLObjectType.Builder rootBuilder, Set<Operation> operations, String rootName) {
for (Operation operation : operations) {
operation = eventEmitter.fireCreateOperation(operation);
GraphQLFieldDefinition graphQLFieldDefinition = createGraphQLFieldDefinitionFromOperation(rootName, operation);
rootBuilder.field(graphQLFieldDefinition);
}
}
use of io.smallrye.graphql.schema.model.Operation in project smallrye-graphql by smallrye.
the class Bootstrap method addGroupedRootObject.
private void addGroupedRootObject(GraphQLObjectType.Builder rootBuilder, Map<Group, Set<Operation>> operationMap, String rootName) {
Set<Map.Entry<Group, Set<Operation>>> operationsSet = operationMap.entrySet();
for (Map.Entry<Group, Set<Operation>> operationsEntry : operationsSet) {
Group group = operationsEntry.getKey();
Set<Operation> operations = operationsEntry.getValue();
GraphQLObjectType namedType = createNamedType(rootName, group, operations);
GraphQLFieldDefinition.Builder graphQLFieldDefinitionBuilder = GraphQLFieldDefinition.newFieldDefinition().name(group.getName()).description(group.getDescription());
graphQLFieldDefinitionBuilder.type(namedType);
DataFetcher<?> dummyDataFetcher = dfe -> namedType.getName();
GraphQLFieldDefinition namedField = graphQLFieldDefinitionBuilder.build();
this.codeRegistryBuilder.dataFetcherIfAbsent(FieldCoordinates.coordinates(rootName, namedField.getName()), dummyDataFetcher);
rootBuilder.field(namedField);
}
}
use of io.smallrye.graphql.schema.model.Operation in project smallrye-graphql by smallrye.
the class Bootstrap method createGraphQLObjectType.
private void createGraphQLObjectType(Type type) {
GraphQLObjectType.Builder objectTypeBuilder = GraphQLObjectType.newObject().name(type.getName()).description(type.getDescription());
// Directives
if (type.hasDirectiveInstances()) {
for (DirectiveInstance directiveInstance : type.getDirectiveInstances()) {
objectTypeBuilder.withDirective(createGraphQLDirectiveFrom(directiveInstance));
}
}
// Fields
if (type.hasFields()) {
objectTypeBuilder = objectTypeBuilder.fields(createGraphQLFieldDefinitionsFromFields(type, type.getFields().values()));
}
// Operations
if (type.hasOperations()) {
for (Operation operation : type.getOperations().values()) {
String name = operation.getName();
if (!type.hasBatchOperation(name)) {
operation = eventEmitter.fireCreateOperation(operation);
GraphQLFieldDefinition graphQLFieldDefinition = createGraphQLFieldDefinitionFromOperation(type.getName(), operation);
objectTypeBuilder = objectTypeBuilder.field(graphQLFieldDefinition);
} else {
log.duplicateOperation(operation.getName());
}
}
}
// Batch Operations
if (type.hasBatchOperations()) {
for (Operation operation : type.getBatchOperations().values()) {
operation = eventEmitter.fireCreateOperation(operation);
GraphQLFieldDefinition graphQLFieldDefinition = createGraphQLFieldDefinitionFromBatchOperation(type.getName(), operation);
objectTypeBuilder = objectTypeBuilder.field(graphQLFieldDefinition);
}
}
// Interfaces
if (type.hasInterfaces()) {
Set<Reference> interfaces = type.getInterfaces();
for (Reference i : interfaces) {
if (interfaceMap.containsKey(i.getName())) {
GraphQLInterfaceType graphQLInterfaceType = interfaceMap.get(i.getName());
objectTypeBuilder = objectTypeBuilder.withInterface(graphQLInterfaceType);
}
}
}
GraphQLObjectType graphQLObjectType = objectTypeBuilder.build();
typeMap.put(type.getName(), graphQLObjectType);
// Register this output for interface type resolving
InterfaceOutputRegistry.register(type, graphQLObjectType);
}
Aggregations