npm i -D typescript tsx
npm i --save-dev graphql @graphql-tools/schema graphql-jit
import { makeExecutableSchema } from "@graphql-tools/schema";
import { parse } from "graphql";
import { compileQuery, isCompiledQuery } from "graphql-jit";
const typeDefs = `
type Query {
hello: String
goodbye: String
users: [User!]!
}
type User {
name: String!
age: Int!
}
`;
const users = [
{
name: "John",
age: 30
},
{
name: "Jane",
age: 25
}
];
const resolvers = {
Query: {
hello: () => "World!",
goodbye: () => "Goodbye!",
users: () => users
},
};
const schema = makeExecutableSchema({ typeDefs, resolvers });
const query = `
query GetHello {
hello
}
query GetGoodbye {
goodbye
}
query GetUsers
{
users
{
name
age
}
}
`;
const document = parse(query);
const compilerOptions = {
customJSONSerializer: true,
};
const operationName = "GetUsers";
const compiledQuery = compileQuery(schema, document, operationName, compilerOptions);
if (!isCompiledQuery(compiledQuery)) {
console.error(compiledQuery);
throw new Error("Error compiling query");
}
const executionResult = await compiledQuery.query(null, null, null);
//console.log(executionResult);
console.log(JSON.stringify(executionResult, null, 2));
Top comments (0)