DevTUPLE

Basic graphql project with springboot

Graphql is a query language for API. Graphql structures the data in the form of a graph. Hence it provides flexible data fetching.

Components in a Graphql project with springboot:

  1. Create a maven project.
  2. The maven dependency for Graphql framework can be given as below, further add dependency for springboot and mysql jdbc.
<dependency>
	<groupId>com.graphql-java</groupId>
	<artifactId>graphql-spring-boot-starter</artifactId>
	<version>5.0.2</version>
</dependency>

<dependency>
	<groupId>com.graphql-java</groupId>
	<artifactId>graphql-java-tools</artifactId>
	<version>5.2.4</version>
</dependency>
  1. The graphql schema will have Query and Mutation type which the corresponding java class.

The Query class should implement GraphQLQueryResolver and the Mutation class should implement GraphQLMutationResolver. These classes should be a component of springboot application.

  1. The representation for Author table that has id, name and age in the graphql can be given as
type Author {
	id: ID!
	name: String!
	age: Int
}

As we can see the ID, String, Int are the DataTypes of Graphql, further Author can also be used as a dataType. Here exclamatory mark represents Not Null.

  1. Query type in graphql is used for querying. The query for finding all authors will be
type Query {
	findAllAuthors: [Author]!
}

the corresponding Java method for this query which should be inside the Query class is

public Iterable<Author> findAllAuthors() {
		return authorRepository.findAll();
	}

Here the JPA repository of author entity is used as authorRepository.

  1. Mutation type is used for creating and changing the data.The mutation schema for author will be
type Mutation {
	createAuthor(name: String!, age: Int): Author!
}

the corresponding Java method for this mutation which should be inside the Mutation class is

public Author createAuthor(String name, Integer age) {
		Author author = new Author();
		author.setName(name);
		author.setAge(age);
		authorRepository.save(author);
		return author;
	}

Outputs:

  1. Graphql mutation in postman as a post request will be
mutation {
   createAuthor(name:"Aravind",age:25){
       id
       name
       age
    }
}

and the result is

{
    "data": {
        "createAuthor": {
            "id": "1",
            "name": "Aravind",
            "age": 25
        }
    }
}
  1. Graphql query in postman as a post method will be
 query{
   findAllAuthors{
       id
       name
       age
    }
}

and the result is

{
    "data": {
        "findAllAuthors": [
            {
                "id": "1",
                "name": "Aravind",
                "age": 25
            }
        ]
    }
}

as the table has only one author it returns the details of that author.

Query and Mutation class

Query class

@Component
public class Query implements GraphQLQueryResolver {
	private AuthorRepository authorRepository;
	@Autowired
	public Query(AuthorRepository authorRepository) {
		this.authorRepository = authorRepository;
	}

	public Iterable<Author> findAllAuthors() {
		return authorRepository.findAll();
	}

}

Mutation class

@Component
public class Mutation implements GraphQLMutationResolver {
	private AuthorRepository authorRepository;
	@Autowired
	public Mutation(AuthorRepository authorRepository) {
		this.authorRepository = authorRepository;
	}

	public Author createAuthor(String name, Integer age) {
		Author author = new Author();
		author.setName(name);
		author.setAge(age);
		authorRepository.save(author);
		return author;
	}
}

Written by Aravind P C

Do you want to know when I post something new?
Then subscribe to my newsletter.