I have created a custom SlingDataFetcher resolver and I want to use the cq:Name (node name) in my GraphQL query:
import org.apache.sling.graphql.api.SlingDataFetcher;
import org.apache.sling.graphql.api.SlingDataFetcherEnvironment;
import org.osgi.service.component.annotations.Component;
@Component(service = SlingDataFetcher.class, property = {"name=nodeName"})
public class GraphQLResolver implements SlingDataFetcher<String> {
@Override
public String get(SlingDataFetcherEnvironment env) {
return env.getCurrentResource() != null ? env.getCurrentResource().getName() : null;
}
}
I would like to use this nodeName field and be able to query it from the GraphQL Editor or in a persisted query (located at /conf/project/settings/graphql/persistentQueries).
However, I am unable to access nodeName in my GraphQL queries..
Solved! Go to Solution.
Views
Replies
Total Likes
Hi @Arthiselva ,
No, you cannot expose a new GraphQL field without touching the schema. In AEM GraphQL, the schema is auto-generated from Content Fragment Models. If the field is not in the model, GraphQL won’t know about it, even if you have a custom SlingDataFetcher.
Your custom SlingDataFetcher works only if the schema already has a field mapped to it. That mapping comes from either:
Adding a field in the Content Fragment Model and assigning your data fetcher, or
Using a custom schema extension (SDL file) where you declare the field and wire it to your fetcher.
You must define the field in the schema (via CF model or SDL extension).
There is no config in AEM 6.5 AMS that will auto-expose nodeName without schema changes.
I am using Adobe Managed services and we are yet to be on the cloud services.
hi @Arthiselva,
Your SlingDataFetcher implementation and registration are the right starting point, but you also need to declare or extend the GraphQL schema to include a new field (nodeName) in the relevant GraphQL type exposed by Content Fragments.
Please take a look here and here.
Hi @Arthiselva ,
No, you cannot expose a new GraphQL field without touching the schema. In AEM GraphQL, the schema is auto-generated from Content Fragment Models. If the field is not in the model, GraphQL won’t know about it, even if you have a custom SlingDataFetcher.
Your custom SlingDataFetcher works only if the schema already has a field mapped to it. That mapping comes from either:
Adding a field in the Content Fragment Model and assigning your data fetcher, or
Using a custom schema extension (SDL file) where you declare the field and wire it to your fetcher.
You must define the field in the schema (via CF model or SDL extension).
There is no config in AEM 6.5 AMS that will auto-expose nodeName without schema changes.
Facing an issue where a custom GraphQL schema is not being registered with the corresponding Sling Data Fetcher in AEM. The schema fields are not appearing in the GraphiQL editor (http://localhost:4502/aem/graphiql.html), and it seems the core issue is that the schema is not linked to the Sling Data Fetcher.
Schema File: The schema file is located at /conf/<project>/settings/graphql/example-schema.gql and contains the following definition:
type Query {
exampleData: String @fetcher(name: "exampleDataFetcher")
}
Sling Data Fetcher Implementation:
The ExampleDataFetcher class is implemented as follows:
import org.apache.sling.graphql.api.SlingDataFetcher;
import org.apache.sling.graphql.api.SlingDataFetcherEnvironment;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(
service = SlingDataFetcher.class,
property = {
"name=exampleDataFetcher"
}
)
public class ExampleDataFetcher implements SlingDataFetcher<String> {
private static final Logger LOGGER = LoggerFactory.getLogger(ExampleDataFetcher.class);
@Override
public String get(SlingDataFetcherEnvironment environment) throws Exception {
LOGGER.info("Fetching data in ExampleDataFetcher...");
return "Hello from ExampleDataFetcher!";
}
}
What additional steps are required to ensure the custom schema is registered and linked to the Sling Data Fetcher?
Are there specific configurations needed for the schema to appear in the GraphiQL editor?
Views
Replies
Total Likes
Views
Likes
Replies
Views
Likes
Replies