Download the schema
Either route gives the same SDL for hosted Darpan. From the live API.GET /rest/s1/graphql/sdl needs no key. It returns JSON with the SDL in one string field, sdl, so unwrap it:
graphql-reference/schema.graphql:
Introspection (
__schema, __type) is a query like any other: send it to /graphql/public with an API key. Without one it returns UNAUTHENTICATED. Only /graphql/sdl is open.Generate a typed TypeScript client
This uses GraphQL Code Generator with its client preset.-
Install it next to your code:
-
Save
schema.graphqlin the project root, and addcodegen.ts:codegen.ts -
Write each query in its own
.graphqlfile undersrc/:src/morning.graphql -
Generate:
src/gql/graphql.tsnow holdsMorningDocument,MorningQueryandMorningQueryVariables. Run it again whenever you change a query or download a new schema. -
Call the API with the generated types:
Run it withsrc/morning.ts
npx tsx src/morning.ts, or build it with your usual bundler. A misspelt field or a missing$auditnow fails at generate or compile time, not in production.
Pitfalls
Related
- Common tasks: queries to start from
- Versioning: how the schema changes over time
- Errors and Cost and throttling