Search in sources :

Example 11 with ExecutionStepInfo

use of graphql.execution.ExecutionStepInfo in project graphql-orchestrator-java by intuit.

the class GraphQLServiceBatchLoaderTest method callsAllHooks.

@Test
public void callsAllHooks() {
    QueryExecutor emptyFn = (input, context) -> CompletableFuture.completedFuture(new HashMap<>());
    final BatchLoaderExecutionHooks mockHooks = mock(BatchLoaderExecutionHooks.class);
    GraphQLServiceBatchLoader loader = newQueryExecutorBatchLoader().queryExecutor(emptyFn).serviceMetadata(mockServiceMetadata).batchLoaderExecutionHooks(mockHooks).build();
    GraphQLObjectType queryType = GraphQLObjectType.newObject().name("query").build();
    MergedField mergedField1 = newMergedField(newField("first").build()).build();
    GraphQLSchema graphQLSchema = newSchema().query(queryType).build();
    DataFetchingEnvironment dfe = newDataFetchingEnvironment().variables(ImmutableMap.of("1", "3")).graphQLSchema(graphQLSchema).context(GraphQLContext.newContext().build()).mergedField(mergedField1).parentType(queryType).executionStepInfo(ExecutionStepInfo.newExecutionStepInfo().path(ExecutionPath.parse("/first")).field(mergedField1).type(GraphQLObjectType.newObject().name("FirstType").build()).build()).build();
    loader.load(Collections.singletonList(dfe)).whenComplete((not, used) -> {
        verify(mockHooks, times(1)).onBatchLoadStart(any(), any());
        verify(mockHooks, times(1)).onExecutionInput(any(), any());
        verify(mockHooks, times(1)).onQueryResult(any(), any());
        verify(mockHooks, times(1)).onBatchLoadEnd(any(), any());
    }).toCompletableFuture().join();
}
Also used : OperationDefinition(graphql.language.OperationDefinition) DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) FragmentSpread(graphql.language.FragmentSpread) Collections.singletonList(java.util.Collections.singletonList) ExecutionStepInfo(graphql.execution.ExecutionStepInfo) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) Mockito.doReturn(org.mockito.Mockito.doReturn) SelectionSet(graphql.language.SelectionSet) GraphQLObjectType(graphql.schema.GraphQLObjectType) Operation(graphql.language.OperationDefinition.Operation) MergedField.newMergedField(graphql.execution.MergedField.newMergedField) ImmutableMap(com.google.common.collect.ImmutableMap) GraphQLServiceBatchLoader.newQueryExecutorBatchLoader(com.intuit.graphql.orchestrator.batch.GraphQLServiceBatchLoader.newQueryExecutorBatchLoader) BatchFieldAuthorization(com.intuit.graphql.orchestrator.authorization.BatchFieldAuthorization) GraphQLContext(graphql.GraphQLContext) FragmentDefinition(graphql.language.FragmentDefinition) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) MockitoAnnotations.initMocks(org.mockito.MockitoAnnotations.initMocks) VariableReference.newVariableReference(graphql.language.VariableReference.newVariableReference) Node(graphql.language.Node) Mock(org.mockito.Mock) ArgumentMatchers.anyMap(org.mockito.ArgumentMatchers.anyMap) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Field.newField(graphql.language.Field.newField) HashSet(java.util.HashSet) Scalars(graphql.Scalars) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) TypeName.newTypeName(graphql.language.TypeName.newTypeName) GraphQLSchema(graphql.schema.GraphQLSchema) PassthroughQueryModifier(com.intuit.graphql.orchestrator.batch.GraphQLTestUtil.PassthroughQueryModifier) XtextGraph(com.intuit.graphql.orchestrator.xtext.XtextGraph) Before(org.junit.Before) Collections.emptyMap(java.util.Collections.emptyMap) VariableDefinition.newVariableDefinition(graphql.language.VariableDefinition.newVariableDefinition) Mockito.times(org.mockito.Mockito.times) MergedField(graphql.execution.MergedField) Test(org.junit.Test) Field(graphql.language.Field) Mockito.verify(org.mockito.Mockito.verify) Directive(graphql.language.Directive) ExecutionPath(graphql.execution.ExecutionPath) Argument(graphql.language.Argument) Document(graphql.language.Document) Mockito(org.mockito.Mockito) Mockito.never(org.mockito.Mockito.never) GraphQLSchema.newSchema(graphql.schema.GraphQLSchema.newSchema) DataFetchingEnvironmentImpl.newDataFetchingEnvironment(graphql.schema.DataFetchingEnvironmentImpl.newDataFetchingEnvironment) OperationDefinition.newOperationDefinition(graphql.language.OperationDefinition.newOperationDefinition) Collections(java.util.Collections) MergedField.newMergedField(graphql.execution.MergedField.newMergedField) MergedField(graphql.execution.MergedField) GraphQLObjectType(graphql.schema.GraphQLObjectType) GraphQLSchema(graphql.schema.GraphQLSchema) DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) DataFetchingEnvironmentImpl.newDataFetchingEnvironment(graphql.schema.DataFetchingEnvironmentImpl.newDataFetchingEnvironment) Test(org.junit.Test)

