10 Jan 2022
import { createServer } from "graphql-yoga";
import { useGraphQlJit } from "@envelop/graphql-jit";
import fetch from "node-fetch";
const typeDefs = /* GraphQL */ `
type Query {
hello: String!
}
`;
const resolvers = {
Query: {
hello: async () =>
await fetch("http://localhost:1234/helloworld").then((res) => res.text()),
},
};
const server = createServer({
typeDefs,
resolvers,
plugins: [useGraphQlJit()],
maskedErrors: true,
});
server.start(() => console.log("Server is running on localhost:4000"));
import { createServer, createPubSub } from "graphql-yoga";
const pubSub = createPubSub();
const typeDefs = /* GraphQL */ `
type Query {
hello: String!
}
type Mutation {
speak(word: String!): String!
}
type Subscription {
speaking: String!
}
`;
const resolvers = {
Query: {
hello: () => "World!",
},
Mutation: {
speak: (_, { word }) => {
pubSub.publish("speak", word);
return word;
},
},
Subscription: {
speaking: {
subscribe: () => pubSub.subscribe("speak"),
resolve: (payload) => payload,
},
},
};