Skip to content

NikitaShchienko/spring-graphql-app

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Spring GraphQL

Spring boot application with GraphQL

Query

Simple query:

query getAuthors {
    authors {
        id
        lastName
        firstName
    }
}

query getBooks {
    books {
        name
    }
}

Query with arguments:

query getBook {
  book(id: 1) {
    id
    name
  }
}

Variables

Query with variables:

query getBooks($filter: String = "Кап") {
  books(Filter: $filter) {
    id
    name
  }
}

# Variable
{
  "filter": "У"
}

Alias

Query with alias:

query getBook {
  firstBook: book(id: 1) {
    id
    name
  }
  secondBook: book(id: 2) {
    id
    name
  }
}

Mutation

Example:

mutation delete {
   deleteAuthor(id: 1)
 }

 mutation create {
   createAuthor(
     lastName: "Last name"
     firstName: "First name") {
      id
      firstName
   }
 }

mutaion update {
	updateAuthor(
		id: 1
		lastName: "Last name") {
		  id
          firstName
		  lastName
	}
}

Fragment

Query with fragment:

fragment nameFields on Author {
	firstName
	lastName
}

query getAllAuthors {
  authors {
    id
    ...nameFields
  }
}

Input type

Mutation with AuthorInput type:

mutation addAuthor {
  addAuthor(author: {
    lastName: "Test 1"
    firstName: "Test 2"
  }) {
    id
    lastName
    firstName
  }
}

Directive

Directive @include

query getAuthors($withBooks: Boolean = false) {
  authors {
    id
    lastName
    firstName
    books @include(if: $withBooks) {
      name
    }
  }
}

# Variables
{
  "withBooks": true
}

Directive @skip

query getAuthors($withoutBooks: Boolean = true) {
  authors {
    id
    lastName
    firstName
    books @skip(if: $withoutBooks) {
      name
    }
  }
}

# Variables
{
  "$withoutBooks": false
}

SPQR

GraphQL SPQR (GraphQL Schema Publisher & Query Resolver, pronounced like speaker) is a simple-to-use library for rapid development of GraphQL APIs in Java.

Link: https://github.com/leangen/graphql-spqr-spring-boot-starter

Annotation

@GraphQLApi

Example:

@Component("graphql_BookGraphQLApi")
@GraphQLApi
public class BookGraphQLApi {
...
}

@GraphQLEnumValue

Example:

@GraphQLType(name = "BookType", description = "Book type")
public enum BookType {

    @GraphQLEnumValue(name = "Novel", description = "Novel") NOVEL
}

@GraphQLArgument

Example:

    @GraphQLQuery(name = "book")
    public Book getBook(@GraphQLArgument(name = "id", description = "Book ID") Integer id) {
        return bookRepository.getById(id);
    }

@GraphQLMutation

    @GraphQLMutation(name = "createAuthor")
    public Author createAuthor(@GraphQLArgument(name = "firstName", description = "First name") String firstName,
                               @GraphQLArgument(name = "lastName", description = "Last name") String lastName) {
        Author author = new Author(firstName, lastName);
        return authorRepository.save(author);
    }

@GraphQLNonNull

    @GraphQLMutation(name = "updateBook")
    public Book updateBook(@GraphQLNonNull @GraphQLArgument(name = "id", description = "Book ID") Integer id,
                           @GraphQLNonNull @GraphQLArgument(name = "name", description = "Book name") String name) {
        Book book = bookRepository.findById(id).orElseThrow(() -> new GraphQLException("Book not found"));
        book.setName(name);

        subscribers.get(id).forEach(subscriber -> subscriber.next(book));

        return bookRepository.save(book);
    }

Links

GraphQL Java: https://www.graphql-java.com/documentation/master/

About

Spring boot application with GraphQL

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages