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

634 Views Asked by At

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

There are 1 best solutions below

1
On

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.