{"id":22191651,"url":"https://github.com/leosimoes/templatecode-java-spring-graphql","last_synced_at":"2026-05-09T00:38:57.066Z","repository":{"id":235220512,"uuid":"790319588","full_name":"leosimoes/TemplateCode-Java-Spring-GraphQL","owner":"leosimoes","description":"Template Code for a GraphQL in the Java language with Spring Framework and Gradle.","archived":false,"fork":false,"pushed_at":"2024-04-26T14:16:10.000Z","size":4294,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-30T01:16:37.191Z","etag":null,"topics":["graphql","java","spring"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/leosimoes.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2024-04-22T17:02:17.000Z","updated_at":"2024-05-06T00:36:11.000Z","dependencies_parsed_at":"2024-04-25T13:52:30.543Z","dependency_job_id":null,"html_url":"https://github.com/leosimoes/TemplateCode-Java-Spring-GraphQL","commit_stats":null,"previous_names":["leosimoes/templatecode-java-spring-graphql"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leosimoes%2FTemplateCode-Java-Spring-GraphQL","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leosimoes%2FTemplateCode-Java-Spring-GraphQL/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leosimoes%2FTemplateCode-Java-Spring-GraphQL/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leosimoes%2FTemplateCode-Java-Spring-GraphQL/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/leosimoes","download_url":"https://codeload.github.com/leosimoes/TemplateCode-Java-Spring-GraphQL/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245351757,"owners_count":20601088,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["graphql","java","spring"],"created_at":"2024-12-02T12:17:05.041Z","updated_at":"2026-05-09T00:38:57.038Z","avatar_url":"https://github.com/leosimoes.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# TemplateCode - Java - Spring - GraphQL\nAuthor: Leonardo Simões\n\nTemplate Code for a GraphQL in the Java language with Spring Framework and Gradle.\nThe APP has two resources: MainResource and AssociatedResource.\n\n![Image-13-A-UML-All](images/Image-13-A-UML-All.png)\n\nThe annotations used were:\n- `@Autowired` to inject a component's dependency;\n- `@Controller` to define the class that receives and responds to requests;\n- `@Service` for classes that implement business rules;\n- `@Repository` for interfaces that perform database queries;\n- `@QueryMapping` for controller methods that only perform and return a query;\n- `@MutationMapping` for controller methods that change (create, update or delete) data;\n- `@SchemaMapping` to perform queries, changes or define data fields that are objects;\n- `@Argument` to define a parameter of a controller operation;\n- `@Entity` to reference entity in the database;\n- `@Table(name=\"MAINS\")` to define the name of the table in the database;\n- `@Column` to indicate that an attribute is a database column;\n- `@ManyToOne` to indicate 1 to N relationship;\n- `@Data` (lombok) to create getters and setters;\n- `@Builder` (lombok) to create builder;\n- `@NoArgsConstructor` (lombok) to create constructor without arguments;\n- `@AllArgsConstructor` (lombok) to create constructor with all parameters as arguments.\n\n\n## Development steps\nThe project development steps were:\n\n1. Create project (in IntelliJ) with:\n- Java language (17);\n- Spring Framework (6.2.3);\n- Dependencies: Web, GraphQL, DevTools, Lombok, JPA and H2.\n\n![Image-01-IntelliJ](images/Image-01-IntelliJ.png)\n\n2. Change the start of the Run screen with custom text (or image):\n- format the desired text using `https://springhow.com/spring-boot-banner-generator/` and download `banner.txt`;\n\n![Image-02-BannerGenerator](images/Image-02-BannerGenerator.png)\n\n- place the `banner.txt` file in `src/main/resources` (or another path);\n- (optional) add `${spring.application.name}` and `Spring Boot ${spring-boot.formatted-version}` in `banner.txt`;\n- (optional) configure the `banner.txt` file path in `application.properties`:\n  * `spring.banner.location=classpath:/banner.txt`.\n\n3. Configure the H2 database:\n- In `build.gradle` configure according to the desired type of use:\n* `implementation 'com.h2database:h2'`;\n* `runtimeOnly 'com.h2database:h2'`;\n* `testImplementation 'com.h2database:h2'`;\n- In `application.properties`:\n\n```properties\n# ================================================================\n#                   APPLICATION\n# ================================================================\nspring.application.name=TemplateCode-Java-Spring-GraphQL\n# spring.banner.location=classpath:/banner.txt\n# ================================================================\n#                   DATASOURCE - H2 DATABASE\n# ================================================================\nspring.datasource.url=jdbc:h2:mem:proddb\nspring.datasource.driver-class-name=org.h2.Driver\nspring.datasource.username=leo\nspring.datasource.password=senha\nspring.h2.console.enabled=true\nspring.h2.console.path=/h2\n```\n\n![Image-03-Terminal-Run](images/Image-03-Terminal-Run.png)\n\n- test access to the H2 console at `http://localhost:8080/h2/`:\n\n![Image-04-B-ConsoleH2](images/Image-04-B-ConsoleH2.png)\n\nNote:\n- By default, the usename would be \"sa\" and the password \"\", and the h2 console would be disabled.\n\n4. Create AssociatedEntity class:\n- in the `entities` package;\n- with attributes UUID id, String name;\n- corresponding to the table named `ASSOCIATEDS`.\n\n5. Create MainEntity class:\n- in the `entities` package;\n- with UUID id, String name, String description and AssociatedEntity associated attributes;\n- corresponding to the table named `MAINS`.\n\n![Image-05-UML-Classes-Entities](images/Image-05-UML-Classes-Entities.png)\n\nNote: When creating classes that represent database entities:\n- annotate them with `@Entity`, `@Table(name=\"...\")` to map entity;\n- annotate them with `@Data`, `@Builder`, `@NoArgsConstructor`, `@AllArgsConstructor` to use Lombok;\n- add attribute `UUID id` annotated with `@Id` and `@GeneratedValue(strategy = GenerationType.UUID)`;\n- configure relationships with `@ManyToOne`, `@OneToMany` or `@OneToOne` in attributes that are objects;\n- configure the other columns with `@Column` and perhaps some validator like `@NotBlank` or `@NotNull`;\n- log into the H2 console and check if the tables were created correctly:\n\n![Image-06-ConsoleH2-Tables](images/Image-06-ConsoleH2-Tables.png)\n\n6. Create tables from SQL script (not from Hibernate):\n- run the application with `spring.jpa.hibernate.ddl-auto=create-drop` in `application.properties`;\n- execute the command `SCRIPT` or (`SCRIPT TO 'D:/schema.sql'`) to obtain SQL code for creating tables;\n- place the content of the previous step in `src/main/resources/schema.sql`;\n- run the application with `spring.jpa.hibernate.ddl-auto=none` and\n  `spring.jpa.defer-datasource-initialization=true` in `application.properties`;\n\n```properties\n# spring.jpa.hibernate.ddl-auto=create-drop\nspring.jpa.hibernate.ddl-auto=none\nspring.jpa.defer-datasource-initialization=true\nspring.sql.init.mode=always\n```\n\n- (optional) configure the `schema.sql` file path in `application.properties`:\n  * `spring.sql.init.schema-locations=classpath:/schema.sql`.\n\n![Image-07-ConsoleH2-BackupSQL](images/Image-07-ConsoleH2-BackupSQL.png)\n\n7. Create `AssociatedRepository` and `MainRepository` interfaces:\n- in the `repositories` package;\n- annotated with `@Repository`;\n- extends `JPARepository`;\n- `AssociatedRepository` has method declarations:\n  * `Boolean existsByNameEqualsIgnoreCase(String name);`\n  * `Optional\u003cMainEntity\u003e findByNameEqualsIgnoreCase(String name);`\n- `MainRepository` has method declarations:\n  * `Boolean existsByNameEqualsIgnoreCase(String name);`\n  * `Optional\u003cMainEntity\u003e findByNameEqualsIgnoreCase(String name);`\n  * `List\u003cMainEntity\u003e findByDescriptionIsLikeIgnoreCase(String description);`\n  * `List\u003cMainEntity\u003e findByAssociated_Name(String associated_name);`\n\n![Image-08-UML-Interfaces-Repositories](images/Image-08-UML-Interfaces-Repositories.png)\n\n8. Create dtos classes `AssociatedPayload` and `MainPayload`:\n- in the `dtos` package;\n- annotated with `@Data`, `@Builder`, `@NoArgsConstructor`, `@AllArgsConstructor` to use Lombok;\n\n![Image-09-UML-Classes-DTOs](images/Image-09-UML-Classes-DTOs.png)\n\n9. Create mappers between records (\"payloads\") and classes (\"entities\"):\n- add addons in `build.gradle`:\n\n```groovy\n// compileOnly 'org.projectlombok:lombok'\nimplementation 'org.projectlombok:lombok'\nimplementation 'org.mapstruct:mapstruct:1.5.5.Final'\nannotationProcessor 'org.mapstruct:mapstruct-processor:1.5.5.Final'\ntestAnnotationProcessor 'org.mapstruct:mapstruct-processor:1.5.5.Final'\nimplementation 'org.projectlombok:lombok-mapstruct-binding:0.2.0'\n```\n\n- create the `AssociatedMapper` and `MainMapper` interfaces:\n  * annotated with `@Mapper`;\n  * with `INSTANCE` attribute;\n  * with 4 methods for conversions between entity and payload, and between list of entities and list of payloads;\n  * `MainMapper` has two helper methods:\n    * `default AssociatedEntity toAssociated(MainPayload payload)`;\n    * `default String map(AssociatedEntity associatedEntity)`;\n\n![Image-10-UML-Interfaces-Mappers](images/Image-10-UML-Interfaces-Mappers.png)\n\n10. `In src/main/resources/graphql`, create `schema.graphqls` or `schema.gqls`:\n- `type Query` must contain all query names;\n- `type Mutation` must contain all creation, update and deletion operations;\n- `AssociatedInput` and `AssociatedOutput` correspond to the `AssociatedPayload` class;\n- `MainInput` and `MainOutput` correspond to the `MainPayload` class.\n- the names of operations and their parameters must be the same as those in the controller,\n  or must be explicitly referenced;\n- the names of data types do not need to be the same as the classes they reference.\n\n```graphql\ntype MainOutput {\n    name: String!\n    description: String\n    associated: AssociatedOutput\n}\n\ntype AssociatedOutput {\n    name: String!\n}\n\ninput MainInput {\n    name: String!\n    description: String\n    associated: AssociatedInput\n}\n\ninput AssociatedInput {\n    name: String!\n}\n\ntype Query {\n    getAllMain: [MainOutput!]!\n    getMainbyName(name: String!): MainOutput\n    getAllAssociated: [AssociatedOutput!]!\n    getAssociatedbyName(name: String!): AssociatedOutput\n}\n\ntype Mutation {\n    createMain(mainInput: MainInput!): MainOutput!\n    updateMain(name: String!, mainInput: MainInput!): MainOutput\n    deleteMain(name: String!): Boolean\n    createAssociated(associatedInput: AssociatedInput!): AssociatedOutput!\n    updateAssociated(name: String!, associatedInput: AssociatedInput!): AssociatedOutput\n    deleteAssociated(name: String!): Boolean\n}\n```\n\n11. Create service layer:\n- create `services` package;\n- add `AssociatedService` and `MainService` interfaces;\n- add classes `AssociatedServiceImp` and `MainServiceImpl`:\n  * annotated with `@Service`;\n  * have an attribute for the repository and another for the mapper;\n  * have a constructor with one parameter, but which initializes both attributes;\n  * implement the interfaces.\n- the methods `create`, `getByName`, `getAll`, `updateByName`, `deleteByName` are in these classes and interfaces.\n\n![Image-11-UML-Services](images/Image-11-UML-Services.png)\n\n12. Create controller layer:\n- create `controllers` package;\n- add `MainController` and `AssociatedController` classes:\n  * annotated with `@Controller`;\n  * have an attribute for service;\n  * have a constructor annotated with `@Autowired` and with service as a parameter;\n  * its methods:\n    * are annotated with `@QueryMapping` or `@MutationMapping`;\n    * has names corresponding to those in `schema.graphqls`;\n    * its parameters are annotated with `@Argument`;\n    * has parameter names corresponding to those in `schema.graphqls`;\n    * input and output objects are from classes in the `dtos` package.\n\n![Image-12-UML-Controllers](images/Image-12-UML-Controllers.png)\n\n\n## References\nSpring - Guides - Building a GraphQL service:\nhttps://spring.io/guides/gs/graphql-server\n\nSpring - Docs - Spring for GraphQL:\nhttps://docs.spring.io/spring-graphql/reference/index.html\n\nBaeldung - Introduction to GraphQL:\nhttps://www.baeldung.com/graphql\n\nBaeldung - Getting Started with GraphQL and Spring Boot:\nhttps://www.baeldung.com/spring-graphql\n\nGraphQL Java - Tutorial - Getting started with Spring for GraphQL:\nhttps://www.graphql-java.com/tutorials/getting-started-with-spring-boot/\n\nGraphQL.org - Learn:\nhttps://graphql.org/learn/\n\nAuth0 - Blog - How to Build a GraphQL API with Spring Boot:\nhttps://auth0.com/blog/how-to-build-a-graphql-api-with-spring-boot/","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleosimoes%2Ftemplatecode-java-spring-graphql","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fleosimoes%2Ftemplatecode-java-spring-graphql","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleosimoes%2Ftemplatecode-java-spring-graphql/lists"}