1 Kasım 2021 Pazartesi

SpringGraphQL

Gradle
İki proje var. 
1. Şöyle yaparız
implementation("org.springframework.boot:spring-boot-starter-graphql")
2. Şöyle yaparız. Kullanmayın
implementation("com.graphql-java-kickstart:graphql-spring-boot-starter:14.0.0")
SpringGraphQL anotasyonlara dayalı. Açıklaması şöyle
So instead of implementing GraphQLQueryResolver/GraphQLMutationResolver, we use @QueryMapping and @MutationMapping alongside with @Argument to resolve the method arguments. Also there is @SchemaMapping (@QueryMapping/@MutationMapping’s parent) which allows a method to act as the DataFetcher for a field from the schema mapping.  
@MutationMapping Anotasyonu
Örnek
Şöyle yaparız
@RestController
public class StudentController {

  private Map<String, Student> map = new HashMap<>();
  private AtomicInteger id = new AtomicInteger();

  @QueryMapping
  public Student getStudentById(@Argument String id) {
    return map.get(id);
  }

  @MutationMapping
  public Student addStudent(@Argument String name) {
    int id = this.id.incrementAndGet();
    Student student = new Student(String.valueOf(id), name);
    map.put(String.valueOf(id), student);
    return student;
  }

  @QueryMapping
  public List<Student> getStudents(){
    return new ArrayList<>(map.values());
  }
}
@QueryMapping Anotasyonu
Şu satırı dahil ederiz
import org.springframework.graphql.data.method.annotation.QueryMapping;
Örnek
Şöyle yaparız
@Controller
public class GreetingController {
  @QueryMapping //1
  public String hello() { //2
    return "Hello, world!";
  }
}
Açıklaması şöyle
1. Binds this method to a query, i.e. a field under the Query type
2. Determine the query from the method name if not declared on the annotation
DataFetcherExceptionResolver Arayüzü
Exception handling için gerekir. Açıklaması şöyle
DataFetcherExceptionResolver is an asynchronous contract. For most implementations, it would be sufficient to extend DataFetcherExceptionResolverAdapter and override one of its resolveToSingleError or resolveToMultipleErrors methods that resolve exceptions synchronously.

QuerydslPredicateExecutor Arayüzü
Şu satırı dahil ederiz
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
Örnek
Şöyle yaparız
public interface AccountRepository extends Repository<Account,Long>,
  QuerydslPredicateExecutor<Account> {
}
Kullanmak için şöyle yaparız
//For single result queries
DataFetcher<Account> dataFetcher = 
  QuerydslDataFetcher.builder(repository).single();

//For multi-result queries
DataFetcher<Iterable<Account>> dataFetcher = 
  QuerydslDataFetcher.builder(repository).many();

Hiç yorum yok:

Yorum Gönder