Search in sources :

Example 1 with FieldCoordinates

use of graphql.schema.FieldCoordinates in project graphql-java by graphql-java.

the class SchemaGeneratorHelper method buildField.

GraphQLFieldDefinition buildField(BuildContext buildCtx, TypeDefinition<?> parentType, FieldDefinition fieldDef) {
    GraphQLFieldDefinition.Builder builder = GraphQLFieldDefinition.newFieldDefinition();
    builder.definition(buildCtx.isCaptureAstDefinitions() ? fieldDef : null);
    builder.name(fieldDef.getName());
    builder.description(buildDescription(buildCtx, fieldDef, fieldDef.getDescription()));
    builder.deprecate(buildDeprecationReason(fieldDef.getDirectives()));
    builder.comparatorRegistry(buildCtx.getComparatorRegistry());
    Pair<List<GraphQLDirective>, List<GraphQLAppliedDirective>> appliedDirectives = buildAppliedDirectives(buildCtx, inputTypeFactory(buildCtx), fieldDef.getDirectives(), emptyList(), FIELD_DEFINITION, buildCtx.getDirectives(), buildCtx.getComparatorRegistry());
    buildAppliedDirectives(buildCtx, builder, appliedDirectives);
    fieldDef.getInputValueDefinitions().forEach(inputValueDefinition -> builder.argument(buildArgument(buildCtx, inputValueDefinition)));
    GraphQLOutputType fieldType = buildOutputType(buildCtx, fieldDef.getType());
    builder.type(fieldType);
    GraphQLFieldDefinition fieldDefinition = builder.build();
    // if they have already wired in a fetcher - then leave it alone
    FieldCoordinates coordinates = FieldCoordinates.coordinates(parentType.getName(), fieldDefinition.getName());
    if (!buildCtx.getCodeRegistry().hasDataFetcher(coordinates)) {
        DataFetcherFactory<?> dataFetcherFactory = buildDataFetcherFactory(buildCtx, parentType, fieldDef, fieldType, appliedDirectives.first, appliedDirectives.second);
        buildCtx.getCodeRegistry().dataFetcher(coordinates, dataFetcherFactory);
    }
    return directivesObserve(buildCtx, fieldDefinition);
}
Also used : GraphQLOutputType(graphql.schema.GraphQLOutputType) GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) ArrayList(java.util.ArrayList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) FieldCoordinates(graphql.schema.FieldCoordinates)

Example 2 with FieldCoordinates

use of graphql.schema.FieldCoordinates in project graphql-java by graphql-java.

the class SchemaDirectiveWiringEnvironmentImpl method setFieldDataFetcher.

@Override
public GraphQLFieldDefinition setFieldDataFetcher(DataFetcher<?> newDataFetcher) {
    assertNotNull(fieldDefinition, () -> "An output field must be in context to call this method");
    assertNotNull(fieldsContainer, () -> "An output field container must be in context to call this method");
    FieldCoordinates coordinates = FieldCoordinates.coordinates(fieldsContainer, fieldDefinition);
    codeRegistry.dataFetcher(coordinates, newDataFetcher);
    return fieldDefinition;
}
Also used : FieldCoordinates(graphql.schema.FieldCoordinates)

Example 3 with FieldCoordinates

use of graphql.schema.FieldCoordinates in project graphql-java by graphql-java.

the class SchemaTransformExamples method trasnformSchema.

void trasnformSchema() {
    GraphQLTypeVisitorStub visitor = new GraphQLTypeVisitorStub() {

        @Override
        public TraversalControl visitGraphQLObjectType(GraphQLObjectType objectType, TraverserContext<GraphQLSchemaElement> context) {
            GraphQLCodeRegistry.Builder codeRegistry = context.getVarFromParents(GraphQLCodeRegistry.Builder.class);
            // we need to change __XXX introspection types to have directive extensions
            if (someConditionalLogic(objectType)) {
                GraphQLObjectType newObjectType = buildChangedObjectType(objectType, codeRegistry);
                return changeNode(context, newObjectType);
            }
            return CONTINUE;
        }

        private boolean someConditionalLogic(GraphQLObjectType objectType) {
            // up to you to decide what causes a change, perhaps a directive is on the element
            return objectType.hasDirective("specialDirective");
        }

        private GraphQLObjectType buildChangedObjectType(GraphQLObjectType objectType, GraphQLCodeRegistry.Builder codeRegistry) {
            GraphQLFieldDefinition newField = GraphQLFieldDefinition.newFieldDefinition().name("newField").type(Scalars.GraphQLString).build();
            GraphQLObjectType newObjectType = objectType.transform(builder -> builder.field(newField));
            DataFetcher newDataFetcher = dataFetchingEnvironment -> {
                return "someValueForTheNewField";
            };
            FieldCoordinates coordinates = FieldCoordinates.coordinates(objectType.getName(), newField.getName());
            codeRegistry.dataFetcher(coordinates, newDataFetcher);
            return newObjectType;
        }
    };
    GraphQLSchema newSchema = SchemaTransformer.transformSchema(schema, visitor);
}
Also used : GraphQLObjectType(graphql.schema.GraphQLObjectType) FieldCoordinates(graphql.schema.FieldCoordinates) GraphQLSchemaElement(graphql.schema.GraphQLSchemaElement) GraphQLCodeRegistry(graphql.schema.GraphQLCodeRegistry) SchemaTransformer(graphql.schema.SchemaTransformer) TraversalControl(graphql.util.TraversalControl) CONTINUE(graphql.util.TraversalControl.CONTINUE) GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) GraphQLTypeVisitorStub(graphql.schema.GraphQLTypeVisitorStub) TraverserContext(graphql.util.TraverserContext) Scalars(graphql.Scalars) DataFetcher(graphql.schema.DataFetcher) GraphQLSchema(graphql.schema.GraphQLSchema) GraphQLObjectType(graphql.schema.GraphQLObjectType) GraphQLCodeRegistry(graphql.schema.GraphQLCodeRegistry) GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) DataFetcher(graphql.schema.DataFetcher) FieldCoordinates(graphql.schema.FieldCoordinates) GraphQLTypeVisitorStub(graphql.schema.GraphQLTypeVisitorStub) GraphQLSchema(graphql.schema.GraphQLSchema) TraverserContext(graphql.util.TraverserContext)

Example 4 with FieldCoordinates

use of graphql.schema.FieldCoordinates in project spring-graphql by spring-projects.

the class AnnotatedControllerConfigurer method findHandlerMethods.

/**
 * Scan beans in the ApplicationContext, detect and prepare a map of handler methods.
 */
private Collection<MappingInfo> findHandlerMethods() {
    ApplicationContext context = obtainApplicationContext();
    Map<FieldCoordinates, MappingInfo> result = new HashMap<>();
    for (String beanName : context.getBeanNamesForType(Object.class)) {
        if (beanName.startsWith(SCOPED_TARGET_NAME_PREFIX)) {
            continue;
        }
        Class<?> beanType = null;
        try {
            beanType = context.getType(beanName);
        } catch (Throwable ex) {
            // An unresolvable bean type, probably from a lazy bean - let's ignore it.
            if (logger.isTraceEnabled()) {
                logger.trace("Could not resolve type for bean '" + beanName + "'", ex);
            }
        }
        if (beanType == null || !AnnotatedElementUtils.hasAnnotation(beanType, Controller.class)) {
            continue;
        }
        Class<?> beanClass = context.getType(beanName);
        findHandlerMethods(beanName, beanClass).forEach((info) -> {
            HandlerMethod handlerMethod = info.getHandlerMethod();
            MappingInfo existing = result.put(info.getCoordinates(), info);
            if (existing != null && !existing.getHandlerMethod().equals(handlerMethod)) {
                throw new IllegalStateException("Ambiguous mapping. Cannot map '" + handlerMethod.getBean() + "' method \n" + handlerMethod + "\nto " + info.getCoordinates() + ": There is already '" + existing.getHandlerMethod().getBean() + "' bean method\n" + existing + " mapped.");
            }
        });
    }
    return result.values();
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) HashMap(java.util.HashMap) FieldCoordinates(graphql.schema.FieldCoordinates) HandlerMethod(org.springframework.graphql.data.method.HandlerMethod)

Example 5 with FieldCoordinates

use of graphql.schema.FieldCoordinates in project graphql-java by graphql-java.

the class ExecutableNormalizedOperationFactory method createNormalizedQueryImpl.

/**
 * Creates a new Normalized query tree for the provided query
 */
private ExecutableNormalizedOperation createNormalizedQueryImpl(GraphQLSchema graphQLSchema, OperationDefinition operationDefinition, Map<String, FragmentDefinition> fragments, Map<String, Object> coercedVariableValues, @Nullable Map<String, NormalizedInputValue> normalizedVariableValues) {
    FieldCollectorNormalizedQueryParams parameters = FieldCollectorNormalizedQueryParams.newParameters().fragments(fragments).schema(graphQLSchema).coercedVariables(coercedVariableValues).normalizedVariables(normalizedVariableValues).build();
    GraphQLObjectType rootType = Common.getOperationRootType(graphQLSchema, operationDefinition);
    CollectNFResult collectFromOperationResult = collectFromOperation(parameters, operationDefinition, rootType);
    ImmutableListMultimap.Builder<Field, ExecutableNormalizedField> fieldToNormalizedField = ImmutableListMultimap.builder();
    ImmutableMap.Builder<ExecutableNormalizedField, MergedField> normalizedFieldToMergedField = ImmutableMap.builder();
    ImmutableListMultimap.Builder<FieldCoordinates, ExecutableNormalizedField> coordinatesToNormalizedFields = ImmutableListMultimap.builder();
    for (ExecutableNormalizedField topLevel : collectFromOperationResult.children) {
        ImmutableList<FieldAndAstParent> mergedField = collectFromOperationResult.normalizedFieldToAstFields.get(topLevel);
        normalizedFieldToMergedField.put(topLevel, newMergedField(map(mergedField, fieldAndAstParent -> fieldAndAstParent.field)).build());
        updateFieldToNFMap(topLevel, mergedField, fieldToNormalizedField);
        updateCoordinatedToNFMap(coordinatesToNormalizedFields, topLevel);
        buildFieldWithChildren(topLevel, mergedField, parameters, fieldToNormalizedField, normalizedFieldToMergedField, coordinatesToNormalizedFields, 1);
    }
    for (FieldCollectorNormalizedQueryParams.PossibleMerger possibleMerger : parameters.possibleMergerList) {
        List<ExecutableNormalizedField> childrenWithSameResultKey = possibleMerger.parent.getChildrenWithSameResultKey(possibleMerger.resultKey);
        ENFMerger.merge(possibleMerger.parent, childrenWithSameResultKey, graphQLSchema);
    }
    return new ExecutableNormalizedOperation(operationDefinition.getOperation(), operationDefinition.getName(), new ArrayList<>(collectFromOperationResult.children), fieldToNormalizedField.build(), normalizedFieldToMergedField.build(), coordinatesToNormalizedFields.build());
}
Also used : OperationDefinition(graphql.language.OperationDefinition) NodeUtil(graphql.language.NodeUtil) ValuesResolver(graphql.execution.ValuesResolver) GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) FragmentSpread(graphql.language.FragmentSpread) GraphQLInterfaceType(graphql.schema.GraphQLInterfaceType) GraphQLUnionType(graphql.schema.GraphQLUnionType) Collections.singletonList(java.util.Collections.singletonList) Selection(graphql.language.Selection) Assert.assertShouldNeverHappen(graphql.Assert.assertShouldNeverHappen) Collections.singleton(java.util.Collections.singleton) Map(java.util.Map) GraphQLCompositeType(graphql.schema.GraphQLCompositeType) SelectionSet(graphql.language.SelectionSet) GraphQLObjectType(graphql.schema.GraphQLObjectType) ImmutableSet(com.google.common.collect.ImmutableSet) MergedField.newMergedField(graphql.execution.MergedField.newMergedField) ImmutableMap(com.google.common.collect.ImmutableMap) Predicate(java.util.function.Predicate) FpKit.groupingBy(graphql.util.FpKit.groupingBy) Collection(java.util.Collection) Set(java.util.Set) Sets(com.google.common.collect.Sets) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) ConditionalNodes(graphql.execution.ConditionalNodes) FragmentDefinition(graphql.language.FragmentDefinition) FieldCoordinates(graphql.schema.FieldCoordinates) Internal(graphql.Internal) GraphQLTypeUtil.unwrapAll(graphql.schema.GraphQLTypeUtil.unwrapAll) GraphQLType(graphql.schema.GraphQLType) ArrayList(java.util.ArrayList) Introspection(graphql.introspection.Introspection) LinkedHashMap(java.util.LinkedHashMap) ImmutableList(com.google.common.collect.ImmutableList) Common(graphql.execution.nextgen.Common) GraphQLSchema(graphql.schema.GraphQLSchema) GraphQLTypeUtil(graphql.schema.GraphQLTypeUtil) MergedField(graphql.execution.MergedField) GraphQLUnmodifiedType(graphql.schema.GraphQLUnmodifiedType) Field(graphql.language.Field) FpKit.filterSet(graphql.util.FpKit.filterSet) Document(graphql.language.Document) VariableDefinition(graphql.language.VariableDefinition) ImmutableKit.map(graphql.collect.ImmutableKit.map) Assert.assertNotNull(graphql.Assert.assertNotNull) InlineFragment(graphql.language.InlineFragment) Collections(java.util.Collections) FieldCoordinates(graphql.schema.FieldCoordinates) ImmutableMap(com.google.common.collect.ImmutableMap) MergedField.newMergedField(graphql.execution.MergedField.newMergedField) MergedField(graphql.execution.MergedField) Field(graphql.language.Field) MergedField.newMergedField(graphql.execution.MergedField.newMergedField) MergedField(graphql.execution.MergedField) GraphQLObjectType(graphql.schema.GraphQLObjectType) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap)

Aggregations

FieldCoordinates (graphql.schema.FieldCoordinates)8 GraphQLFieldDefinition (graphql.schema.GraphQLFieldDefinition)4 GraphQLObjectType (graphql.schema.GraphQLObjectType)2 GraphQLSchema (graphql.schema.GraphQLSchema)2 ArrayList (java.util.ArrayList)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableListMultimap (com.google.common.collect.ImmutableListMultimap)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Sets (com.google.common.collect.Sets)1 Assert.assertNotNull (graphql.Assert.assertNotNull)1 Assert.assertShouldNeverHappen (graphql.Assert.assertShouldNeverHappen)1 Internal (graphql.Internal)1 Scalars (graphql.Scalars)1 ImmutableKit.map (graphql.collect.ImmutableKit.map)1 ConditionalNodes (graphql.execution.ConditionalNodes)1 MergedField (graphql.execution.MergedField)1 MergedField.newMergedField (graphql.execution.MergedField.newMergedField)1 ValuesResolver (graphql.execution.ValuesResolver)1 Common (graphql.execution.nextgen.Common)1