AEM Graphql Java | Community
Skip to main content
Prince_Shivhare
Community Advisor
Community Advisor
April 30, 2023
Solved

AEM Graphql Java

  • April 30, 2023
  • 4 replies
  • 4016 views

Hi Community,

 

I am trying to create a graphQl endpoint using the https://www.graphql-java.com/documentation/getting-started/ .

 

So I'll be writing this sample code in Servlet and trying to print the same response {hello=world} using the Servlet endpoint.

But when I am adding the dependency, the bundle is never active when I run the core maven build and always fails during the full build.

Could you help me with what I am missing here?

Thank you,

Prince

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Prince_Shivhare

Strangely, I don't have any compile issues.
Could you help me with what changes you made in core pom for the BND plugin?

4 replies

Sachin_Arora_
Community Advisor
Community Advisor
May 1, 2023

@prince_shivhare Can you please share build error and list of packages not getting resolved in bundle. 

Please confirm you are trying to add below dependency in code.

<dependency> <groupId>com.graphql-java</groupId> <artifactId>graphql-java</artifactId> <version>20.2</version> </dependency>
Prince_Shivhare
Community Advisor
Community Advisor
May 1, 2023

It's an error of some dependent jars in a graphql-java.
If you'll try to setup the same and use the example Java code provided in the link. you'll see the error during the build.

Sachin_Arora_
Community Advisor
Community Advisor
May 1, 2023

@prince_shivhare I got compilation error and after changing below code it fixed the issue and build failure also did not come. 

RuntimeWiring runtimeWiring = RuntimeWiring.newRuntimeWiring() .type("Query", builder -> builder.dataFetcher("hello", new StaticDataFetcher("world"))) .build();

 For bundle to be active, you need to embed the graphql jar in BND plugin to resolve import issues.

Prince_Shivhare
Community Advisor
Prince_ShivhareCommunity AdvisorAuthorAccepted solution
Community Advisor
May 1, 2023

Strangely, I don't have any compile issues.
Could you help me with what changes you made in core pom for the BND plugin?

Level 2
July 19, 2023

Hi @prince_shivhare 

I'm also facing the same issue. Could you please provide me the steps if you already fix it?

 

Thank you!

Prince_Shivhare
Community Advisor
Community Advisor
July 20, 2023

@lakshmi99 ,

 

Could you please help me with your usecase?

Level 2
July 20, 2023

Hi @prince_shivhare 

Use case: Expose content fragments data using servlet through graphql. 

Trying sample code in the link above but getting many dependency issues fir the below. Tried to resolve but no luck.

import static graphql.schema.idl.RuntimeWiring.newRuntimeWiring;
import graphql.ExecutionResult;
import graphql.GraphQL;
import graphql.schema.GraphQLSchema;
import graphql.schema.StaticDataFetcher;
import graphql.schema.idl.RuntimeWiring;
import graphql.schema.idl.SchemaGenerator;
import graphql.schema.idl.SchemaParser;
import graphql.schema.idl.TypeDefinitionRegistry;

public class GQLService {

public static void main(String[] args) {
String schema = "type Query{hello: String}";
SchemaParser schemaParser = new SchemaParser();
TypeDefinitionRegistry typeDefinitionRegistry = schemaParser.parse(schema);
RuntimeWiring runtimeWiring = newRuntimeWiring()
.type("Query", builder -> builder.dataFetcher("hello", new StaticDataFetcher("world")))
.build();
SchemaGenerator schemaGenerator = new SchemaGenerator();
GraphQLSchema graphQLSchema = schemaGenerator.makeExecutableSchema(typeDefinitionRegistry, runtimeWiring);
GraphQL build = GraphQL.newGraphQL(graphQLSchema).build();
ExecutionResult executionResult = build.execute("{hello}");
System.out.println(executionResult.getData().toString());
// Prints: {hello=world}
}
}

 

 

Thank you

sarav_prakash
Community Advisor
Community Advisor
February 10, 2025

@prince_shivhare , referring to your question, I wrote an article explaining different ways to fetch graphql response https://medium.com/@bsaravanaprakash/aem-graphql-fa%C3%A7ade-patterns-building-wrappers-apis-around-aem-graphql-92f36f5a79d8

 

You may read at your leisure.