Example 12 with ExecutionStepInfo

use of graphql.execution.ExecutionStepInfo in project graphql-orchestrator-java by intuit.

the class GraphQLServiceBatchLoaderTest method makesCorrectBatchQueryWithCustomFieldAuthorization.

@Test
public void makesCorrectBatchQueryWithCustomFieldAuthorization() {
    QueryExecutor validator = (environment, context) -> {
        assertThat(environment.getVariables()).describedAs("Batch Loader merges variables").extracting("1", "2").containsOnly("3", "4");
        assertThat(environment.getQuery()).contains("query", "first", "second");
        assertThat(environment.getOperationName()).isEqualTo("QUERY");
        assertThat(environment.getRoot()).isNotNull();
        return CompletableFuture.completedFuture(new HashMap<>());
    };
    GraphQLObjectType queryType = GraphQLObjectType.newObject().name("query").build();
    MergedField mergedField1 = newMergedField(newField("first").build()).build();
    MergedField mergedField2 = newMergedField(newField("second").build()).build();
    GraphQLSchema graphQLSchema = newSchema().query(queryType).build();
    GraphQLContext graphQLContext = GraphQLContext.newContext().build();
    graphQLContext.put(BatchFieldAuthorization.class, mockBatchFieldAuthorization);
    DataFetchingEnvironment dfe1 = newDataFetchingEnvironment().variables(ImmutableMap.of("1", "3")).graphQLSchema(graphQLSchema).context(graphQLContext).mergedField(mergedField1).parentType(queryType).executionStepInfo(ExecutionStepInfo.newExecutionStepInfo().path(ExecutionPath.parse("/first")).field(mergedField1).type(GraphQLObjectType.newObject().name("FirstType").build()).build()).build();
    DataFetchingEnvironment dfe2 = newDataFetchingEnvironment().variables(ImmutableMap.of("2", "4")).context(GraphQLContext.newContext().build()).graphQLSchema(graphQLSchema).mergedField(mergedField2).parentType(queryType).executionStepInfo(ExecutionStepInfo.newExecutionStepInfo().path(ExecutionPath.parse("/second")).field(mergedField2).type(GraphQLObjectType.newObject().name("SecondType").build()).build()).build();
    GraphQLServiceBatchLoader batchLoader = GraphQLServiceBatchLoader.newQueryExecutorBatchLoader().queryExecutor(validator).serviceMetadata(mockServiceMetadata).queryOperationModifier(new PassthroughQueryModifier()).build();
    batchLoader.variableDefinitionFilter = mockVariableDefinitionFilter;
    batchLoader.load(asList(dfe1, dfe2));
}
Also used : OperationDefinition(graphql.language.OperationDefinition) DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) FragmentSpread(graphql.language.FragmentSpread) Collections.singletonList(java.util.Collections.singletonList) ExecutionStepInfo(graphql.execution.ExecutionStepInfo) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) Mockito.doReturn(org.mockito.Mockito.doReturn) SelectionSet(graphql.language.SelectionSet) GraphQLObjectType(graphql.schema.GraphQLObjectType) Operation(graphql.language.OperationDefinition.Operation) MergedField.newMergedField(graphql.execution.MergedField.newMergedField) ImmutableMap(com.google.common.collect.ImmutableMap) GraphQLServiceBatchLoader.newQueryExecutorBatchLoader(com.intuit.graphql.orchestrator.batch.GraphQLServiceBatchLoader.newQueryExecutorBatchLoader) BatchFieldAuthorization(com.intuit.graphql.orchestrator.authorization.BatchFieldAuthorization) GraphQLContext(graphql.GraphQLContext) FragmentDefinition(graphql.language.FragmentDefinition) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) MockitoAnnotations.initMocks(org.mockito.MockitoAnnotations.initMocks) VariableReference.newVariableReference(graphql.language.VariableReference.newVariableReference) Node(graphql.language.Node) Mock(org.mockito.Mock) ArgumentMatchers.anyMap(org.mockito.ArgumentMatchers.anyMap) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Field.newField(graphql.language.Field.newField) HashSet(java.util.HashSet) Scalars(graphql.Scalars) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) TypeName.newTypeName(graphql.language.TypeName.newTypeName) GraphQLSchema(graphql.schema.GraphQLSchema) PassthroughQueryModifier(com.intuit.graphql.orchestrator.batch.GraphQLTestUtil.PassthroughQueryModifier) XtextGraph(com.intuit.graphql.orchestrator.xtext.XtextGraph) Before(org.junit.Before) Collections.emptyMap(java.util.Collections.emptyMap) VariableDefinition.newVariableDefinition(graphql.language.VariableDefinition.newVariableDefinition) Mockito.times(org.mockito.Mockito.times) MergedField(graphql.execution.MergedField) Test(org.junit.Test) Field(graphql.language.Field) Mockito.verify(org.mockito.Mockito.verify) Directive(graphql.language.Directive) ExecutionPath(graphql.execution.ExecutionPath) Argument(graphql.language.Argument) Document(graphql.language.Document) Mockito(org.mockito.Mockito) Mockito.never(org.mockito.Mockito.never) GraphQLSchema.newSchema(graphql.schema.GraphQLSchema.newSchema) DataFetchingEnvironmentImpl.newDataFetchingEnvironment(graphql.schema.DataFetchingEnvironmentImpl.newDataFetchingEnvironment) OperationDefinition.newOperationDefinition(graphql.language.OperationDefinition.newOperationDefinition) Collections(java.util.Collections) PassthroughQueryModifier(com.intuit.graphql.orchestrator.batch.GraphQLTestUtil.PassthroughQueryModifier) HashMap(java.util.HashMap) MergedField.newMergedField(graphql.execution.MergedField.newMergedField) MergedField(graphql.execution.MergedField) GraphQLObjectType(graphql.schema.GraphQLObjectType) GraphQLContext(graphql.GraphQLContext) GraphQLSchema(graphql.schema.GraphQLSchema) DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) DataFetchingEnvironmentImpl.newDataFetchingEnvironment(graphql.schema.DataFetchingEnvironmentImpl.newDataFetchingEnvironment) Test(org.junit.Test)

