Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

February Developer Challenge - CAP Plugins: Week 2

thomas_jung
Developer Advocate
Developer Advocate
17,525

If you missed week 1, you can find all the details: here

Now that the challenge is complete: here is my sample solution: jung-thomas/dev-challenge-feb-2024: Developer Challenge February 2024 (github.com)

This week's challenge is going to build upon what we learned last week with the OData V2 plugin. We will continue working with the same project, but now we want to add support for GraphQL.

What is GraphQL?

GraphQL is a query language for APIs and a runtime for executing those queries by using a type system you define for your data. Unlike REST, which uses multiple URLs to access different resources, GraphQL accesses all the resources from a single endpoint with queries that articulate exactly what data is needed.

GraphQL and OData are both specifications for building and consuming APIs but take different approaches to some aspects.

GraphQL is a query language for APIs and a runtime for executing those queries by using a type system you define for your data.

OData defines a set of best practices for building and consuming RESTful APIs. It extends REST by providing a uniform way to describe both the data and the data model, enabling more detailed queries and interactions with the data.

How does this impact the SAP Cloud Application Programming Model?

The SAP Cloud Application Programming model was designed to be protocol agnostic as much as possible. While OData V4 is the default service type exposed by CAP applications; CAP was never intended to only support OData or just one version of that specification. Therefore its possible from a single model and service definition to support both OData and GraphQL even with the differences in approaches used by the two different protocols!

Your Task this week

Use the GraphQl Adapter plug-in to add GraphQL support to your project and to ONLY the CatalogService endpoint. And make sure that this same service also still supports OData V4 in parallel.

GraphQL Adapter: The GraphQL Adapter is a protocol adapter that generically generates a GraphQL schema for the models of an application and serves an endpoint that allows you to query your services using the GraphQL query language.

https://github.com/cap-js/graphql

To complete the challenge, post a screenshot from the graphql test tool (which can be accessed from /graphql url of your application) running a query against the ListOfBooks entity in the CatalogService. 

graphql1.png

 

Bonus 1: The test tool doesn't automatically include a link to the GraphQL test tool. But using a server.js file we can use the on serving event to extend the CAP test tool ourselves. Your bonus challenge is to add the code to your project to expose a link in the test tool to the GraphQL page but only for the service endpoints that support GraphQL. Hint: Several of the samples in the SAP-Samples repository on GitHub perform this technique.
graphql2.png

57 REPLIES 57
Read only

qmacro
Developer Advocate
Developer Advocate
Read only

Read only

kumarniti4
Product and Topic Expert
Product and Topic Expert
14,569

Screenshots:

kumarniti4_2-1708017014504.png

 

Bonus:

kumarniti4_1-1708016958921.png

Thanks, @qmacro, for the additional hint. I was so close already but gave up. I tried again after seeing your hint and found the example.

Read only

ajmaradiaga
Developer Advocate
Developer Advocate
Read only

satya-dev
Participant
Read only

egenoves
Participant
14,434

The GraphSql plugin:

sap-challenge-2.jpg

Read only

smok1
Explorer
14,406

Added GraphQL:

graphql.jpg

Bonus 1:

smok1_0-1707973163561.png

 

Read only

Ruthiel
Product and Topic Expert
Product and Topic Expert
14,288

 

Ruthiel_0-1707991680455.png

I tried to add the GraphQL link unsuccessfully. 
Maybe something wrong with my server.js code.

 

 

Read only

thomas_jung
Developer Advocate
Developer Advocate
14,247

If you need a hint or help with the bonus feel free to direct message me here on the Community platform.

Read only

0 Kudos
8,527

I need help on this as i am getting error 

Kaustav_0-1709970419191.png

 

 

Read only

thomas_jung
Developer Advocate
Developer Advocate
0 Kudos
8,381

You need to write a query before executing. From your other post it looks like you got it. But if if still having problems, let us know.

Read only

SandipAgarwalla
Active Contributor
Read only

12,803

Can't seem to get the bonus execise correct. Found the server.js, but does not work 🙄

Read only

0 Kudos
12,579

@SandipAgarwalla - if you want, direct message me on the Community Platform. I can help you with the troubleshooting or share my solution. It's only a couple of lines of code. 

Read only

stickman_0x00
Participant
14,317
 

stickman_0x00_2-1707998726493.png

 

stickman_0x00_1-1707998714009.png

Felt more like I was learning how to search github than understanding how it works adding the links 😕

Read only

14,247

Searching GitHub is a good skill to master! There's so much great material on CAP and everything there, that being able to pinpoint something or fish out an example of how to do something that the one that can wield search effectively has an advantage over those that can't. Plus you need to recognise and sift through CAP code examples to find the solution, so that's CAP specific! 🙂

Read only

14,060

Can't argue with that and this time you guys did gave us enough tips to solve the problem 😋. In the mean time learned that how to do for the rest protocol too.

Continue keep bringing them!

Read only

13,464

I just debug the 'standard code', understand how the 'standard Cap index' page is constructed and did my own solution. Curious to find the SAP sample solution on GIT. 

Read only

qmacro
Developer Advocate
Developer Advocate
14,274

If I may be so bold and offer a bit of a clue to those looking to complete the bonus part of this week's task. The search facility on GitHub is fantastic, and with the help of search modifiers, you can narrow in on what you're looking for with ease. 

The base search is at https://github.com/search and the query language includes parameters that allow you to specific the context of your search. For example, you might want to set the `org` to `SAP-samples` and the `type` to `code`, and perhaps the `path` to something else, while looking for the things that @thomas_jung hinted at in the bonus description 🙂

You should end up with a very specific URL that you can GET in your browser, and there are (at the time of writing) only two results, both of which have code that you can use.

Good luck! 

Read only

NicholasB
Product and Topic Expert
Product and Topic Expert
0 Kudos
12,392

I must admit that it took me a while but this hint is pure gold. Thank you!

Read only

Former Member
Read only

gustavo_paulo
Explorer
Read only

ruben_alvarez
Explorer
Read only

Cmdd
Participant
12,728

GraphQL

Cmdd_0-1708085081098.png

... and the bonus part

Cmdd_1-1708085130211.png

Is it correct that the old rewriting rule (challenge #1) is not working anymore in this new scenario? I still see "Proxy rewrite rule created: "^/feb_devchallenge" ~> "/odata/v4" " in the output log but in the test tool the old name is exposed .

Read only

thomas_jung
Developer Advocate
Developer Advocate
0 Kudos
12,625

Proxy rewrite rule?  Are you trying to change the main ODataV4 URL path? If so you can do that via annotation in the service definition now since CAP 7.0. June 2023 | CAPire (cloud.sap)

Read only

12,542

Yes! Sorry, I was trying to play with exposed path. Thanks for the hint 🙂 

Read only

MeriemSouissi
Participant
Read only

NicholasB
Product and Topic Expert
Product and Topic Expert
Read only

Alpesa1990
Participant
Read only

Jarek
Explorer
Read only

brunonalon
Explorer
Read only

tobiasz_h
Active Participant