use of graphql.schema.GraphQLSchemaElement in project graphql-java by graphql-java.
the class StronglyConnectedComponentsTopologicallySorted method topologicallySort.
private List<GraphQLSchemaElement> topologicallySort(Set<GraphQLSchemaElement> allNodes) {
List<GraphQLSchemaElement> result = new ArrayList<>();
Set<GraphQLSchemaElement> notPermMarked = new LinkedHashSet<>(allNodes);
Set<GraphQLSchemaElement> tempMarked = new LinkedHashSet<>();
Set<GraphQLSchemaElement> permMarked = new LinkedHashSet<>();
/*
* Taken from: https://en.wikipedia.org/wiki/Topological_sorting#Depth-first_search
* while exists nodes without a permanent mark do
* select an unmarked node n
* visit(n)
*/
while (true) {
Iterator<GraphQLSchemaElement> iterator = notPermMarked.iterator();
if (!iterator.hasNext()) {
break;
}
GraphQLSchemaElement n = iterator.next();
iterator.remove();
visit(n, tempMarked, permMarked, notPermMarked, result, allNodes);
}
return result;
}
use of graphql.schema.GraphQLSchemaElement in project graphql-java by graphql-java.
the class StronglyConnectedComponentsTopologicallySorted method visit.
private void visit(GraphQLSchemaElement n, Set<GraphQLSchemaElement> tempMarked, Set<GraphQLSchemaElement> permMarked, Set<GraphQLSchemaElement> notPermMarked, List<GraphQLSchemaElement> result, Set<GraphQLSchemaElement> allNodes) {
/*
* Taken from: https://en.wikipedia.org/wiki/Topological_sorting#Depth-first_search
* if n has a permanent mark then
* return
* if n has a temporary mark then
* stop (not a DAG)
*
* mark n with a temporary mark
*
* for each node m with an edge from n to m do
* visit(m)
*
* remove temporary mark from n
* mark n with a permanent mark
* add n to head of L
*/
if (permMarked.contains(n)) {
return;
}
if (tempMarked.contains(n)) {
// https://en.wikipedia.org/wiki/Directed_acyclic_graph
Assert.assertShouldNeverHappen("This schema is not forming an Directed Acyclic Graph : %s has already has a temporary mark", n);
return;
}
tempMarked.add(n);
if (reverseDependencies.containsKey(n)) {
for (GraphQLSchemaElement m : reverseDependencies.get(n)) {
if (allNodes.contains(m)) {
visit(m, tempMarked, permMarked, notPermMarked, result, allNodes);
}
}
}
tempMarked.remove(n);
permMarked.add(n);
notPermMarked.remove(n);
result.add(n);
}
use of graphql.schema.GraphQLSchemaElement 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.GraphQLSchemaElement in project stargate by stargate.
the class FieldTypeCachesTest method getGraphQLTypeShouldReuseTheSameInstanceForMaps.
@ParameterizedTest
@MethodSource("getMapArgs")
public void getGraphQLTypeShouldReuseTheSameInstanceForMaps(Column.ColumnType keyDbType, Column.ColumnType valueDbType) {
Column.ColumnType mapDbType = Column.Type.Map.of(keyDbType, valueDbType);
GraphQLType graphTypeParentType = getOutputType(mapDbType);
assertThat(graphTypeParentType).isInstanceOf(GraphQLList.class);
GraphQLSchemaElement childObjectType = graphTypeParentType.getChildren().get(0);
// Following calls should yield the same instance
assertThat(childObjectType).isSameAs(getOutputType(mapDbType).getChildren().get(0));
}
use of graphql.schema.GraphQLSchemaElement in project stargate by stargate.
the class FieldTypeCachesTest method testNestedMaps.
private static void testNestedMaps(GraphQLType parentGraphType, String name) {
assertThat(parentGraphType).isInstanceOf(GraphQLList.class);
GraphQLSchemaElement graphType = parentGraphType.getChildren().get(0);
assertThat(graphType).isInstanceOf(GraphQLNamedType.class);
GraphQLNamedType graphQLInputType = (GraphQLNamedType) graphType;
assertThat(graphQLInputType.getName()).isEqualTo(name);
assertThat(graphQLInputType.getChildren()).hasSize(2);
List<GraphQLNamedSchemaElement> fields = graphQLInputType.getChildren().stream().map(f -> (GraphQLNamedSchemaElement) f).collect(Collectors.toList());
assertThat(fields.get(0).getName()).isEqualTo("key");
assertThat(fields.get(1).getName()).isEqualTo("value");
}
Aggregations