简体   繁体   中英

GraphQL on top of REST API

Morning guys, I'm currently working on writing GraphQL on top of REST API in it. Say that a JSON response is an object 'user' that contains elements such as id, name, email and an 'address' which is an object consisting of street, suite, city, and zipcode. How should I declare the type in the schema for this 'address' field, which contains multiple elements inside. I have seen the documentation and could not understand what would be the correct type for my scenario.

The snippet of the schema related is below:

export default new GraphQLObjectType({
    name: 'User',
    description: 'User(s) object in JSONPlaceholder Fake API',
    fields: () => ({
        id: {
            type: GraphQLID
        },
        name: {
            type: GraphQLString
        },
        username: {
            type: GraphQLString
        },
        email: {
            type: GraphQLString
        },
        address: {
            type: (?)
        }
    })
})

Below is my JSON API which I target.

{
  "id": 1,
  "name": "Leanne Graham",
  "username": "Bret",
  "email": "Sincere@april.biz",
  "address": {
    "street": "Kulas Light",
    "suite": "Apt. 556",
    "city": "Gwenborough",
    "zipcode": "92998-3874",
    "geo": {
      "lat": "-37.3159",
      "lng": "81.1496"
    }
  },
  "phone": "1-770-736-8031 x56442",
  "website": "hildegard.org",
  "company": {
    "name": "Romaguera-Crona",
    "catchPhrase": "Multi-layered client-server neural-net",
    "bs": "harness real-time e-markets"
  }
}

I think of creating separate objects and creating resolver to only fetch that particular 'address' object, but that means I'm querying twice. I assume there might be some workaround to avoid this.

Thanks in advance!

As you mentioned you should create a new GraphQLObjectType to represent the address object and its fields and then set this as the type of address. In this case as the field names match the JSON payload there is no requirement for a custom resolver.

If you think you might end up making duplicate calls to a REST API you may find dataloaders library from facebook helpful. You can use it to ensure that a given request to a REST API is made only once per request. It can also help out with other tings like batching.

You code would look something like this -

const AddressType = new GraphQLObjectType({
    name: 'AddressType',
    description: 'Address',
    fields: () => ({
        // then add all the fields on address  
    })
})

export default new GraphQLObjectType({
    name: 'User',
    description: 'User(s) object in JSONPlaceholder Fake API',
    fields: () => ({
        id: {
            type: GraphQLID
        },
        name: {
            type: GraphQLString
        },
        username: {
            type: GraphQLString
        },
        email: {
            type: GraphQLString
        },
        address: {
            type: AddressType
        }
    })
})

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