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);
}
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;
}
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);
}
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();
}
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());
}
Aggregations