Example 13 with ExecutionStepInfo

use of graphql.execution.ExecutionStepInfo in project graphql-orchestrator-java by intuit.

the class GraphQLServiceBatchLoaderTest method propagatesVariableDefinitions.

@Test
public void propagatesVariableDefinitions() {
    doReturn(new HashSet<String>() {

        {
            add("TestVariableDefinition");
            add("TestVariableDefinition2");
        }
    }).when(mockVariableDefinitionFilter).getVariableReferencesFromNode(any(GraphQLSchema.class), any(GraphQLObjectType.class), anyMap(), anyMap(), any(Node.class));
    QueryExecutor validator = (input, context) -> {
        assertThat(input.getQuery()).contains("Bulk_Query($TestVariableDefinition:TestType,$TestVariableDefinition2:TestType2)").contains("fieldWithArgument(SomeArgument:$TestVariableDefinition)");
        return CompletableFuture.completedFuture(new HashMap<>());
    };
    GraphQLObjectType queryType = GraphQLObjectType.newObject().name("query").build();
    GraphQLSchema graphQLSchema = newSchema().query(queryType).build();
    OperationDefinition operationWithVariableDefinitions = newOperationDefinition().name("Bulk_Query").variableDefinitions(asList(newVariableDefinition("TestVariableDefinition").type(newTypeName("TestType").build()).build(), newVariableDefinition("TestVariableDefinition2").type(newTypeName("TestType2").build()).build())).operation(Operation.QUERY).build();
    final MergedField mergedFieldWithArgument = newMergedField(newField("fieldWithArgument").arguments(singletonList(Argument.newArgument("SomeArgument", newVariableReference().name("TestVariableDefinition").build()).build())).build()).build();
    final ExecutionStepInfo root = ExecutionStepInfo.newExecutionStepInfo().type(GraphQLObjectType.newObject().name("FakeType").build()).path(ExecutionPath.rootPath()).build();
    DataFetchingEnvironment dfe1 = newDataFetchingEnvironment().variables(emptyMap()).graphQLSchema(graphQLSchema).operationDefinition(operationWithVariableDefinitions).context(GraphQLContext.newContext().build()).mergedField(mergedFieldWithArgument).executionStepInfo(ExecutionStepInfo.newExecutionStepInfo().path(ExecutionPath.parse("/fieldWithArgument")).parentInfo(root).field(mergedFieldWithArgument).type(GraphQLObjectType.newObject().name("FirstType").build()).build()).parentType(queryType).build();
    GraphQLServiceBatchLoader batchLoader = GraphQLServiceBatchLoader.newQueryExecutorBatchLoader().queryExecutor(validator).serviceMetadata(mockServiceMetadata).queryOperationModifier(new PassthroughQueryModifier()).build();
    batchLoader.variableDefinitionFilter = mockVariableDefinitionFilter;
    batchLoader.load(singletonList(dfe1));
}
Also used : OperationDefinition(graphql.language.OperationDefinition) DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) FragmentSpread(graphql.language.FragmentSpread) Collections.singletonList(java.util.Collections.singletonList) ExecutionStepInfo(graphql.execution.ExecutionStepInfo) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) Mockito.doReturn(org.mockito.Mockito.doReturn) SelectionSet(graphql.language.SelectionSet) GraphQLObjectType(graphql.schema.GraphQLObjectType) Operation(graphql.language.OperationDefinition.Operation) MergedField.newMergedField(graphql.execution.MergedField.newMergedField) ImmutableMap(com.google.common.collect.ImmutableMap) GraphQLServiceBatchLoader.newQueryExecutorBatchLoader(com.intuit.graphql.orchestrator.batch.GraphQLServiceBatchLoader.newQueryExecutorBatchLoader) BatchFieldAuthorization(com.intuit.graphql.orchestrator.authorization.BatchFieldAuthorization) GraphQLContext(graphql.GraphQLContext) FragmentDefinition(graphql.language.FragmentDefinition) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) MockitoAnnotations.initMocks(org.mockito.MockitoAnnotations.initMocks) VariableReference.newVariableReference(graphql.language.VariableReference.newVariableReference) Node(graphql.language.Node) Mock(org.mockito.Mock) ArgumentMatchers.anyMap(org.mockito.ArgumentMatchers.anyMap) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Field.newField(graphql.language.Field.newField) HashSet(java.util.HashSet) Scalars(graphql.Scalars) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) TypeName.newTypeName(graphql.language.TypeName.newTypeName) GraphQLSchema(graphql.schema.GraphQLSchema) PassthroughQueryModifier(com.intuit.graphql.orchestrator.batch.GraphQLTestUtil.PassthroughQueryModifier) XtextGraph(com.intuit.graphql.orchestrator.xtext.XtextGraph) Before(org.junit.Before) Collections.emptyMap(java.util.Collections.emptyMap) VariableDefinition.newVariableDefinition(graphql.language.VariableDefinition.newVariableDefinition) Mockito.times(org.mockito.Mockito.times) MergedField(graphql.execution.MergedField) Test(org.junit.Test) Field(graphql.language.Field) Mockito.verify(org.mockito.Mockito.verify) Directive(graphql.language.Directive) ExecutionPath(graphql.execution.ExecutionPath) Argument(graphql.language.Argument) Document(graphql.language.Document) Mockito(org.mockito.Mockito) Mockito.never(org.mockito.Mockito.never) GraphQLSchema.newSchema(graphql.schema.GraphQLSchema.newSchema) DataFetchingEnvironmentImpl.newDataFetchingEnvironment(graphql.schema.DataFetchingEnvironmentImpl.newDataFetchingEnvironment) OperationDefinition.newOperationDefinition(graphql.language.OperationDefinition.newOperationDefinition) Collections(java.util.Collections) HashMap(java.util.HashMap) Node(graphql.language.Node) GraphQLSchema(graphql.schema.GraphQLSchema) DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) DataFetchingEnvironmentImpl.newDataFetchingEnvironment(graphql.schema.DataFetchingEnvironmentImpl.newDataFetchingEnvironment) PassthroughQueryModifier(com.intuit.graphql.orchestrator.batch.GraphQLTestUtil.PassthroughQueryModifier) MergedField.newMergedField(graphql.execution.MergedField.newMergedField) MergedField(graphql.execution.MergedField) ExecutionStepInfo(graphql.execution.ExecutionStepInfo) GraphQLObjectType(graphql.schema.GraphQLObjectType) OperationDefinition(graphql.language.OperationDefinition) OperationDefinition.newOperationDefinition(graphql.language.OperationDefinition.newOperationDefinition) Test(org.junit.Test)

