Search in sources :

Example 6 with GraphQLSchemaElement

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;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) GraphQLSchemaElement(graphql.schema.GraphQLSchemaElement) ArrayList(java.util.ArrayList)

Example 7 with GraphQLSchemaElement

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);
}
Also used : GraphQLSchemaElement(graphql.schema.GraphQLSchemaElement)

Example 8 with GraphQLSchemaElement

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);
}
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 9 with GraphQLSchemaElement

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));
}
Also used : GraphQLSchemaElement(graphql.schema.GraphQLSchemaElement) ColumnType(io.stargate.db.schema.Column.ColumnType) Column(io.stargate.db.schema.Column) GraphQLType(graphql.schema.GraphQLType) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 10 with GraphQLSchemaElement

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");
}
Also used : GraphQLSchemaElement(graphql.schema.GraphQLSchemaElement) BeforeEach(org.junit.jupiter.api.BeforeEach) GraphQLNamedType(graphql.schema.GraphQLNamedType) Arrays(java.util.Arrays) GraphQLInputObjectType(graphql.schema.GraphQLInputObjectType) Mock(org.mockito.Mock) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) GraphQLScalarType(graphql.schema.GraphQLScalarType) GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) GraphQLInputObjectField(graphql.schema.GraphQLInputObjectField) GraphQLNamedInputType(graphql.schema.GraphQLNamedInputType) GraphQLType(graphql.schema.GraphQLType) Column(io.stargate.db.schema.Column) GraphQLNamedSchemaElement(graphql.schema.GraphQLNamedSchemaElement) ArrayList(java.util.ArrayList) Scalars(graphql.Scalars) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) ColumnType(io.stargate.db.schema.Column.ColumnType) Arguments.arguments(org.junit.jupiter.params.provider.Arguments.arguments) MethodSource(org.junit.jupiter.params.provider.MethodSource) GraphQLObjectType(graphql.schema.GraphQLObjectType) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) GraphQLNamedOutputType(graphql.schema.GraphQLNamedOutputType) GraphQLNonNull(graphql.schema.GraphQLNonNull) GraphQLInputType(graphql.schema.GraphQLInputType) GraphQLOutputType(graphql.schema.GraphQLOutputType) Arguments(org.junit.jupiter.params.provider.Arguments) Collectors(java.util.stream.Collectors) Type(io.stargate.db.schema.Column.Type) List(java.util.List) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) GraphQLList(graphql.schema.GraphQLList) SchemaAssertions(io.stargate.graphql.schema.SchemaAssertions) Stream(java.util.stream.Stream) CqlScalar(io.stargate.graphql.schema.scalars.CqlScalar) GraphQLSchemaElement(graphql.schema.GraphQLSchemaElement) GraphQLNamedSchemaElement(graphql.schema.GraphQLNamedSchemaElement) GraphQLNamedType(graphql.schema.GraphQLNamedType)

Aggregations

GraphQLSchemaElement (graphql.schema.GraphQLSchemaElement)23 GraphQLType (graphql.schema.GraphQLType)17 GraphQLObjectType (graphql.schema.GraphQLObjectType)15 GraphQLFieldDefinition (graphql.schema.GraphQLFieldDefinition)14 GraphQLNamedType (graphql.schema.GraphQLNamedType)14 List (java.util.List)13 GraphQLInputObjectField (graphql.schema.GraphQLInputObjectField)12 ArrayList (java.util.ArrayList)12 GraphQLDirective (graphql.schema.GraphQLDirective)11 GraphQLInputObjectType (graphql.schema.GraphQLInputObjectType)11 GraphQLInputType (graphql.schema.GraphQLInputType)11 GraphQLNamedOutputType (graphql.schema.GraphQLNamedOutputType)11 GraphQLScalarType (graphql.schema.GraphQLScalarType)11 GraphQLArgument (graphql.schema.GraphQLArgument)10 GraphQLSchema (graphql.schema.GraphQLSchema)10 Map (java.util.Map)10 Collectors (java.util.stream.Collectors)10 GraphQLInterfaceType (graphql.schema.GraphQLInterfaceType)9 GraphQLOutputType (graphql.schema.GraphQLOutputType)9 GraphQLUnionType (graphql.schema.GraphQLUnionType)9