-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GraphQL ADR: Prototype resolvers for tsp-client.ts
Base this on below [1]'s instructions and resulting skeleton files, amended for TSP's /health and (limited) /traces endpoints solely. Include an executable bash ./test script with its assumptions documented in it. Join an optional VS Code launch configuration that can replace running [2] (below) manually; use it for occasional debugging too. Using these hereby added resolvers depends on a running default trace-server locally. Per [1], resolvers are in JavaScript as they only demonstrate this limited scope. TypeScript may be used if ever expanding such use of resolvers, depending on if GraphQL ever gets in TSP or not. Refer to [3] from this repository, for how to call client's checkHealth; refer to [4] for fetchTraces but only return their amount. More on how to implement resolvers, including more advanced ones, is in [5]. REST endpoints can otherwise be resolved through a data source [6], but that approach remains out of this small prototype scope for now. Document the nature of this prototype, along with steps, in the ADR. Add the related reference about resolving Graph queries with REST endpoints. [1] https://www.apollographql.com/docs/apollo-server/getting-started/ [2] node index.js [3] trace-viewer-contribution.ts [4] trace-manager.ts [5] https://www.apollographql.com/docs/apollo-server/data/resolvers/ [6] https://www.apollographql.com/docs/apollo-server/data/data-sources/#restdatasource-reference Signed-off-by: Marco Miller <marco.miller@ericsson.com>
- Loading branch information
1 parent
b148568
commit f48afa8
Showing
7 changed files
with
970 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# About these files | ||
|
||
Refer to `../../0002-GraphQL.md` to know more about these files and their purpose. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// This is a stripped version of [1], down to showing TSP's /health and /traces endpoints. | ||
// [1] https://www.apollographql.com/docs/apollo-server/getting-started/#step-3-define-your-graphql-schema | ||
|
||
const { ApolloServer, gql } = require('apollo-server'); | ||
const { TspClient } = require('tsp-typescript-client/lib/protocol/tsp-client'); | ||
|
||
// A schema is a collection of type definitions (hence "typeDefs"). | ||
const typeDefs = gql` | ||
# The "Query" type is special: it lists all of the available queries that | ||
# clients can execute, along with the return type for each. Both non-nullable. | ||
type Query { | ||
status: String! | ||
traces: Int! | ||
} | ||
`; | ||
|
||
const baseUrl = "http://localhost:8080/tsp/api"; | ||
|
||
// Resolvers define the technique for fetching the types defined in the schema. | ||
// Here, a default yet running trace-server is assumed for showcase simplicity. | ||
const resolvers = { | ||
Query: { | ||
async status() { | ||
const tspClient = new TspClient(baseUrl); | ||
try { | ||
const response = await tspClient.checkHealth(); | ||
if (response.isOk()) { | ||
// Then assume presence of the corresponding model. | ||
return response.getModel().status; | ||
} | ||
// Reliably return the resulting message otherwise. | ||
return response.getStatusMessage(); | ||
} catch (error) { | ||
// Likely caused by the missing local trace-server. | ||
return error.message; | ||
} | ||
}, | ||
async traces() { | ||
// Same simple approach as above. Returns how many traces only. | ||
const tspClient = new TspClient(baseUrl); | ||
try { | ||
const response = await tspClient.fetchTraces(); | ||
if (response.isOk()) { | ||
return response.getModel().length; | ||
} | ||
return response.getStatusCode(); | ||
} catch (error) { | ||
return -1; | ||
} | ||
} | ||
}, | ||
}; | ||
|
||
const server = new ApolloServer({ typeDefs, resolvers }); | ||
|
||
// The `listen` method launches a web server. | ||
server.listen().then(({ url }) => { | ||
console.log(`🚀 Server ready at ${url}`); | ||
}); |
Oops, something went wrong.