为什么在这个 GraphQL 设置中没有调用 DataFetcher?

2022-09-03 15:52:49

我想写一段代码,它将处理 GraphQL 查询,如下所示:

  query {
      group(id: "com.graphql-java")
      name(name: "graphql-java")
      version(id: "2.3.0")
  }

我创建了一个数据提取器,并在方法中放置了一个断点:get

  import graphql.schema.DataFetcher;
  import graphql.schema.DataFetchingEnvironment;

  public class TestDataFetcher implements DataFetcher {
      public Object get(final DataFetchingEnvironment dataFetchingEnvironment) {
          return null;
      }
  }

然后我写了下面的代码:

  public class Example02 {
      public static void main(final String[] args) throws IOException {
          final Example02 app = new Example02();
          app.run();
      }
      void run() throws IOException {
          final TestDataFetcher testDataFetcher = new TestDataFetcher();

          final List<GraphQLFieldDefinition> fields = Lists.newArrayList(
                  createGroupField(testDataFetcher),
                  createNameField(),
                  createVersionField());

          final GraphQLObjectType queryType = newObject()
                  .name("query")
                  .fields(fields)
                  .build();
          final GraphQLSchema schema = GraphQLSchema.newSchema()
                  .query(queryType)
                  .build();
          final String query = FileUtils.readFileToString(
                  new File("src/main/resources/query1.txt"),
                  "UTF-8"
          );
          final Map<String, Object> result = (Map<String, Object>) new GraphQL(schema).execute(query).getData();
          System.out.println(result);
      }

      private GraphQLFieldDefinition createVersionField() {
          return newFieldDefinition().type(GraphQLString).name("version").build();
      }

      private GraphQLFieldDefinition createNameField() {
          return newFieldDefinition().type(GraphQLString).name("name").build();
      }

      private GraphQLFieldDefinition createGroupField(TestDataFetcher testDataFetcher) {
          final GraphQLArgument idArg = newArgument().name("id").type(GraphQLString).build();
          return newFieldDefinition()
                  .type(GraphQLString)
                  .name("group")
                  .dataFetcher(testDataFetcher)
                  .argument(idArg)
                  .build();
      }
  }

当我在调试模式下运行该方法时,断点未激活。main

为什么?我该如何修复它?


答案 1

下面是您的工作示例。我已经使用了你的查询文件,因为你发布它。根据需要进一步实现您的数据取件器。基本上,您应该为名称和版本字段定义参数。调试器在运行时会告诉一切,它具有包含所有问题的数组。new GraphQL(schema).execute(query)errors

import graphql.GraphQL;
import graphql.schema.*;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static graphql.Scalars.GraphQLString;
import static graphql.schema.GraphQLArgument.newArgument;
import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition;
import static graphql.schema.GraphQLObjectType.newObject;

public class Example2 {


    public class TestDataFetcher implements DataFetcher {
        public Object get(DataFetchingEnvironment environment) {
            String id = (String)environment.getArgument("id");
            return id;
        }
    }

    public static void main(final String[] args)  {
        Example2 app = new Example2();
        app.run();
    }
    void run() {
         TestDataFetcher testDataFetcher = new TestDataFetcher();   

         List<GraphQLFieldDefinition> fields = new ArrayList<GraphQLFieldDefinition>();

            fields.add(createGroupField(testDataFetcher));
                fields.add(createNameField());
                fields.add(createVersionField());

         GraphQLObjectType queryType = newObject()
                .name("query")
                .fields(fields)
                .build();

         GraphQLSchema schema = GraphQLSchema.newSchema()
                .query(queryType)
                .build();
        String query = null;
        try {
              query = FileUtils.readFileToString(
                    new File("src/main/resources/query1.txt"),
                    "UTF-8"
            );
        }catch(IOException ioe){
            ioe.printStackTrace();
        }

        if(query!=null) {
            Map<String, Object> result = (Map<String, Object>) new GraphQL(schema).execute(query).getData();
            System.out.println(result);
        }
    }

    private GraphQLFieldDefinition createVersionField() {
         GraphQLArgument arg = newArgument().name("id").type(GraphQLString).build();
        return newFieldDefinition().type(GraphQLString).name("version").argument(arg).build();
    }

    private GraphQLFieldDefinition createNameField() {
        GraphQLArgument arg = newArgument().name("name").type(GraphQLString).build();
        return newFieldDefinition().type(GraphQLString).name("name").argument(arg).build();
    }

    private GraphQLFieldDefinition createGroupField(TestDataFetcher testDataFetcher) {
        final GraphQLArgument idArg = newArgument().name("id").type(GraphQLString).build();
        return newFieldDefinition()
                .type(GraphQLString)
                .name("group")
                .dataFetcher(testDataFetcher)
                .argument(idArg)
                .build();
    }
}

答案 2

您的问题是您的查询。如果调试变量,则为 。问题是查询中的“\n”。queryquery {\n group(id: "com.graphql-java")\n name(name: "graphql-java")\n version(id: "2.3.0")\n}

如果将查询更改为断点,则将执行断点。query{group(id: "com.graphql-java")}

为了执行您的查询,我必须首先更新 GraphQlFiledDefinitions 以接收参数。

private GraphQLFieldDefinition createVersionField(TestDataFetcher testDataFetcher) {
    final GraphQLArgument idArg = newArgument().name("id").type(GraphQLString).build();
    return newFieldDefinition().type(GraphQLString).name("version").staticValue("id value").argument(idArg).build();
}

private GraphQLFieldDefinition createNameField(TestDataFetcher testDataFetcher) {
    final GraphQLArgument nameArg = newArgument().name("name").type(GraphQLString).build();
    return newFieldDefinition().type(GraphQLString).name("name").staticValue("name Value").argument(nameArg).build();
}

private GraphQLFieldDefinition createGroupField(TestDataFetcher testDataFetcher) {
    final GraphQLArgument idArg = newArgument().name("id").type(GraphQLString).build();
    return newFieldDefinition()
            .type(GraphQLString)
            .name("group")
            .dataFetcher(testDataFetcher)
            .argument(idArg)
            .build();
}

然后我可以使用没有换行符的查询query {group(id: "com.graphql-java"),name(name:"graphql-java"),version(id: "2.3.0")}


推荐