Example 14 with ExecutionStepInfo

use of graphql.execution.ExecutionStepInfo in project graphql-orchestrator-java by intuit.

the class GraphQLTestUtil method buildCompleteExecutionStepInfo.

public static ExecutionStepInfo buildCompleteExecutionStepInfo(Document document, String... path) {
    OperationDefinition query = document.getDefinitionsOfType(OperationDefinition.class).get(0);
    Map<String, FragmentDefinition> fragmentDefinitions = document.getDefinitionsOfType(FragmentDefinition.class).stream().collect(Collectors.toMap(FragmentDefinition::getName, Function.identity()));
    ExecutionStepInfo parent = newExecutionStepInfo().type(newObject().name("FakeType").build()).path(rootPath()).build();
    Queue<String> paths = new LinkedList<>(Arrays.asList(path));
    Stack<ExecutionStepInfo> hierarchy = new Stack<>();
    hierarchy.push(parent);
    for (final Selection selection : query.getSelectionSet().getSelections()) {
        final Field field = (Field) selection;
        if (field.getName().equals(paths.peek())) {
            paths.poll();
            buildExecutionStepInfo(field, parent, fragmentDefinitions, "", hierarchy, paths);
        }
    }
    return hierarchy.pop();
}
Also used : MergedField.newMergedField(graphql.execution.MergedField.newMergedField) Field(graphql.language.Field) FragmentDefinition(graphql.language.FragmentDefinition) ExecutionStepInfo(graphql.execution.ExecutionStepInfo) ExecutionStepInfo.newExecutionStepInfo(graphql.execution.ExecutionStepInfo.newExecutionStepInfo) Selection(graphql.language.Selection) OperationDefinition(graphql.language.OperationDefinition) LinkedList(java.util.LinkedList) Stack(java.util.Stack)

