如何使用Spring Spqr GraphQL Spqr Spring Boot Starter的注释创建片段GraphQL。

huangapple go评论78阅读模式
英文:

how to create fragments using Annotations of spring-spqr graphql-spqr-spring-boot-starter graphQL

问题

使用 graphql-spqr-spring-boot-startergraphql-spqr,但无法使用 @GraphQLDirective 创建片段(Fragment),不确定是否有任何方法可以实现。

我的意图是通过代码创建片段,如下所示:

@Data
@GraphQLFragment
public class ProfileFields {
    private String name;
    private String emailId;
    private String phoneNo;
}

并在以下查询中使用此片段,有人可以指导我使用哪些注解:

{
  profile(id: "101") {
    ...ProfileFields
  }
}
英文:

using graphql-spqr-spring-boot-starter and graphql-spqr but not able to create Fragment using @GraphQLDirective, not sure if there is anyway to do it.

my intension is to create Fragment through code like

@Data
@GraphQLFragment
public class ProfileFields{
 private String name;
 private String emailId;
 private String phoneNo;
}

and use this fragment in the query below, can someone guide me what are the annotations used for this

{
  profile(id: "101"){
    ...ProfileFields
  }
}

答案1

得分: 1

这不是 GraphQL 的工作方式。片段是由客户端随意定义的。你不能在服务器上预先定义它们。片段的定义是查询的一部分。对于片段的工作,服务器上无需做任何事情(也无法做任何事情)。

客户端可以发送如下查询:

{
  profile(id: "101") {
    ... ProfileFields
  }
}

fragment ProfileFields on Profile {
  name
  registrationDate
}

至于 @GraphQLDirective,它用于定义模式(服务器端)指令。指令与片段无关。

英文:

That's not how GraphQL works. Fragments are defined by the client ad-hoc. You can not define them ahead of time on the server. The definition of the fragment is a part of the query. There's nothing you need to (or can) do on the server for the fragments to work.

The client could send a query such as:

{
  profile(id: "101") {
    ... ProfileFields
  }
}

fragment ProfileFields on Profile {
  name
  registrationDate
}

As for @GraphQLDirective, it is used to define schema (server-side) directives. Directives are not related to fragments.

huangapple
  • 本文由 发表于 2020年5月4日 23:53:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/61596326.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定