Search in sources :

Example 1 with GraphQLDataFetcherManager

use of com.tvd12.ezyhttp.server.graphql.GraphQLDataFetcherManager in project ezyhttp by youngmonkeys.

the class GraphQLConfigurationTest method test.

@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void test() throws NoSuchFieldException, IllegalAccessException {
    // given
    EzyBeanContextBuilder builder = new EzySimpleBeanContext.Builder();
    Set<String> packagesToScan = RandomUtil.randomSet(8, String.class);
    packagesToScan.add("com.tvd12.ezyhttp.server.graphql.test.config");
    for (String p : packagesToScan) {
        builder.scan(p);
    }
    EzyBeanContext context = builder.build();
    GraphQLConfiguration sut = new GraphQLConfiguration();
    EzySingletonFactory singletonFactory = context.getSingletonFactory();
    sut.setSingletonFactory(singletonFactory);
    sut.setObjectMapper(new ObjectMapper());
    sut.setGraphQLEnable(true);
    // when
    sut.config();
    GraphQLController controller = (GraphQLController) singletonFactory.getSingleton(GraphQLController.class);
    Field dataFetcherManagerField = GraphQLController.class.getDeclaredField("dataFetcherManager");
    dataFetcherManagerField.setAccessible(true);
    GraphQLDataFetcherManager dataFetcherManager = (GraphQLDataFetcherManager) dataFetcherManagerField.get(controller);
    Field dataFetchersField = GraphQLDataFetcherManager.class.getDeclaredField("dataFetchers");
    dataFetchersField.setAccessible(true);
    Map<String, GraphQLDataFetcher> dataFetchers = (Map<String, GraphQLDataFetcher>) dataFetchersField.get(dataFetcherManager);
    // then
    Asserts.assertNotNull(controller);
    Asserts.assertTrue(dataFetchers.containsKey("A"));
}
Also used : GraphQLDataFetcher(com.tvd12.ezyhttp.server.graphql.GraphQLDataFetcher) EzyBeanContext(com.tvd12.ezyfox.bean.EzyBeanContext) EzyBeanContextBuilder(com.tvd12.ezyfox.bean.EzyBeanContextBuilder) GraphQLController(com.tvd12.ezyhttp.server.graphql.controller.GraphQLController) EzySingletonFactory(com.tvd12.ezyfox.bean.EzySingletonFactory) Field(java.lang.reflect.Field) EzyBeanContextBuilder(com.tvd12.ezyfox.bean.EzyBeanContextBuilder) GraphQLDataFetcherManager(com.tvd12.ezyhttp.server.graphql.GraphQLDataFetcherManager) Map(java.util.Map) GraphQLConfiguration(com.tvd12.ezyhttp.server.graphql.GraphQLConfiguration) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.testng.annotations.Test)

Example 2 with GraphQLDataFetcherManager

use of com.tvd12.ezyhttp.server.graphql.GraphQLDataFetcherManager in project ezyhttp by youngmonkeys.

the class GraphQLControllerTest method testQueryWithNullVariableType.

@Test
public void testQueryWithNullVariableType() throws Exception {
    // given
    GraphQLSchemaParser schemaParser = new GraphQLSchemaParser();
    GraphQLDataFetcher fooDataFetcher = new GraphQLFooDataFetcher();
    GraphQLDataFetcherManager dataFetcherManager = GraphQLDataFetcherManager.builder().addDataFetcher(fooDataFetcher).build();
    ObjectMapper objectMapper = new ObjectMapper();
    GraphQLController controller = GraphQLController.builder().schemaParser(schemaParser).dataFetcherManager(dataFetcherManager).objectMapper(objectMapper).build();
    String fooQuery = "{foo}";
    // when
    Object fooResult1 = controller.doGet(fooQuery, "{\"value\": \"Bar\"}");
    Object fooResult2 = controller.doGet(fooQuery, null);
    // then
    Asserts.assertEquals(fooResult1.toString(), "{foo=Foo {value=Bar}}");
    Asserts.assertEquals(fooResult2.toString(), "{foo=Foo null}");
}
Also used : GraphQLSchemaParser(com.tvd12.ezyhttp.server.graphql.GraphQLSchemaParser) GraphQLDataFetcher(com.tvd12.ezyhttp.server.graphql.GraphQLDataFetcher) GraphQLDataFetcherManager(com.tvd12.ezyhttp.server.graphql.GraphQLDataFetcherManager) GraphQLController(com.tvd12.ezyhttp.server.graphql.controller.GraphQLController) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.testng.annotations.Test)

Example 3 with GraphQLDataFetcherManager

use of com.tvd12.ezyhttp.server.graphql.GraphQLDataFetcherManager in project ezyhttp by youngmonkeys.

the class GraphQLControllerTest method testQueryWithVariables.

