Friday, May 28, 2021

multiple RootQuery entry points

 As RootQuery  linked to UserType, we can go directy to Users and fetch other data from there.

To access company data directly, add it to Root query.

const RootQuery = new GraphQLObjectType({
  name: "RootQueryType",
  fields: {
    user: {
      type: UserType,
      args: { id: { type: GraphQLString } },
      resolve(parentValueargs) {
        return axios
          .get(`http://localhost:3000/users/${args.id}`)
          .then((resp=> resp.data);
      },
    },
    company:{
      type: CompanyType,
      args: {id: {type: GraphQLString}},
      resolve(parentValue,args){
        return axios.get(`http://localhost:3000/companies/${args.id}`)
        .then(resp=>resp.data);
      }
    }
  },
});

The graphql query:

{
  company(id"1"){
   id
   name
   description
  }
  }

No comments:

Post a Comment