0

I am getting a response from an Api that looks like this:

property: Array(100)
0: {identifier: {…}, address: {…}, location: {…}, vintage: {…}}
1: {identifier: {…}, address: {…}, location: {…}, vintage: {…}}
2: {identifier: {…}, address: {…}, location: {…}, vintage: {…}}
3: {identifier: {…}, address: {…}, location: {…}, vintage: {…}}

I am wanting a list of some specified fields in the address object for instance just country and oneLine, but for every index of the property

array

address:
country: "US"
countrySubd: "CA"
line1: "1702 ELKHORN RD"
line2: "ROYAL OAKS, CA 95076"
locality: "Royal Oaks"
matchCode: "ExaStr"
oneLine: "1702 ELKHORN RD, ROYAL OAKS, CA 95076"
postal1: "95076"
postal2: "9218"
postal3: "R002"

I have been struggling for 2 days on how to write the schema for this in my graphql schema page. Can somebody please help me?

here is what I have been trying but keep getting null value for data

require("es6-promise").polyfill();
require("isomorphic-fetch");

const {
  GraphQLString,
  GraphQLList,
  GraphQLSchema,
  GraphQLObjectType,
  GraphQLInt
} = require("graphql");



const Identifier = new GraphQLObjectType({
  name: "identifier",
  fields: () => ({
    obPropId: { type: GraphQLInt }
  })
});

const AddressType = new GraphQLObjectType({
    name: 'Address',
    fields: () => ({
        country: { type: GraphQLString },
        oneLine: {type: GraphQLString }

    })
})

const RootQuery = new GraphQLObjectType({
  name: "RootQueryType",
  fields: {
    property: {
      type: new GraphQLList(Identifier),
      resolve(parent, args) {
        return fetch(
          "https://api.gateway.attomdata.com/propertyapi/v1.0.0/property/address?postalcode=95076&page=1&pagesize=100",
          {
            headers: {
              Accept: "application/json",
              APIKey: "XXXXXXXXXXXXXXXXXXXX"
            }
          }
        )
          .then((response) => { 
            const jsonResponse = response.json();
            return jsonResponse
          }).then((jsonResonse) => console.log(JSON.stringify(jsonResonse)))
          .then(res => res.data)
          .catch(error => {
            console.log(error);
          });
      }
    }
  }
});

module.exports = new GraphQLSchema({
  query: RootQuery
});

Im running it on a express server and do my checks on localhost:5000/graphql


Jesse James
  • 75
  • 1
  • 9
  • Please show all the relevant code, including what you've tried, as well as what errors or unexpected behavior you encountered. – Daniel Rearden Feb 27 '20 at 00:25
  • 1
    To me, it seems like you are missing the type in-between `Address` and the query field. You introduce `type Property { id: Identifier, address: Address }`. Then your property query would return an array of that. https://codesandbox.io/s/elegant-ellis-9f1yg – Herku Feb 28 '20 at 10:55
  • @Herku thanks man That was it!! – Jesse James Feb 28 '20 at 11:41
  • I will add an answer if you don't mind – Herku Mar 02 '20 at 14:02

1 Answers1

0

In the comments we were able to work out the following:

Another type is required to connect the Address type with the RootQuery type. We can introduce the type and adjust the return type of the query type:

type Property {
  id: Identifier
  address: Address
}

type Query {
  property: [Property] # consider using plural field name "properties"
}

I created a working Codesandboy to show how it behaves.

Herku
  • 7,198
  • 27
  • 36