@Test
public void testQueryWithVariables() throws Exception {
    // given
    GraphQLSchemaParser schemaParser = new GraphQLSchemaParser();
    GraphQLDataFetcher welcomeDataFetcher = new GraphQLWelcomeDataFetcher();
    GraphQLDataFetcherManager dataFetcherManager = GraphQLDataFetcherManager.builder().addDataFetcher(welcomeDataFetcher).build();
    ObjectMapper objectMapper = new ObjectMapper();
    GraphQLController controller = GraphQLController.builder().schemaParser(schemaParser).dataFetcherManager(dataFetcherManager).objectMapper(objectMapper).build();
    String welcomeQuery = "{welcome}";
    String variables = "{\"name\": \"Foo\"}";
    GraphQLRequest welcomeRequest = new GraphQLRequest();
    welcomeRequest.setQuery(welcomeQuery);
    welcomeRequest.setVariables(variables);
    // when
    Object welcomeResult1 = controller.doGet(welcomeQuery, variables);
    Object welcomeResult2 = controller.doPost(welcomeRequest);
    // then
    Asserts.assertEquals(welcomeResult1.toString(), "{welcome=Welcome Foo}");
    Asserts.assertEquals(welcomeResult2.toString(), "{welcome=Welcome Foo}");
}
Also used : GraphQLSchemaParser(com.tvd12.ezyhttp.server.graphql.GraphQLSchemaParser) GraphQLRequest(com.tvd12.ezyhttp.server.graphql.data.GraphQLRequest) GraphQLDataFetcher(com.tvd12.ezyhttp.server.graphql.GraphQLDataFetcher) GraphQLDataFetcherManager(com.tvd12.ezyhttp.server.graphql.GraphQLDataFetcherManager) GraphQLController(com.tvd12.ezyhttp.server.graphql.controller.GraphQLController) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.testng.annotations.Test)

Example 4 with GraphQLDataFetcherManager

use of com.tvd12.ezyhttp.server.graphql.GraphQLDataFetcherManager in project ezyhttp by youngmonkeys.

the class GraphQLControllerTest method testFetcherNotFoundException.

@Test
public void testFetcherNotFoundException() {
    // given
    GraphQLSchemaParser schemaParser = new GraphQLSchemaParser();
    GraphQLDataFetcherManager dataFetcherManager = GraphQLDataFetcherManager.builder().build();
    ObjectMapper objectMapper = new ObjectMapper();
    GraphQLController controller = GraphQLController.builder().schemaParser(schemaParser).dataFetcherManager(dataFetcherManager).objectMapper(objectMapper).build();
    String heroQuery = "{hero}";
    // when
    Throwable e = Asserts.assertThrows(() -> controller.doGet(heroQuery, null));
    // then
    Asserts.assertEquals(HttpNotFoundException.class.toString(), e.getClass().toString());
}
Also used : GraphQLSchemaParser(com.tvd12.ezyhttp.server.graphql.GraphQLSchemaParser) HttpNotFoundException(com.tvd12.ezyhttp.core.exception.HttpNotFoundException) GraphQLDataFetcherManager(com.tvd12.ezyhttp.server.graphql.GraphQLDataFetcherManager) GraphQLController(com.tvd12.ezyhttp.server.graphql.controller.GraphQLController) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.testng.annotations.Test)

Example 5 with GraphQLDataFetcherManager

use of com.tvd12.ezyhttp.server.graphql.GraphQLDataFetcherManager in project ezyhttp by youngmonkeys.

the class GraphQLControllerTest method testInvalidScheme.

@Test
public void testInvalidScheme() {
    // given
    GraphQLSchemaParser schemaParser = new GraphQLSchemaParser();
    GraphQLDataFetcher meDataFetcher = new GraphQLYouDataFetcher();
    GraphQLDataFetcherManager dataFetcherManager = GraphQLDataFetcherManager.builder().addDataFetcher(meDataFetcher).build();
    ObjectMapper objectMapper = new ObjectMapper();
    GraphQLController controller = GraphQLController.builder().schemaParser(schemaParser).dataFetcherManager(dataFetcherManager).objectMapper(objectMapper).build();
    GraphQLRequest youRequest = new GraphQLRequest();
    youRequest.setQuery("query{you{friends{name}}}}");
    // when
    Throwable e = Asserts.assertThrows(() -> controller.doPost(youRequest));
    // then
    Asserts.assertEquals(GraphQLInvalidSchemeException.class.toString(), e.getClass().toString());
}
Also used : GraphQLSchemaParser(com.tvd12.ezyhttp.server.graphql.GraphQLSchemaParser) GraphQLRequest(com.tvd12.ezyhttp.server.graphql.data.GraphQLRequest) GraphQLDataFetcher(com.tvd12.ezyhttp.server.graphql.GraphQLDataFetcher) GraphQLDataFetcherManager(com.tvd12.ezyhttp.server.graphql.GraphQLDataFetcherManager) GraphQLController(com.tvd12.ezyhttp.server.graphql.controller.GraphQLController) GraphQLInvalidSchemeException(com.tvd12.ezyhttp.server.graphql.exception.GraphQLInvalidSchemeException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.testng.annotations.Test)

Aggregations

GraphQLController (com.tvd12.ezyhttp.server.graphql.controller.GraphQLController)7 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)6 GraphQLDataFetcherManager (com.tvd12.ezyhttp.server.graphql.GraphQLDataFetcherManager)6 Test (org.testng.annotations.Test)6 GraphQLDataFetcher (com.tvd12.ezyhttp.server.graphql.GraphQLDataFetcher)5 GraphQLSchemaParser (com.tvd12.ezyhttp.server.graphql.GraphQLSchemaParser)5 GraphQLRequest (com.tvd12.ezyhttp.server.graphql.data.GraphQLRequest)3 EzyBeanContext (com.tvd12.ezyfox.bean.EzyBeanContext)1 EzyBeanContextBuilder (com.tvd12.ezyfox.bean.EzyBeanContextBuilder)1 EzySingletonFactory (com.tvd12.ezyfox.bean.EzySingletonFactory)1 HttpNotFoundException (com.tvd12.ezyhttp.core.exception.HttpNotFoundException)1 GraphQLConfiguration (com.tvd12.ezyhttp.server.graphql.GraphQLConfiguration)1 GraphQLInvalidSchemeException (com.tvd12.ezyhttp.server.graphql.exception.GraphQLInvalidSchemeException)1 Field (java.lang.reflect.Field)1 List (java.util.List)1 Map (java.util.Map)1