Help with subscribe arguments #289
-
I'm probably doing something wrong, but how can I pass the subscribe GraphQL arguments This is a quick and silly example but has the pattern I follow: types, objects, field and async generator. type RandomIntInput = {
min: number
max: number
qty: number
}
const RandomIntInputObject = builder.inputRef<RandomIntInput>('RandomIntInput')
RandomIntInputObject.implement({
fields: (t) => ({
min: t.int(),
max: t.int(),
qty: t.int()
})
})
builder.subscriptionField('randomInts', (t) =>
t.field({
type: 'Int',
args: { input: t.arg({ type: RandomIntInputObject, required: true }) },
subscribe: (root, { input }, context, info) => randomInts(input),
resolve: async (payload: number) => payload
})
)
async function* randomInts(input: RandomIntInput) {
const { qty = 10, min = -10, max = 10 } = input // <- This always fails because it is undefined
for (let i = 1; i <= qty; ++i) {
await sleep(500)
yield randomIntFromInterval(min, max)
}
}
const sleep = (ms: number) => new Promise<void>((resolve) => setTimeout(resolve, ms))
const randomIntFromInterval = (min: number, max: number) => Math.floor(Math.random() * (max - min + 1) + min) When I call the async generator the input is always |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
|
Beta Was this translation helpful? Give feedback.
-
I suspect the issue is with how you are executing your query, or how the server is parsing/passing arguments. The following works for me, but if you have a more complete example, including how you are executing your subscription that might help. import SchemaBuilder from "@pothos/core";
import { subscribe } from "graphql";
import gql from "graphql-tag";
const builder = new SchemaBuilder<{ DefaultInputFieldRequiredness: true }>({
defaultInputFieldRequiredness: true,
});
type RandomIntInput = {
min: number;
max: number;
qty: number;
};
const RandomIntInputObject = builder.inputRef<RandomIntInput>("RandomIntInput");
RandomIntInputObject.implement({
fields: (t) => ({
min: t.int(),
max: t.int(),
qty: t.int(),
}),
});
const sleep = (ms: number) =>
new Promise<void>((resolve) => setTimeout(resolve, ms));
const randomIntFromInterval = (min: number, max: number) =>
Math.floor(Math.random() * (max - min + 1) + min);
async function* randomInts(input: RandomIntInput) {
const { qty = 10, min = -10, max = 10 } = input;
for (let i = 1; i <= qty; ++i) {
await sleep(500);
yield randomIntFromInterval(min, max);
}
}
builder.queryType({
fields: (t) => ({ hello: t.string({ resolve: () => "world" }) }),
});
builder.subscriptionType({});
builder.subscriptionField("randomInts", (t) =>
t.field({
type: "Int",
args: { input: t.arg({ type: RandomIntInputObject, required: true }) },
subscribe: (root, { input }, context, info) => randomInts(input),
resolve: async (root: number) => {
return root;
},
})
);
const schema = builder.toSchema({});
const result = subscribe({
schema,
document: gql`
subscription {
randomInts(input: { min: 1, max: 10, qty: 10 })
}
`,
});
result.then(async (itr) => {
for await (const val of itr) {
console.log(val.data.randomInts);
}
}); I ran this with and a tsconfig file with: {
"compilerOptions": {
"strict": true,
"module": "commonjs",
"jsx": "preserve",
"esModuleInterop": true,
"sourceMap": true,
"allowJs": true,
"target": "es2021",
"lib": [
"es6",
"dom"
],
"rootDir": "src",
"moduleResolution": "node"
}
} |
Beta Was this translation helpful? Give feedback.
-
I have another subscription example here if anyone else needs it: https://github.com/theogravity/graphql-pothos-server-example/tree/main/src/gql/subscriptions/posts |
Beta Was this translation helpful? Give feedback.
I suspect the issue is with how you are executing your query, or how the server is parsing/passing arguments. The following works for me, but if you have a more complete example, including how you are executing your subscription that might help.