Search in sources :

Example 1 with Operation

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;
}
Also used : Type(org.jboss.jandex.Type) OperationType(io.smallrye.graphql.schema.model.OperationType) Annotations(io.smallrye.graphql.schema.Annotations) Execute(io.smallrye.graphql.schema.model.Execute) Argument(io.smallrye.graphql.schema.model.Argument) Reference(io.smallrye.graphql.schema.model.Reference) Operation(io.smallrye.graphql.schema.model.Operation)

Example 2 with 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();
}
Also used : GraphQLObjectType(graphql.schema.GraphQLObjectType) GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) Operation(io.smallrye.graphql.schema.model.Operation)

Example 3 with Operation

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);
    }
}
Also used : GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) Operation(io.smallrye.graphql.schema.model.Operation)

Example 4 with Operation

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);
    }
}
Also used : JsonReaderFactory(javax.json.JsonReaderFactory) Arrays(java.util.Arrays) GraphQLInputObjectType(graphql.schema.GraphQLInputObjectType) InterfaceResolver(io.smallrye.graphql.execution.resolver.InterfaceResolver) GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) Wrapper(io.smallrye.graphql.schema.model.Wrapper) GraphQLInterfaceType(graphql.schema.GraphQLInterfaceType) GraphQLInputObjectField(graphql.schema.GraphQLInputObjectField) GraphQLList.list(graphql.schema.GraphQLList.list) BigDecimal(java.math.BigDecimal) EnumType(io.smallrye.graphql.schema.model.EnumType) DirectiveArgument(io.smallrye.graphql.schema.model.DirectiveArgument) DirectiveInstance(io.smallrye.graphql.schema.model.DirectiveInstance) ErrorInfoMap(io.smallrye.graphql.execution.error.ErrorInfoMap) Map(java.util.Map) FieldDataFetcher(io.smallrye.graphql.execution.datafetcher.FieldDataFetcher) Schema(io.smallrye.graphql.schema.model.Schema) GraphQLObjectType(graphql.schema.GraphQLObjectType) GraphQLDirective(graphql.schema.GraphQLDirective) GraphQLScalarTypes(io.smallrye.graphql.scalar.GraphQLScalarTypes) CollectionCreator(io.smallrye.graphql.execution.datafetcher.CollectionCreator) Collection(java.util.Collection) GraphQLNonNull(graphql.schema.GraphQLNonNull) GraphqlFieldVisibility(graphql.schema.visibility.GraphqlFieldVisibility) Set(java.util.Set) GraphQLInputType(graphql.schema.GraphQLInputType) SmallRyeContext(io.smallrye.graphql.execution.context.SmallRyeContext) GraphQLArgument(graphql.schema.GraphQLArgument) JsonBCreator(io.smallrye.graphql.json.JsonBCreator) Config(io.smallrye.graphql.spi.config.Config) List(java.util.List) Stream(java.util.stream.Stream) Type(io.smallrye.graphql.schema.model.Type) Entry(java.util.Map.Entry) Optional(java.util.Optional) SmallRyeGraphQLServerLogging.log(io.smallrye.graphql.SmallRyeGraphQLServerLogging.log) GraphQLEnumType(graphql.schema.GraphQLEnumType) FieldCoordinates(graphql.schema.FieldCoordinates) GraphQLCodeRegistry(graphql.schema.GraphQLCodeRegistry) JsonInputRegistry(io.smallrye.graphql.json.JsonInputRegistry) DirectiveLocation(graphql.introspection.Introspection.DirectiveLocation) SmallRyeGraphQLServerMessages(io.smallrye.graphql.SmallRyeGraphQLServerMessages) GraphQLScalarType(graphql.schema.GraphQLScalarType) HashMap(java.util.HashMap) EventEmitter(io.smallrye.graphql.execution.event.EventEmitter) LookupService(io.smallrye.graphql.spi.LookupService) Argument(io.smallrye.graphql.schema.model.Argument) ArrayList(java.util.ArrayList) ClassloadingService(io.smallrye.graphql.spi.ClassloadingService) HashSet(java.util.HashSet) Group(io.smallrye.graphql.schema.model.Group) Reference(io.smallrye.graphql.schema.model.Reference) BatchDataFetcher(io.smallrye.graphql.execution.datafetcher.BatchDataFetcher) EnumValue(io.smallrye.graphql.schema.model.EnumValue) Json(javax.json.Json) DataFetcher(graphql.schema.DataFetcher) GraphQLSchema(graphql.schema.GraphQLSchema) BlockedFields(graphql.schema.visibility.BlockedFields) LinkedHashSet(java.util.LinkedHashSet) JsonReader(javax.json.JsonReader) DirectiveType(io.smallrye.graphql.schema.model.DirectiveType) DEFAULT_FIELD_VISIBILITY(graphql.schema.visibility.DefaultGraphqlFieldVisibility.DEFAULT_FIELD_VISIBILITY) InputType(io.smallrye.graphql.schema.model.InputType) GraphQLOutputType(graphql.schema.GraphQLOutputType) NO_INTROSPECTION_FIELD_VISIBILITY(graphql.schema.visibility.NoIntrospectionGraphqlFieldVisibility.NO_INTROSPECTION_FIELD_VISIBILITY) InterfaceOutputRegistry(io.smallrye.graphql.execution.resolver.InterfaceOutputRegistry) StringReader(java.io.StringReader) GraphQLTypeReference(graphql.schema.GraphQLTypeReference) Operation(io.smallrye.graphql.schema.model.Operation) Jsonb(javax.json.bind.Jsonb) Classes(io.smallrye.graphql.execution.Classes) Field(io.smallrye.graphql.schema.model.Field) ReferenceType(io.smallrye.graphql.schema.model.ReferenceType) Group(io.smallrye.graphql.schema.model.Group) Set(java.util.Set) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) Operation(io.smallrye.graphql.schema.model.Operation) Entry(java.util.Map.Entry) GraphQLObjectType(graphql.schema.GraphQLObjectType) ErrorInfoMap(io.smallrye.graphql.execution.error.ErrorInfoMap) Map(java.util.Map) HashMap(java.util.HashMap)

