简体   繁体   中英

customize json response in graphQL

i use Express-js and express graphQL module to create my endpoint and web service ;

i am looking for way to create custom response in graphQL my endpoint is simple

select books from database my response is

{
  "data": {
    "books": [
      {
        "id": "5b5c02beab8dc1182b2e0a03",
        "name": "dasta"
      },
      {
        "id": "5b5c02c0ab8dc1182b2e0a04",
        "name": "dasta"
      }
    ]
  }
}

but in need something like this

{
  "result": "success",
  "msg" : "list ...",
  "data": [
      {
        "id": "5b5c02beab8dc1182b2e0a03",
        "name": "dasta"
      },
      {
        "id": "5b5c02c0ab8dc1182b2e0a04",
        "name": "dasta"
      }
  ]
}

here is my bookType

const BookType = new GraphQLObjectType({
    name: 'Book',
    fields: () => ({
        id: {type: GraphQLID},
        name: {type: GraphQLString},
        genre: {type: GraphQLString},
        author_id: {type: GraphQLString},
        author: {
            type: AuthorType,
            resolve(parent, args) {
                return Author.findById(parent.author_id);
            }
        }
    })
});

That's not a legal GraphQL response. As per section 7.1 of the spec , after describing the data , errors , and extensions : top-level keys:

... the top level response map must not contain any entries other than the three described above.

You might put this data into extensions ; or make it an explicit part of your GraphQL API; or simply let "success" be implied by the presence of a result and the lack of an error.

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