Example 15 with ExecutionStepInfo

use of graphql.execution.ExecutionStepInfo in project graphql-orchestrator-java by intuit.

the class MergedFieldModifierTest method filtersNonRelevantFields.

@Test
public void filtersNonRelevantFields() {
    ExecutionStepInfo executionStepInfo = buildCompleteExecutionStepInfo(document, "consumer", "finance", "tax");
    DataFetchingEnvironment dataFetchingEnvironment = newDataFetchingEnvironment().executionStepInfo(executionStepInfo).mergedField(executionStepInfo.getField()).build();
    final MergedFieldModifierResult result = new MergedFieldModifier(dataFetchingEnvironment).getFilteredRootField();
    assertThat(printPreOrder(result.getMergedField().getSingleField(), graphQLSchema, result.getFragmentDefinitions())).containsExactly("consumer", "finance", "tax", "returns", "returnHeader", "taxYr", "inline:someField");
}
Also used : ExecutionStepInfo(graphql.execution.ExecutionStepInfo) GraphQLTestUtil.buildCompleteExecutionStepInfo(com.intuit.graphql.orchestrator.batch.GraphQLTestUtil.buildCompleteExecutionStepInfo) MergedFieldModifierResult(com.intuit.graphql.orchestrator.batch.MergedFieldModifier.MergedFieldModifierResult) DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) DataFetchingEnvironmentImpl.newDataFetchingEnvironment(graphql.schema.DataFetchingEnvironmentImpl.newDataFetchingEnvironment) Test(org.junit.Test)

Aggregations

ExecutionStepInfo (graphql.execution.ExecutionStepInfo)47 DataFetchingEnvironment (graphql.schema.DataFetchingEnvironment)28 HashMap (java.util.HashMap)25 Field (graphql.language.Field)24 MergedField (graphql.execution.MergedField)23 Map (java.util.Map)23 Test (org.junit.Test)23 DataFetchingEnvironmentImpl.newDataFetchingEnvironment (graphql.schema.DataFetchingEnvironmentImpl.newDataFetchingEnvironment)22 CompletableFuture (java.util.concurrent.CompletableFuture)22 OperationDefinition (graphql.language.OperationDefinition)19 GraphQLObjectType (graphql.schema.GraphQLObjectType)19 Collections (java.util.Collections)18 Argument (graphql.language.Argument)16 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)16 Before (org.junit.Before)16 Scalars (graphql.Scalars)15 ExecutionPath (graphql.execution.ExecutionPath)15 MergedField.newMergedField (graphql.execution.MergedField.newMergedField)15 FragmentDefinition (graphql.language.FragmentDefinition)15 GraphQLSchema (graphql.schema.GraphQLSchema)15