sikanhe / gqtx

Code-first Typescript GraphQL Server without codegen or metaprogramming
458 stars 13 forks source link

Return the root query type #59

Closed maraisr closed 2 years ago

maraisr commented 2 years ago

With a

const query = t.queryType({ ... });

const queryable = t.interfaceType({
    name: "Queryable",
    fields: () => [
        t.abstractField({
            name: "query",
            type: t.NonNull(query),
        }),
    ],
});

how do I define a resolver for:

const mutationPayload = t.objectType({
    name: 'MyMutationPayload',
    interfaces: [queryable],
    fields: () => [
        t.field({ name: 'query', type: t.NonNull(query) })
    ]
});

const MyMutation = t.field({
    name: "MyMutation",
    type: mutationPayload,
    async resolve(src, args, ctx) {
        // .. stuff
        return { /* do I need to put query here somehow? */ }
    }
})

t.mutationType({
    fields: () => [MyMutation],
})

Usecase


mutation DoThing {
    MyMutation {
        query {
            hello # 👈 get this resolved from the root.
        }
    }
}