Can anyone point me to a reference implementation of nested / namespaced Mutations?
mutation {
Diagnostics {
doSomeAction(input:3){
success
message
}
}
}
Presently have single level working just fine (i.e. no Diagnostics
in the object above). I can define the schema just fine however run into problems with the sub-mutation actually being resolved.
const express = require("express");
const { gql, ApolloServer } = require("apollo-server-express");
const heroes = [
{ id: 1000, name: "bunny" },
{ id: 2000, name: "ram" }
];
const typeDefs = gql`
type Query {
heroes: [Hero]
getHero(id: Int): Hero
}
type Hero {
id: Int
name: String
}
`;
const resolvers = {
Query: {
heroes: () => heroes,
getHero: (_, args, context) => heroes.find(e => e === args.id)
}
};
const server = new ApolloServer({ typeDefs, resolvers });
const app = express();
server.applyMiddleware({ app });
app.listen({ port: 4000 }, () =>
console.log("http://localhost:4000" + server.graphqlPath)
);
getHero: (_, args, context) => heroes.find(e => e === args.id)
{
getHero(id:1000){
id
name
}
}
export const resolvers = {
User: {
hasLocations(obj, args, context, info) {
// -- count the locations and set to false / true
return true;
}
}
};
hasLocation
field?
relay
library. I created a SO post here: https://stackoverflow.com/questions/61684910/graphql-go-relay-the-base64-hash-of-my-node-id-is-not-what-i-expect. If anybody can help, would really appreciate it!
func AsyncResolve(fn graphql.FieldResolveFn) graphql.FieldResolveFn {
return func(p graphql.ResolveParams) (interface{}, error) {
type result struct {
data interface{}
err error
}
ch := make(chan *result, 1)
go func() {
defer close(ch)
data, err := fn(p)
ch <- &result{data: data, err: err}
}()
return func() (interface{}, error) {
r := <-ch
return r.data, r.err
}, nil
}
}
With the above function, you could now easily do stuff like the following:
fields := graphql.Fields{
"hello": &graphql.Field{
Type: graphql.String,
Resolve: AsyncResolve(func(p graphql.ResolveParams) (interface{}, error) {
fmt.Println("hello")
time.Sleep(5 * time.Second)
fmt.Println("hello done")
return "hello", nil
}),
},
"world": &graphql.Field{
Type: graphql.String,
Resolve: AsyncResolve(func(p graphql.ResolveParams) (interface{}, error) {
fmt.Println("world")
time.Sleep(3 * time.Second)
fmt.Println("world done")
return "world", nil
}),
},
}
and voila, you've got resolvers that run concurrently :)
fields must be an object with field names as keys or a function which return such an object