flesch
John is a mild-mannered software engineering living in a quiet Chicago suburb with his wife, two kids, and a few dogs. Having never bruised or broken a bone and often described as "surprisingly strong," he is definitely not a superhero's alter-ego.
Joined June 7, 2024
Public vals
2
flesch
altairClient
HTTP
Altair GraphQL Client import { altairClient } from "https://esm.town/v/flesch/altairClient";
import { graphql, GraphQLObjectType, GraphQLSchema, GraphQLString } from "https://esm.sh/graphql";
// Define the GraphQL schema
const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: "Query",
fields: {
hello: {
type: GraphQLString,
resolve: () => "Hello from Val Town GraphQL API!",
},
},
}),
});
// Function to handle GraphQL requests
async function handleGraphQLRequest(request: Request): Promise<Response> {
const { query, variables } = await request.json();
const result = await graphql({
schema,
source: query,
variableValues: variables,
});
return new Response(JSON.stringify(result), {
headers: {
"Content-Type": "application/json"
},
});
}
// HTTP handler function
export default altairClient(async function(req: Request): Promise<Response> {
if (req.method === "POST") {
return handleGraphQLRequest(req);
} else {
return new Response("This endpoint only accepts POST requests for GraphQL queries.", { status: 405 });
}
}); ↑ https://www.val.town/v/flesch/graphqlAPIEndpoint
1