【问题标题】:how to add endpoint to graphql client?如何将端点添加到graphql客户端?
【发布时间】:2021-05-04 14:10:38
【问题描述】:

我想创建一个客户端来使用 GraphQL 端点。

我试过GraphQL Java提供的示例代码,看起来 像这样:

GraphQLObjectType fooType = newObject()
    .name("Foo")
    .field(newFieldDefinition()
        .name("bar")
        .type(GraphQLString))
    .build();

GraphQLSchema schema = GraphQLSchema.newSchema()
    .query(fooType)
    .build();
GraphQL graphQL = GraphQL.newGraphQL(schema)
    .build();

ExecutionInput executionInput = ExecutionInput.newExecutionInput()
    .query("query { hero { name } }")
    .build();

ExecutionResult executionResult = graphQL.execute(executionInput);

Object data = executionResult.getData();
List<GraphQLError> errors = executionResult.getErrors();

我不知道这是否是最好的方法。我使用 AWS AppSync 作为 GraphQL 服务器。如何更新我的代码,以便它引用 AWS 端点?

【问题讨论】:

    标签: java graphql aws-appsync


    【解决方案1】:

    我建议使用 Apollo 客户端。请看这个Getting Started guide

    为了将 Apollo 客户端与 AWS AppSync 一起使用,您需要做两件事:

    1. 提供端点的 URL;
    2. 向您的请求添加授权/API 密钥标头。

    例子:

    String url = "https://xxx.appsync-api.us-east-1.amazonaws.com/graphql";
    OkHttpClient okhttp = new OkHttpClient.Builder()
        .addInterceptor(chain -> {
            Request requestWithAuth = chain.request()
                .newBuilder()
                .addHeader("X-API-Key", "da2-alphanum-string-here")
                .build();
            chain.proceed(requestWithAuth);
        })
        .build();
    ApolloClient apollo = ApolloClient.builder()
        .serverUrl(url)
        .okHttpClient(okhttp)
        .build();
    

    【讨论】:

      猜你喜欢
      • 2016-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-07
      • 1970-01-01
      • 2013-09-24
      • 2021-02-10
      • 1970-01-01
      相关资源
      最近更新 更多