【问题标题】:Can a graphql mutation be wired inside a DataFetcher class along with query?可以将 graphql 突变与查询一起连接到 DataFetcher 类中吗?
【发布时间】:2018-12-03 08:51:52
【问题描述】:

错误:FieldUndefined:'Query' 类型中的字段 'createUser' 未定义 @'createUser'"

@Service
public class GraphQlService {

    @Value("classpath:schema.graphql")
    Resource resource;

    private GraphQL graphQL;


    @Autowired
    UserDataFetcher userFetcher;
    @Autowired
    PostDataFetcher postFetcher;


    @PostConstruct
    public void loadSchema() throws IOException {

        File schemaFile = resource.getFile();
        TypeDefinitionRegistry typeRegistry = new SchemaParser().parse(schemaFile);
        RuntimeWiring wiring = buildRuntimeWiring();
        GraphQLSchema schema = new SchemaGenerator().makeExecutableSchema(typeRegistry, wiring);
        graphQL = GraphQL.newGraphQL(schema).build();


    }

    private RuntimeWiring buildRuntimeWiring() {
        return RuntimeWiring.newRuntimeWiring()
                .type("Query", typeWiring -> typeWiring
                        .dataFetcher("user", userFetcher)
                        .dataFetcher("post", postFetcher))
                .type("Mutation", typeWiring -> typeWiring
                        .dataFetcher("createUser", userFetcher))
                .build();
    }

    public GraphQL getGraphQL() {
        return graphQL;
    }

}

1.我不能对查询和变异都使用通用的数据提取器/reslover 正如我在下面的一个班级中所做的那样。它无法找到 创建用户?

@Component
public class UserDataFetcher implements DataFetcher<List<User>> {
@Autowired
UserRepository userRepository;

public User createUser(DataFetchingEnvironment environment) {
    String username = environment.getArgument("username");
    String location= environment.getArgument("location");
    User[] follower = environment.getArgument("followers");
    User[] following = environment.getArgument("following");
    Post[] pos = environment.getArgument("posts");

    User user = new User();
    user.setUsername(username);
    user.setFollowers(follower);
    user.setFollowing(following);
    user.setLocation(location);
    user.setPosts(pos);
    return userRepository.save(user);
}


@Override
public List<User> get(DataFetchingEnvironment environment) {
    return userRepository.findAll();
}
}

//下面的 SDL 用于架构

schema {
    query: Query
    mutation: Mutation
}

type User{
    id:ID!
    username:String!
    followers:[User]
    following:[User]
    location:String!
    posts:[Posts]
}

type Post{
    id: ID
    title: String!
    author: User!

}



type Query{
    user: [User]
    post: [Post]
}

type Mutation{
    createUser(username: String!, location: String!,followers: [User],following:[User],posts:[Post]):User

}

2。架构是否正确,因为它会说 User 和 Post 没有作为 InputType 被提及。我尝试了 User 和 Post 的 InputType 但是 无法让它工作。如何正确存储模式 关注者和关注者看起来像?

【问题讨论】:

    标签: graphql graphql-java


    【解决方案1】:

    1) 不,每个DataFetcher 实现只能执行一个操作——这就是接口指定的get 方法。框架如何知道调用createUser 方法?

    2) 不,架构无效。您不能使用User 作为参数(输入)类型。您需要定义一个单独的输入类型,例如

    input UserInput {
       username:String!
       followers:[ID!]
       following:[ID!]
       location:String!
    }
    

    试想一下,如果您可以输入User,您将如何提供强制ID?如果User 实现了一个接口,因为没有为输入定义接口怎么办?想象一下如果User 有一个递归字段类型。在输出中,这很好(因为您可以直接控制嵌套级别),但对于输入类型来说就不可能满足。

    输入和输出的规则有很大的不同,所以需要单独定义类型。

    此外,在此示例中,只有 IDs 是可选的关注者或被关注者,而不是完整的对象,因为它通常更有意义。

    【讨论】:

      猜你喜欢
      • 2018-02-15
      • 2019-11-18
      • 1970-01-01
      • 2021-04-08
      • 2019-08-06
      • 2017-08-24
      • 2018-02-04
      • 2022-06-22
      • 2019-06-18
      相关资源
      最近更新 更多