How to create a nested resolver in apollo graphql server

梦想的初衷 提交于 2019-12-31 21:44:09

问题


Given the following apollo server graphql schema I wanted to break these down into separate modules so I don't want the author query under the root Query schema.. and want it separated. So i added another layer called authorQueries before adding it to the Root Query

type Author {
    id: Int,
    firstName: String,
    lastName: String
}  
type authorQueries {
    author(firstName: String, lastName: String): Author
}

type Query {
    authorQueries: authorQueries
}

schema {
    query: Query
}

I tried the following.. you can see that authorQueries was added as another layer before the author function is specified.

Query: {
    authorQueries :{
        author (root, args) {
            return {}
       }
    }
}

When querying in Graphiql, I also added that extra layer..

{
    authorQueries {
        author(firstName: "Stephen") {
            id
        }
    }
}

I get the following error.

"message": "Resolve function for \"Query.authorQueries\" returned undefined",


回答1:


To create a "nested" resolver, simply define the resolver on the return type of the parent field. In this case, your authorQueries field returns the type authorQueries, so you can put your resolver there:

{
  Query: { authorQueries: () => ({}) },
  authorQueries: {
    author(root, args) {
      return "Hello, world!";
    }
  }
}

So in the technical sense, there is no such thing as a nested resolver - every object type has a flat list of fields, and those fields have return types. The nesting of the GraphQL query is what makes the result nested.




回答2:


I found that returning functions on the parent fields return type results in the this arg being bound, and breaks the resolver interface b/c the nested resolver doesn't the parent as the first argument.

For inline type definitions

import {
  graphql,
} from 'graphql';

import {
  makeExecutableSchema, IResolverObject
} from 'graphql-tools';

const types = `
type Query {
  person: User
}

type User {
  id: ID
  name: String,
  dog(showCollar: Boolean): Dog
}

type Dog {
  name: String
}
`;

const User: IResolverObject = {
  dog(obj, args, ctx) {
    console.log('Dog Arg 1', obj);
    return {
      name: 'doggy'
    };
  }
};

const resolvers = {
  User,
  Query: {
    person(obj) {
      console.log('Person Arg 1', obj);
      return {
        id: 'foo',
        name: 'bar',
      };
    }
  }
};

const schema = makeExecutableSchema({
  typeDefs: [types],
  resolvers
});

const query = `{ 
  person {
    name,
    dog(showCollar: true) {
      name
    }
  }
 }`;


graphql(schema, query).then(result => {
  console.log(JSON.stringify(result, null, 2));
});

// Person Arg 1 undefined
// Dog Arg 1 { id: 'foo', name: 'bar' }
// {
//   "data": {
//     "person": {
//       "name": "bar",
//       "dog": {
//         "name": "doggy"
//       }
//     }
//   }
// }

You can also use addResolveFunctionsToSchema as seen in the below gist.

https://gist.github.com/blugavere/4060f4bf2f3d5b741c639977821a254f



来源:https://stackoverflow.com/questions/40901845/how-to-create-a-nested-resolver-in-apollo-graphql-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!