|
cwist
Modern C web framework with libttak runtime
|
#include <cwist/net/graphql/graphql.h>#include <cwist/core/mem/alloc.h>#include <cwist/core/sstring/sstring.h>#include <string.h>#include <ctype.h>#include <stdlib.h>#include <stdio.h>
Go to the source code of this file.
Typedefs | |
| typedef struct graphql_field | graphql_field_t |
Enumerations | |
| enum | gql_op_type_t { GQL_OP_QUERY , GQL_OP_MUTATION } |
Functions | |
| cwist_graphql_schema_t * | cwist_graphql_schema_create (void) |
| Allocate an empty GraphQL schema with no query or mutation fields. | |
| void | cwist_graphql_schema_destroy (cwist_graphql_schema_t *schema) |
| Destroy a schema and all registered query/mutation fields. | |
| bool | cwist_graphql_add_query (cwist_graphql_schema_t *schema, const char *field, cwist_graphql_resolver_fn resolver, void *ctx) |
| Register a resolver for a GraphQL query field. | |
| bool | cwist_graphql_add_mutation (cwist_graphql_schema_t *schema, const char *field, cwist_graphql_resolver_fn resolver, void *ctx) |
| Register a resolver for a GraphQL mutation field. | |
| cwist_error_t | cwist_graphql_execute (cwist_graphql_schema_t *schema, const char *request_json, cwist_sstring **out_json) |
| Execute a GraphQL query or mutation from a JSON request body. | |
| bool | cwist_graphql_parse_subscription (const char *query, const cJSON *variables, char *field_out, size_t field_cap, cJSON **args_out) |
Extract the root field name and parsed arguments of a subscription operation. | |
| void | cwist_graphql_serve (cwist_graphql_schema_t *schema, cwist_http_request *req, cwist_http_response *res) |
| HTTP handler serving GraphQL requests over POST. | |
| typedef struct graphql_field graphql_field_t |
| bool cwist_graphql_add_mutation | ( | cwist_graphql_schema_t * | schema, |
| const char * | field, | ||
| cwist_graphql_resolver_fn | resolver, | ||
| void * | ctx | ||
| ) |
Register a resolver for a GraphQL mutation field.
| schema | Schema to register the field on. |
| field | Mutation field name (must be a valid GraphQL name). |
| resolver | Resolver callback invoked when the field is mutated. |
| ctx | Opaque pointer passed to the resolver. |
| true | The field was registered (or its resolver updated). |
| false | NULL schema or invalid field/resolver. |
Definition at line 172 of file graphql.c.
References cap.
| bool cwist_graphql_add_query | ( | cwist_graphql_schema_t * | schema, |
| const char * | field, | ||
| cwist_graphql_resolver_fn | resolver, | ||
| void * | ctx | ||
| ) |
Register a resolver for a GraphQL query field.
| schema | Schema to register the field on. |
| field | Query field name (must be a valid GraphQL name). |
| resolver | Resolver callback invoked when the field is queried. |
| ctx | Opaque pointer passed to the resolver. |
| true | The field was registered (or its resolver updated). |
| false | NULL schema or invalid field/resolver. |
Definition at line 157 of file graphql.c.
References cap.
| cwist_error_t cwist_graphql_execute | ( | cwist_graphql_schema_t * | schema, |
| const char * | request_json, | ||
| cwist_sstring ** | out_json | ||
| ) |
Execute a GraphQL query or mutation from a JSON request body.
The request must be a JSON object with a string "query" member and an optional "variables" object. On success *out_json receives a compact JSON response of the form {"data": {...}} with an "errors" array added when resolution produced errors. Ownership of *out_json transfers to the caller.
| schema | Schema providing the query/mutation fields. |
| request_json | JSON-encoded GraphQL request text. |
| out_json | Receives the response body string on success. |
Definition at line 467 of file graphql.c.
References cap, CWIST_GRAPHQL_MAX_QUERY_SIZE, cwist_sstring_assign(), cwist_sstring_create(), GQL_OP_MUTATION, and GQL_OP_QUERY.
Referenced by cwist_graphql_serve().
| bool cwist_graphql_parse_subscription | ( | const char * | query, |
| const cJSON * | variables, | ||
| char * | field_out, | ||
| size_t | field_cap, | ||
| cJSON ** | args_out | ||
| ) |
Extract the root field name and parsed arguments of a subscription operation.
Used by the experimental WS subscription layer (cwist/net/graphql/graphql_ws.h); not part of the query/mutation execute path. Only the first root field is reported, matching the single-stream model of the graphql-ws broker.
| query | GraphQL subscription query text. |
| variables | Optional cJSON object of variable values. |
| field_out | Buffer receiving the resolved root field name. |
| field_cap | Capacity of field_out in bytes. |
| args_out | Receives a newly allocated cJSON argument object (NULL when the field carries no arguments); caller owns it. |
| true | The subscription field and arguments were extracted. |
| false | Invalid arguments or malformed subscription query. |
Definition at line 557 of file graphql.c.
References cap.
| cwist_graphql_schema_t * cwist_graphql_schema_create | ( | void | ) |
Allocate an empty GraphQL schema with no query or mutation fields.
Definition at line 79 of file graphql.c.
References cap, and cwist_alloc().
| void cwist_graphql_schema_destroy | ( | cwist_graphql_schema_t * | schema | ) |
Destroy a schema and all registered query/mutation fields.
| schema | Schema to free (may be NULL). |
Definition at line 105 of file graphql.c.
References cwist_free().
| void cwist_graphql_serve | ( | cwist_graphql_schema_t * | schema, |
| cwist_http_request * | req, | ||
| cwist_http_response * | res | ||
| ) |
HTTP handler serving GraphQL requests over POST.
Executes the request body via cwist_graphql_execute() and writes the JSON response. Non-POST requests or execution failures yield a 400 status with an error body. The Content-Type header is always set to application/json.
| schema | Schema used to resolve the request. |
| req | Incoming HTTP request (must use POST). |
| res | Response object receiving status, body, and headers. |
Definition at line 618 of file graphql.c.
References cwist_http_request::body, cwist_http_response::body, cap, cwist_graphql_execute(), CWIST_HTTP_BAD_REQUEST, cwist_http_header_add(), CWIST_HTTP_OK, CWIST_HTTP_POST, cwist_sstring_assign(), cwist_sstring_assign_len(), cwist_sstring_destroy(), cwist_sstring::data, cwist_http_response::headers, cwist_http_request::method, cwist_sstring::size, and cwist_http_response::status_code.