dynamic query graphql apollo with java

走远了吗. 提交于 2021-01-28 02:40:14

问题


The routine of working with the graphql apollo is that add file query .graphql

i would create dynamic query with java and i do not create file for each query

example querys :

one :

query EntryDetailQuery($repoFullName: String!) {
  entry(repoFullName: $repoFullName) {
    id
    repository {
      full_name
      description
      owner {
          login
      }
    }
    postedBy {
      login
    }
  }
}

two :

just request full_name

query EntryDetailQuery($repoFullName: String!) {
  entry(repoFullName: $repoFullName) {
    id
    repository {
      full_name

    }
  }
}

in fact, i would to get dynamic querys with Java

is it possible?


回答1:


It is possible to use dynamic queries with Apollo and Java. After building the corresponding java files using graphql queries, just provide the dynamic parameters in the query using setter and provide the query to the callback.

      EntryDetailQuery productListQuery = EntryDetailQuery.builder().repoFullName("test_repo").build();

      ApolloCall.Callback<EntryDetailQuery.Data> callback = new ApolloCall.Callback<EntryDetailQuery.Data>() {

        @Override
        public void onResponse(@Nonnull Response<EntryDetailQuery.Data> response) {
            logger.debug("Response from graphql:" + response);
        }

        @Override
        public void onFailure(@Nonnull ApolloException e) {
            logger.error("Error in getting response from graphql:" + e.getMessage());
        }
    };

    apolloGraphQlClient.getApolloClient().query(productListQuery).enqueue(callback);


来源:https://stackoverflow.com/questions/51608768/dynamic-query-graphql-apollo-with-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!