Example 5 with Operation

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);
}
Also used : Reference(io.smallrye.graphql.schema.model.Reference) GraphQLTypeReference(graphql.schema.GraphQLTypeReference) GraphQLObjectType(graphql.schema.GraphQLObjectType) GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) Operation(io.smallrye.graphql.schema.model.Operation) GraphQLInterfaceType(graphql.schema.GraphQLInterfaceType) DirectiveInstance(io.smallrye.graphql.schema.model.DirectiveInstance)

Aggregations

Operation (io.smallrye.graphql.schema.model.Operation)13 GraphQLFieldDefinition (graphql.schema.GraphQLFieldDefinition)5 Reference (io.smallrye.graphql.schema.model.Reference)5 SmallRyeGraphQLServerMessages (io.smallrye.graphql.SmallRyeGraphQLServerMessages)4 SmallRyeContext (io.smallrye.graphql.execution.context.SmallRyeContext)4 DataFetcherResult (graphql.execution.DataFetcherResult)3 DataFetchingEnvironment (graphql.schema.DataFetchingEnvironment)3 GraphQLInterfaceType (graphql.schema.GraphQLInterfaceType)3 GraphQLObjectType (graphql.schema.GraphQLObjectType)3 GraphQLTypeReference (graphql.schema.GraphQLTypeReference)3 AbstractDataFetcherException (io.smallrye.graphql.transformation.AbstractDataFetcherException)3 List (java.util.List)3 CompletionStage (java.util.concurrent.CompletionStage)3 BatchLoaderEnvironment (org.dataloader.BatchLoaderEnvironment)3 GraphQLException (org.eclipse.microprofile.graphql.GraphQLException)3 InterfaceResolver (io.smallrye.graphql.execution.resolver.InterfaceResolver)2 Argument (io.smallrye.graphql.schema.model.Argument)2 DirectiveInstance (io.smallrye.graphql.schema.model.DirectiveInstance)2 Multi (io.smallrye.mutiny.Multi)2 Infrastructure (io.smallrye.mutiny.infrastructure.Infrastructure)2