简体   繁体   中英

graphql mutation response type

We have a lot of mutations in our service that all of those return the same type of response like there:

export default new GraphQLObjectType({
  name: 'MessageResponse',
  fields: () => ({
    message: {
      type: GraphQLString,
    },
  }),
});

My question is could I return this type for all of these mutations as a best practice or I have to create single type for each of those mutations?

The only reason you would want to have different types in this scenario is if you needed it implement a different resolve function for your message field. If that's not the case, then it's perfectly fine to use the same type.

Also, bear in mind that it's also possible to just return a scalar at root level. For instance, your Mutation type can look like this:

new GraphQLObjectType({
  name: 'Mutation',
  fields: () => ({
    someMutation: {
      type: GraphQLString,
    },
  }),
})

This isn't always a good idea (for example, if you anticipate need to return additional information in the future). However, it's worth pointing out that it is possible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM