英文:
how to create fragments using Annotations of spring-spqr graphql-spqr-spring-boot-starter graphQL
问题
使用 graphql-spqr-spring-boot-starter 和 graphql-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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论